PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/th/application/models/cms_image.php

https://gitlab.com/anurat/earththailand
PHP | 267 lines | 116 code | 29 blank | 122 comment | 12 complexity | edcc748c99ce9831b5a926669a93b75a MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CMS image class
  4. *
  5. * a class for managing image in Thaiecoalert project
  6. *
  7. * @author Anurat Chapanond
  8. *
  9. * @copyright Connectiv Co., Ltd.
  10. *
  11. * @version 1.0
  12. *
  13. * @since 07/21/14
  14. *
  15. */
  16. class Cms_image {
  17. /**
  18. * Constructor
  19. *
  20. */
  21. function __construct() {
  22. }
  23. /**
  24. * Get a list of images
  25. *
  26. * @return Image object array
  27. */
  28. public function get_image_list() {
  29. return ImageQuery::create()
  30. ->orderBy_order()
  31. ->find();
  32. }
  33. /**
  34. * Get the number of images
  35. *
  36. * @return int
  37. */
  38. public function get_image_no() {
  39. return ImageQuery::create()->count();
  40. }
  41. /**
  42. * Get a image
  43. *
  44. * @param int $image_id
  45. *
  46. * @return Image object
  47. */
  48. public function get_image( $image_id ) {
  49. return ImageQuery::create()->findPk( $image_id );
  50. }
  51. /**
  52. * Get images in an item table
  53. *
  54. * @param string $table
  55. * @param int $table_id
  56. *
  57. * @return Image object array
  58. */
  59. public function get_images( $table, $table_id ) {
  60. return ImageQuery::create()
  61. ->filterBy_table( $table )
  62. ->filterBy_table_id( $table_id )
  63. ->orderBy_order()
  64. ->find();
  65. }
  66. /**
  67. * Get maximum number of image order from a gallery
  68. *
  69. * @param int $gallery_id
  70. *
  71. * @return int
  72. */
  73. public function get_max_order( $table, $table_id ) {
  74. $con = Propel::getConnection();
  75. $sql = "SELECT MAX( i.order )
  76. FROM image i
  77. WHERE i.table = '{$table}'
  78. AND table_id = '{$table_id}'";
  79. $stmt = $con->prepare( $sql );
  80. $stmt->execute();
  81. $result = $stmt->fetchAll();
  82. return $result[0][0];
  83. }
  84. /**
  85. * Check whether the folder has old image
  86. *
  87. * @param string $table
  88. * @param int $table_id
  89. * @param string $file
  90. *
  91. * @return boolean
  92. */
  93. public function has_image( $table, $table_id, $file ) {
  94. $images = $this->get_images( $table, $table_id );
  95. foreach( $images as $image ) {
  96. if( $image->get_path() == $file ) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Add new image
  104. *
  105. * @param string $table
  106. * @param int $table_id
  107. * @param string $name
  108. * @param string $description
  109. * @param string $path
  110. * @param int $image_date
  111. *
  112. * @return Image object
  113. */
  114. public function add_image( $table, $table_id, $name, $description, $path, $image_date ) {
  115. $image = new Image();
  116. $image->set_table( $table );
  117. $image->set_table_id( $table_id );
  118. $image->set_name( $name );
  119. $image->set_description( $description );
  120. $image->set_path( $path );
  121. $image->set_image_date( $image_date );
  122. $image->set_order( $this->get_max_order( $table, $table_id ) +1 );
  123. $image->save();
  124. return $image;
  125. }
  126. /**
  127. * Update image
  128. *
  129. * @param int $image_id
  130. * @param string $table
  131. * @param int $table_id
  132. * @param string $name
  133. * @param string $description
  134. * @param string $path
  135. * @param int $image_date
  136. *
  137. * @return Image object
  138. */
  139. public function update_image( $image_id, $table, $table_id, $name, $description, $path, $image_date ) {
  140. $image = ImageQuery::create()->findPk( $image_id );
  141. $image->set_table( $table );
  142. $image->set_table_id( $table_id );
  143. $image->set_name( $name );
  144. $image->set_description( $description );
  145. $image->set_path( $path );
  146. $image->set_image_date( $image_date );
  147. $image->save();
  148. return $image;
  149. }
  150. /**
  151. * Update image info
  152. *
  153. * @param int $image_id
  154. * @param string $name
  155. * @param string $description
  156. *
  157. * @return Image object
  158. */
  159. public function update_image_info( $image_id, $name, $description ) {
  160. $image = ImageQuery::create()->findPk( $image_id );
  161. $image->set_name( $name );
  162. $image->set_description( $description );
  163. $image->save();
  164. return $image;
  165. }
  166. /**
  167. * Update image orders
  168. *
  169. * @param array $image
  170. * @param array $order
  171. *
  172. */
  173. public function update_orders( $image, $order ) {
  174. $min_order = min( $order );
  175. foreach( $image as $i => $image_id ) {
  176. $image = $this->get_image( $image_id );
  177. $image->set_order( $min_order +$i );
  178. $image->save();
  179. }
  180. }
  181. /**
  182. * Update image list in database for an item table
  183. *
  184. * @param string $table
  185. * @param int $table_id
  186. *
  187. */
  188. public function update_from_folder( $table, $table_id ) {
  189. $cap_name = ucfirst( $table );
  190. $cap_name = ( $cap_name == 'Gallery' )? $cap_name: $cap_name .'s';
  191. $path = APPPATH ."../userfiles/{$cap_name}/" .$table_id;
  192. if( !file_exists( $path ) ) {
  193. return;
  194. }
  195. $files = scandir( $path );
  196. $images = $this->get_images( $table, $table_id );
  197. // delete images from database if files do not exist
  198. foreach( $images as $image ) {
  199. if( !in_array( $image->get_path(), $files ) ) {
  200. $image->delete();
  201. }
  202. }
  203. // update image from folder
  204. foreach( $files as $file ) {
  205. if( $this->has_image( $table, $table_id, $file ) ) {
  206. }
  207. else {
  208. if( $file == '.' || $file == '..' ) {
  209. continue;
  210. }
  211. $this->add_image( $table, $table_id, '', '', $file, time() );
  212. }
  213. }
  214. }
  215. /**
  216. * Delete image
  217. *
  218. * @param int $image_id
  219. *
  220. */
  221. public function delete_image( $image_id ) {
  222. ImageQuery::create()->findPk( $image_id )->delete();
  223. }
  224. /**
  225. * Delete images from table items
  226. *
  227. * @param string $table
  228. * @param int $table_id
  229. *
  230. */
  231. public function delete_images( $table, $table_id ) {
  232. ImageQuery::create()
  233. ->filterBy_table( $table )
  234. ->filterBy_table_id( $table_id )
  235. ->delete();
  236. }
  237. }
  238. ?>