/www/application/modules/gallery/gallery.php

https://github.com/kelios/imshop · PHP · 306 lines · 199 code · 51 blank · 56 comment · 23 complexity · 6b46eacb85dafa95180c22a8c8c557ff MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Image CMS
  4. *
  5. * Gallery module
  6. * Need Imagebox module
  7. */
  8. class Gallery extends MY_Controller {
  9. public $conf = array(
  10. 'upload_url' => 'uploads/gallery/',
  11. 'thumbs_folder' => '_thumbs',
  12. );
  13. private $settings = array();
  14. function __construct()
  15. {
  16. parent::__construct();
  17. $this->load->module('core');
  18. $this->load->model('gallery_m');
  19. // Load gallery settings
  20. $this->settings = $this->gallery_m->load_settings();
  21. }
  22. function autoload()
  23. {
  24. $this->load->helper('gallery');
  25. $this->load->module('imagebox')->autoload();
  26. }
  27. /**
  28. * List categories and get albums from first category
  29. */
  30. function index()
  31. {
  32. $categories = $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']);
  33. $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order']);
  34. // Get covers
  35. if (is_array($albums))
  36. {
  37. $this->template->add_array(array(
  38. 'albums' => $this->create_albums_covers($albums),
  39. 'gallery_category' => $categories
  40. ));
  41. }
  42. $this->display_tpl('albums');
  43. }
  44. /**
  45. * Display category albums
  46. */
  47. function category($id = 0)
  48. {
  49. $category = $this->gallery_m->get_category($id);
  50. if ($category === FALSE)
  51. {
  52. redirect('gallery');
  53. }
  54. else
  55. {
  56. $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order'], $category['id']);
  57. if ($albums !== FALSE)
  58. {
  59. $albums = $this->create_albums_covers($albums);
  60. }
  61. $this->template->add_array(array(
  62. 'albums' => $albums,
  63. 'current_category' => $category,
  64. 'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
  65. ));
  66. $this->display_tpl('albums');
  67. }
  68. }
  69. // Display album images
  70. function album($id = 0)
  71. {
  72. $album = $this->gallery_m->get_album($id);
  73. if ($this->uri->total_segments() > 5 )
  74. $params = $this->uri->uri_to_assoc(5);
  75. else
  76. $params = $this->uri->uri_to_assoc(4);
  77. if ($album == FALSE)
  78. {
  79. show_error('Can\'t load album.');
  80. }
  81. else
  82. {
  83. // Get preview image
  84. if ($params['image'] > 0)
  85. {
  86. $n = 0;
  87. foreach ($album['images'] as $image)
  88. {
  89. if ($image['id'] == $params['image'])
  90. {
  91. $prev_img = $image;
  92. // Create prev/next links
  93. $next = $album['images'][$n + 1];
  94. $prev = $album['images'][$n - 1];
  95. $current_pos = $n + 1;
  96. }
  97. $n++;
  98. }
  99. }
  100. else
  101. {
  102. // Display first image prev
  103. $prev_img = $album['images'][0];
  104. $next = $album['images'][1];
  105. $prev = NULL;
  106. $current_pos = 1;
  107. }
  108. if ($prev_img == NULL)
  109. {
  110. $this->core->error_404();
  111. exit;
  112. }
  113. $prev_img['url'] = $this->conf['upload_url'] . $album['id'] .'/'. $prev_img['file_name'] .'_prev'. $prev_img['file_ext'];
  114. // Comments
  115. $this->load->module('comments');
  116. $this->comments->module = 'gallery';
  117. $this->comments->comment_controller = 'gallery/post_comment';
  118. $this->comments->build_comments($prev_img['id']);
  119. $this->template->add_array(array(
  120. 'album' => $album,
  121. 'thumb_url' => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
  122. 'album_link' => 'gallery/album/' . $album['id'] . '/',
  123. 'album_url' => $this->conf['upload_url'] . $album['id'] . '/',
  124. 'prev_img' => $prev_img,
  125. 'next' => $next,
  126. 'prev' => $prev,
  127. 'current_pos'=> $current_pos,
  128. 'current_category' => $this->gallery_m->get_category($album['category_id']),
  129. 'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
  130. ));
  131. $this->gallery_m->increase_image_views($prev_img['id']);
  132. $this->core->set_meta_tags(array($album['name']));
  133. $this->display_tpl('album');
  134. }
  135. }
  136. function thumbnails($id = 0)
  137. {
  138. $album = $this->gallery_m->get_album($id);
  139. if ($album == FALSE)
  140. {
  141. $this->core->error_404();
  142. exit;
  143. }
  144. $this->template->add_array(array(
  145. 'album' => $album,
  146. 'thumb_url' => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
  147. 'album_link' => 'gallery/album/' . $album['id'] . '/',
  148. 'album_url' => $this->conf['upload_url'] . $album['id'] . '/',
  149. 'current_category' => $this->gallery_m->get_category($album['category_id']),
  150. ));
  151. $this->core->set_meta_tags(array($album['name']));
  152. $this->display_tpl('thumbnails');
  153. }
  154. function post_comment()
  155. {
  156. $image_id = (int) $this->input->post('comment_item_id');
  157. $this->load->module('comments');
  158. $this->comments->module = 'gallery';
  159. if ($this->db->get_where('gallery_images', array('id' => $image_id))->num_rows() > 0)
  160. {
  161. $this->comments->add($image_id);
  162. }
  163. else
  164. {
  165. $this->core->error_404();
  166. }
  167. }
  168. // Create cover url to each album
  169. function create_albums_covers($albums = array())
  170. {
  171. $cnt = count($albums);
  172. for($i = 0; $i < $cnt; $i++)
  173. {
  174. $thumb_url = $this->conf['upload_url'] . $albums[$i]['id'] .'/'. $this->conf['thumbs_folder'].'/';
  175. $albums[$i]['cover_url'] = media_url($thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext']);
  176. if ($albums[$i]['cover_name'] == NULL)
  177. {
  178. $image = $this->gallery_m->get_last_image($albums[$i]['id']);
  179. $albums[$i]['cover_url'] = media_url($thumb_url . $image['file_name'] . $image['file_ext']);
  180. }
  181. else
  182. {
  183. $albums[$i]['cover_url'] = media_url($thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext']);
  184. }
  185. }
  186. return $albums;
  187. }
  188. // Install
  189. function _install()
  190. {
  191. if( $this->dx_auth->is_admin() == FALSE) exit;
  192. $this->load->model('install')->make_install();
  193. }
  194. // Delete module
  195. function _deinstall()
  196. {
  197. if( $this->dx_auth->is_admin() == FALSE) exit;
  198. $this->load->model('install')->deinstall();
  199. }
  200. /**
  201. * Display template file
  202. */
  203. private function display_tpl($file = '')
  204. {
  205. /**
  206. if ( file_exists($this->template->template_dir . 'gallery') )
  207. {
  208. if ( file_exists($this->template->template_dir . 'gallery/main.tpl') )
  209. {
  210. $this->template->add_array(array(
  211. 'content' => $this->template->fetch('gallery/' . $file),
  212. ));
  213. $this->template->display('gallery/main');
  214. }
  215. else
  216. {
  217. $this->template->show('gallery/' . $file);
  218. }
  219. }
  220. else
  221. {
  222. $this->template->add_array(array(
  223. 'content' => $this->fetch_tpl($file),
  224. ));
  225. $file = realpath(dirname(__FILE__)).'/templates/public/main.tpl';
  226. $this->template->display('file:' . $file);
  227. }
  228. **/
  229. $this->template->add_array(array(
  230. 'content' => $this->fetch_tpl($file),
  231. ));
  232. if (file_exists(realpath(dirname(__FILE__)).'/templates/public/main.tpl'))
  233. {
  234. $file = realpath(dirname(__FILE__)).'/templates/public/main.tpl';
  235. $this->template->display('file:' . $file);
  236. }
  237. else
  238. {
  239. // Use main site template
  240. $this->template->show();
  241. exit;
  242. }
  243. }
  244. /**
  245. * Fetch template file
  246. */
  247. private function fetch_tpl($file = '')
  248. {
  249. $file = realpath(dirname(__FILE__)).'/templates/public/'.$file.'.tpl';
  250. return $this->template->fetch('file:'.$file);
  251. }
  252. }
  253. /* End of file gallery.php */