/Gallery/Model/GalleryImage.php

https://bitbucket.org/brainbox/shared · PHP · 347 lines · 140 code · 81 blank · 126 comment · 5 complexity · 058b2250314d8be23a6196629ddfe6b9 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. *
  5. *
  6. *
  7. */
  8. Class Model_GalleryImage{
  9. protected $dbPDO;
  10. public $isActive = true; // Return published items ONLY by default
  11. public $id;
  12. function __construct($dbPDO){
  13. $this->dbPDO = $dbPDO;
  14. }
  15. /**
  16. * Get list of Committee Members
  17. *
  18. * @param $type
  19. * @return array
  20. *
  21. */
  22. function getByGalleryId($galleryId){
  23. $sql = "SELECT
  24. id
  25. , is_active
  26. , filename
  27. , caption
  28. , width
  29. , height
  30. , height
  31. , mime
  32. FROM
  33. gall_image
  34. WHERE
  35. 1=1";
  36. if($this->isActive){
  37. $sql .= " AND is_active = '1' ";
  38. }
  39. $sql .= " AND gallery_id='$galleryId'";
  40. // $sql .= " ORDER BY
  41. // is_active DESC
  42. // , sort_order ASC";
  43. //echo $sql;
  44. $stmnt=$this->dbPDO->prepare($sql);
  45. try{
  46. $stmnt->execute();
  47. return $stmnt->fetchALL(PDO::FETCH_ASSOC);
  48. }
  49. catch(Exception $e){
  50. echo 'Caught exception: ', $e->getMessage(), "\n";
  51. }
  52. }
  53. /**
  54. * Get individual image
  55. *
  56. * @param $type
  57. * @return array
  58. *
  59. */
  60. function getById($id){
  61. $sql = "SELECT
  62. id
  63. , is_active
  64. , filename
  65. , caption
  66. , width
  67. , height
  68. , height
  69. , mime
  70. FROM
  71. gall_image
  72. WHERE
  73. 1=1";
  74. if($this->isActive){
  75. $sql .= " AND is_active = '1' ";
  76. }
  77. $sql .= " AND id=$id";
  78. //echo $sql;
  79. $stmnt=$this->dbPDO->prepare($sql);
  80. try{
  81. $stmnt->execute();
  82. return $stmnt->fetch(PDO::FETCH_ASSOC);
  83. }
  84. catch(Exception $e){
  85. echo 'Caught exception: ', $e->getMessage(), "\n";
  86. }
  87. }
  88. /**
  89. * Get individual image
  90. *
  91. * @param $type
  92. * @return array
  93. *
  94. */
  95. function getByFilename($filename){
  96. $sql = "SELECT
  97. id
  98. , is_active
  99. , filename
  100. , caption
  101. , width
  102. , height
  103. , height
  104. , mime
  105. FROM
  106. gall_image
  107. WHERE
  108. 1=1";
  109. if($this->isActive){
  110. $sql .= " AND is_active = '1' ";
  111. }
  112. $sql .= " AND filename=$filename";
  113. //echo $sql;
  114. $stmnt=$this->dbPDO->prepare($sql);
  115. try{
  116. $stmnt->execute();
  117. return $stmnt->fetch(PDO::FETCH_ASSOC);
  118. }
  119. catch(Exception $e){
  120. echo 'Caught exception: ', $e->getMessage(), "\n";
  121. }
  122. }
  123. /**
  124. * Get individual image
  125. *
  126. * @param $type
  127. * @return array
  128. *
  129. */
  130. function getRandomImage($gallery){
  131. $sql = "SELECT
  132. id
  133. , is_active
  134. , filename
  135. , caption
  136. , width
  137. , height
  138. , height
  139. , mime
  140. FROM
  141. gall_image
  142. WHERE
  143. 1=1";
  144. if($this->isActive){
  145. $sql .= " AND is_active = '1' ";
  146. }
  147. $sql .= " AND gallery_id=" . $gallery->id ."
  148. ORDER BY RAND()
  149. LIMIT 1;" ;
  150. //echo $sql;
  151. $stmnt=$this->dbPDO->prepare($sql);
  152. try{
  153. $stmnt->execute();
  154. return $stmnt->fetch(PDO::FETCH_ASSOC);
  155. }
  156. catch(Exception $e){
  157. echo 'Caught exception: ', $e->getMessage(), "\n";
  158. }
  159. }
  160. function update(){
  161. /*
  162. //print_r($_POST);
  163. //Grab the id - needed for photo uploads
  164. $this->id = $_POST['id'];
  165. $isActive = '0';
  166. if(isset($_POST['isActive']) ){
  167. $isActive = '1';
  168. }
  169. $sql = "UPDATE committee
  170. SET
  171. name = :name,
  172. position = :position,
  173. is_active = :is_active,
  174. sort_order =:sort_order,
  175. biog = :biog
  176. WHERE id = :id ";
  177. $stmnt=$this->dbPDO->prepare($sql);
  178. $stmnt->bindParam(':name', $_POST['name']);
  179. $stmnt->bindParam(':position', $_POST['position']);
  180. $stmnt->bindParam(':is_active', $isActive);
  181. $stmnt->bindParam(':sort_order',$_POST['sortOrder']);
  182. $stmnt->bindParam(':biog', $_POST['biog']);
  183. $stmnt->bindParam(':id', $_POST['id']);
  184. try{
  185. $stmnt->execute();
  186. return true;
  187. }
  188. catch(Exception $e){
  189. echo 'Caught exception: ', $e->getMessage(), "\n";
  190. return false;
  191. }
  192. */
  193. }
  194. function updatePhoto(){
  195. /*
  196. ## HANDLE IMAGE ##
  197. if($_FILES['image']['type'] ){
  198. move_uploaded_file(
  199. $_FILES['image']['tmp_name'],
  200. '../images/committee/' . $this->id . '.jpg'
  201. );
  202. require_once('Images/Thumbnail.php');
  203. $tn=new ThumbNail( self::thumbWidth, self::thumbHeight);
  204. // Load an image into a string (this could be from a database)
  205. $image=file_get_contents('../images/committee/' . $this->id . '.jpg');
  206. // Load the image data
  207. $tn->loadData($image,'image/jpeg');
  208. // Build the thumbnail and store as a file
  209. $tn->buildThumb('../images/committee/thumb_' . $this->id . '.jpg');
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. */
  215. }
  216. /**
  217. * Want this to return gallery id
  218. *
  219. *
  220. */
  221. function add($galleryId, $filename){
  222. $this->isActive = false;
  223. if($result = $this->getByFilename($filename) ){
  224. return false;
  225. }
  226. $sql = "INSERT INTO gall_image
  227. (gallery_id, filename)
  228. VALUES ($galleryId,'$filename')";
  229. //echo $sql;
  230. //exit;
  231. $stmnt=$this->dbPDO->prepare($sql);
  232. try{
  233. $stmnt->execute();
  234. //print_r( $this->dbPDO->errorInfo() );
  235. return $this->dbPDO->lastInsertId();
  236. }
  237. catch(Exception $e){
  238. echo 'Caught exception: ', $e->getMessage(), "\n";
  239. }
  240. }
  241. }