PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/gallery/gallery.php

http://github.com/imagecms/ImageCMS
PHP | 307 lines | 207 code | 59 blank | 41 comment | 30 complexity | 7030cedb79d49922072eab9c5f7af29c MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. use CMSFactory\assetManager;
  3. use CMSFactory\Events;
  4. if (!defined('BASEPATH')) {
  5. exit('No direct script access allowed');
  6. }
  7. /**
  8. * Image CMS
  9. *
  10. * Gallery module
  11. * Need Imagebox module
  12. * @property Gallery_m $gallery_m
  13. * @property Components comments
  14. * @property Gallery_install gallery_install
  15. */
  16. class Gallery extends MY_Controller
  17. {
  18. public $conf = [
  19. 'upload_url' => 'uploads/gallery/',
  20. 'thumbs_folder' => '_thumbs',
  21. ];
  22. private $settings = [];
  23. public function __construct() {
  24. parent::__construct();
  25. $lang = new MY_Lang();
  26. $lang->load('gallery');
  27. $this->load->module('core');
  28. $this->load->model('gallery_m');
  29. // Load gallery settings
  30. $this->settings = $this->gallery_m->load_settings();
  31. $this->load->helper('gallery');
  32. }
  33. public function autoload() {
  34. }
  35. /**
  36. * List categories and get albums from first category
  37. */
  38. public function index() {
  39. $this->core->set_meta_tags(lang('Gallery', 'gallery'));
  40. $categories = $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']);
  41. $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order']);
  42. $data = [
  43. 'gallery_category' => $categories,
  44. 'total' => $this->gallery_m->getTotalImages(),
  45. 'categories' => $categories,
  46. ];
  47. // Get covers
  48. $data['albums'] = is_array($albums) ? $this->create_albums_covers($albums) : NULL;
  49. Events::create()->raiseEvent($data, 'gallery:load');
  50. assetManager::create()
  51. ->setData($data)
  52. ->registerStyle('style', FAlSE)
  53. ->render('index');
  54. }
  55. public function albums() {
  56. $this->core->set_meta_tags(lang('Gallery', 'gallery'));
  57. $categories = $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']);
  58. $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order']);
  59. $data = [
  60. 'gallery_category' => $categories,
  61. 'total' => $this->gallery_m->getTotalImages(),
  62. ];
  63. // Get covers
  64. $data['albums'] = is_array($albums) ? $this->create_albums_covers($albums) : NULL;
  65. assetManager::create()
  66. ->setData($data)
  67. ->registerStyle('style', FAlSE)
  68. ->render('albums');
  69. }
  70. /**
  71. * Display category albums
  72. * @param int $id
  73. */
  74. public function category($id = 0) {
  75. $this->core->set_meta_tags(lang('Gallery', 'gallery'));
  76. $category = $this->gallery_m->get_category($id);
  77. if ($category === FALSE) {
  78. redirect('gallery');
  79. } else {
  80. $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order'], $category['id']);
  81. if ($albums !== FALSE) {
  82. $albums = $this->create_albums_covers($albums);
  83. }
  84. $data = [
  85. 'albums' => $albums,
  86. 'current_category' => $category,
  87. 'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
  88. ];
  89. Events::create()->raiseEvent($data, 'gallery:category');
  90. assetManager::create()
  91. ->setData($data)
  92. ->render('albums');
  93. }
  94. }
  95. /**
  96. * Display album images
  97. * @param int $id
  98. */
  99. public function album($id = 0) {
  100. if (preg_match('/[A-Z]/', $this->uri->uri_string())) {
  101. redirect(site_url(strtolower($this->uri->uri_string())), 'location', 301);
  102. }
  103. $album = $this->gallery_m->get_album($id);
  104. if ($this->uri->total_segments() > 5) {
  105. $params = $this->uri->uri_to_assoc(5);
  106. } else {
  107. $params = $this->uri->uri_to_assoc(4);
  108. }
  109. if ($album == FALSE) {
  110. $this->core->error_404();
  111. }
  112. if ($params['image'] > 0) {
  113. $n = 0;
  114. foreach ($album['images'] as $image) {
  115. if ($image['id'] == $params['image']) {
  116. $prev_img = $image;
  117. // Create prev/next links
  118. $next = $album['images'][$n + 1];
  119. $prev = $album['images'][$n - 1];
  120. $current_pos = $n + 1;
  121. }
  122. $n++;
  123. }
  124. } else {
  125. // Display first image prev
  126. $prev_img = $album['images'][0];
  127. $next = $album['images'][1];
  128. $prev = NULL;
  129. $current_pos = 1;
  130. }
  131. if ($prev_img == NULL) {
  132. $this->core->error_404();
  133. exit;
  134. }
  135. $prev_img['url'] = $this->conf['upload_url'] . $album['id'] . '/' . $prev_img['file_name'] . '_prev' . $prev_img['file_ext'];
  136. $data = [
  137. 'album' => $album,
  138. 'thumb_url' => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
  139. 'album_link' => 'gallery/album/' . $album['id'] . '/',
  140. 'album_url' => $this->conf['upload_url'] . $album['id'] . '/',
  141. 'prev_img' => $prev_img,
  142. 'next' => $next,
  143. 'prev' => $prev,
  144. 'current_pos' => $current_pos,
  145. 'current_category' => $this->gallery_m->get_category($album['category_id']),
  146. 'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
  147. ];
  148. $this->gallery_m->increase_image_views($prev_img['id']);
  149. $this->core->set_meta_tags([$album['name']]);
  150. Events::create()->raiseEvent($data, 'gallery:album');
  151. assetManager::create()
  152. ->setData($data)
  153. // ->registerStyle('jquery.fancybox-1.3.4', FAlSE)
  154. ->registerStyle('style', FAlSE)
  155. // ->registerScript('jquery.fancybox-1.3.4.pack', TRUE)
  156. // ->registerScript('jquery.easing-1.3.pack', TRUE)
  157. // ->registerScript('jquery.mousewheel-3.0.4.pack', TRUE)
  158. ->render($album['tpl_file'] ?: 'album');
  159. }
  160. /**
  161. * @param int $id
  162. * @param int $page
  163. */
  164. public function thumbnails($id = 0, $page = 0) {
  165. if (preg_match('/[A-Z]/', $this->uri->uri_string())) {
  166. redirect(site_url(strtolower($this->uri->uri_string())), 'location', 301);
  167. }
  168. $album = $this->gallery_m->get_album($id, true, 15, $page * 15);
  169. if ($album == FALSE) {
  170. $this->core->error_404();
  171. exit;
  172. }
  173. $this->load->library('Pagination');
  174. $this->pagination = new \CI_Pagination();
  175. $paginationConfig['uri_segment'] = 4;
  176. $paginationConfig['base_url'] = site_url('gallery/thumbnails/' . $id);
  177. $paginationConfig['total_rows'] = ceil($album[count] / 15);
  178. $paginationConfig['last_link'] = ceil($album[count] / 15);
  179. $paginationConfig['per_page'] = 1;
  180. $paginationConfig['page_query_string'] = true;
  181. $paginationConfig['first_link'] = '1';
  182. $paginationConfig['num_links'] = 3;
  183. include_once "./templates/{$this->config->item('template')}/paginations.php";
  184. $this->pagination->initialize($paginationConfig);
  185. $data = [
  186. 'album' => $album,
  187. 'thumb_url' => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
  188. 'album_link' => 'gallery/album/' . $album['id'] . '/',
  189. 'album_url' => $this->conf['upload_url'] . $album['id'] . '/',
  190. 'current_category' => $this->gallery_m->get_category($album['category_id']),
  191. 'pagination' => $this->pagination->create_links(),
  192. ];
  193. $this->core->set_meta_tags([$album['name']]);
  194. assetManager::create()
  195. ->setData($data)
  196. ->render('thumbnails');
  197. }
  198. public function post_comment() {
  199. $image_id = (int) $this->input->post('comment_item_id');
  200. $this->load->module('comments');
  201. $this->comments->module = 'gallery';
  202. if ($this->db->get_where('gallery_images', ['id' => $image_id])->num_rows() > 0) {
  203. $this->comments->add($image_id);
  204. } else {
  205. $this->core->error_404();
  206. }
  207. }
  208. /**
  209. * Create cover url to each album
  210. * @param array $albums
  211. * @return array
  212. */
  213. public function create_albums_covers($albums = []) {
  214. $cnt = count($albums);
  215. for ($i = 0; $i < $cnt; $i++) {
  216. $thumb_url = '/' . $this->conf['upload_url'] . $albums[$i]['id'] . '/' . $this->conf['thumbs_folder'] . '/';
  217. $albums[$i]['cover_url'] = $thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext'];
  218. if ($albums[$i]['cover_name'] == NULL) {
  219. $image = $this->gallery_m->get_last_image($albums[$i]['id']);
  220. if ($image) {
  221. $albums[$i]['cover_url'] = $thumb_url . $image['file_name'] . $image['file_ext'];
  222. } else {
  223. $albums[$i]['cover_url'] = media_url('application/modules/gallery/assets/images/nophoto.jpg');
  224. }
  225. } else {
  226. $albums[$i]['cover_url'] = $thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext'];
  227. }
  228. }
  229. return $albums;
  230. }
  231. // Install
  232. public function _install() {
  233. if ($this->dx_auth->is_admin() == FALSE) {
  234. exit;
  235. }
  236. $this->load->model('gallery_install');
  237. $this->gallery_install->make_install();
  238. }
  239. // Delete module
  240. public function _deinstall() {
  241. if ($this->dx_auth->is_admin() == FALSE) {
  242. exit;
  243. }
  244. $this->load->model('gallery_install');
  245. $this->gallery_install->deinstall();
  246. }
  247. }
  248. /* End of file gallery.php */