/showcase/classes/Admin/Content.php

https://github.com/roycocup/Tests · PHP · 304 lines · 225 code · 60 blank · 19 comment · 43 complexity · f7202f5a8764656a522866f4b324e8d1 MD5 · raw file

  1. <?php
  2. class Showcase_Admin_Content extends Showcase_Controller_Action_Admin
  3. {
  4. protected $_jobId;
  5. protected $_client;
  6. protected $_article;
  7. protected $_image;
  8. protected $_status;
  9. protected $_published;
  10. protected $_uploadPathImages;
  11. protected $_dbImagePath;
  12. protected $_operationType;
  13. protected $_contentId;
  14. protected $_oldImage;
  15. protected $_post; // the original post to return to view
  16. public function __construct($content = null)
  17. {
  18. $this->_post = $content;
  19. (isset($content['contentId']))? $this->_contentId = $content['contentId'] : NULL;
  20. (isset($content['operationType']))? $this->_operationType = $content['operationType'] : NULL;
  21. (isset($content['client']))? $this->_client = $content['client'] : NULL;
  22. (isset($content['jobId']))? $this->_jobId = $content['jobId'] : NULL;
  23. (isset($content['title']))? $this->_article['title'] = $content['title'] : NULL;
  24. (isset($content['description']))? $this->_article['description'] = $content['description'] : NULL;
  25. (isset($content['image']))? $this->_image = $content['image'] : NULL;
  26. (isset($content['datePublished']))? $this->_published = $content['datePublished'] : NULL;
  27. (isset($content['status']))? $this->_status = $content['status'] : NULL;
  28. (isset($content['client']))? $this->_uploadPathImages = Package::buildPath(DOC_DIR, 'document_root', 'images', 'clients', strtolower($this->_client['name']), 'campaigns') : NULL;
  29. (isset($content['client']))? $this->_dbImagePath = "/".Package::buildPath('images','clients', strtolower($this->_client['name']),'campaigns')."/" : NULL;
  30. (isset($content['oldImageName']))? $this->_oldImage = array('name'=>$content['oldImageName'], 'fullPath'=> $this->_uploadPathImages.DIRECTORY_SEPARATOR.$content['oldImageName']) : NULL;
  31. }
  32. public function __call($method, $args)
  33. {
  34. $method = '_' . $method;
  35. if (method_exists($this, $method)) {
  36. return call_user_func_array(array($this, $method), $args);
  37. }
  38. }
  39. public function __set ($v, $val)
  40. {
  41. $var = "_" . $v;
  42. if (property_exists($this, $var)){
  43. $this->$var = $val;
  44. }
  45. return false;
  46. }
  47. public function __get($v)
  48. {
  49. $var = "_" . $v;
  50. if (property_exists($this, $var)){
  51. return $this->$var;
  52. }
  53. return false;
  54. }
  55. public function processContent()
  56. {
  57. //validate
  58. $validation = $this->validate();
  59. if ($validation !== true)
  60. {
  61. $validation['post'] = $this->_post;
  62. return $validation;
  63. }
  64. //convert date into zend date object
  65. $this->_published = new Zend_Date($this->_published);
  66. //upload
  67. if ($this->_image['name'])
  68. {
  69. //upload the file
  70. $upload = $this->_uploadFile();
  71. //if its uploaded, lets puts some info next to the file
  72. $this->_image['fullPath'] = $this->_uploadPathImages.DIRECTORY_SEPARATOR.$this->_image['name'];
  73. $this->_image['path'] = $this->_dbImagePath.$this->_image['name'];
  74. }
  75. if (isset($upload['Error']))
  76. {
  77. $upload['post'] = $this->_post;
  78. return $upload;
  79. }
  80. //save db
  81. if ($this->_operationType == 'save')
  82. {
  83. $result = $this->_saveContent();
  84. }
  85. //update db
  86. if ($this->_operationType == 'update')
  87. {
  88. $result = $this->_updateContent();
  89. }
  90. if (isset($result) && $result == false)
  91. {
  92. //delte the file beeing uploaded
  93. $this->_deleteFile($this->_image);
  94. $result['post'] = $this->_post;
  95. $result['Error'][] = 'There was a problem saving to the database. Please try again later.';
  96. return $result;
  97. } else {
  98. //delete the old file
  99. if (isset($upload) && isset($this->_oldImage)){$this->_deleteFile($this->_oldImage);}
  100. return true;
  101. }
  102. }
  103. private function _saveContent()
  104. {
  105. //save data to db
  106. $oContent = new Showcase_Controller_Action_Helper_Admin_Content;
  107. $content = array(
  108. 'jobId' => $this->_jobId,
  109. 'client' => $this->_client,
  110. 'article' => $this->_article,
  111. 'image' => $this->_image,
  112. 'status' => $this->_status,
  113. 'published' => $this->_published
  114. );
  115. $return = $oContent->saveNewContent($content);
  116. return $return;
  117. }
  118. private function _updateContent()
  119. {
  120. //save data to db
  121. $oContent = new Showcase_Controller_Action_Helper_Admin_Content;
  122. $content = array(
  123. 'contentId' => $this->_contentId,
  124. 'article' => $this->_article,
  125. 'image' => $this->_image,
  126. 'status' => $this->_status,
  127. 'published' => $this->_published
  128. );
  129. $return = $oContent->updateContent($content);
  130. return $return;
  131. }
  132. private function _deleteContent()
  133. {
  134. //delete the db
  135. $oDelete = new Showcase_Controller_Action_Helper_Admin_Content;
  136. $return = $oDelete->deleteContent($this->_contentId);
  137. if ($return !== true)
  138. {
  139. $return['Error'][] = array('There is a problem deleting this phase. Please try again later.');
  140. }else{
  141. //delete the file
  142. $this->_deleteFile($this->_oldImage);
  143. return true;
  144. }
  145. return $return;
  146. }
  147. public function validate()
  148. {
  149. $validation = array();
  150. //instatiate a new validator
  151. $validator = new Showcase_Admin_Validator();
  152. //title validation
  153. if (empty($this->_article['title']))
  154. {
  155. $validation['Error'][] = 'Please fill in a title.';
  156. } else {
  157. if ($text = $validator->isValidText($this->_article['title'], 150))
  158. {
  159. if(is_array($text))
  160. {
  161. if (isset($text['Error']))
  162. {
  163. $validation['Error'][] = $text['Error'];
  164. }
  165. }
  166. }
  167. }
  168. //description validation
  169. if (empty($this->_article['description']))
  170. {
  171. $validation['Error'][] = 'Please fill in a description.';
  172. } else {
  173. if ($description = $validator->isValidText($this->_article['description'], 1000))
  174. {
  175. if(is_array($description ))
  176. {
  177. if (isset($description['Error']))
  178. {
  179. $validation['Error'][] = $description['Error'];
  180. }
  181. }
  182. }
  183. }
  184. //image validation
  185. if (isset($this->_image))
  186. {
  187. if ($this->_image['error'] != 0 && $this->_image['error']!= 4)
  188. {
  189. $validation['Error'][] = array('Error'=>'Corrupt image. Please upload again.');
  190. } else {
  191. if ($this->_image['error']!= 4 && $imageValidation = $validator->isValidImage($this->_image, '500000', '200', '250', array('image/jpg', 'image/gif') ))
  192. {
  193. if (isset($imageValidation['Error']))
  194. {
  195. $validation['Error'][] = $imageValidation['Error'];
  196. }
  197. }
  198. }
  199. }
  200. //date validation
  201. if (empty($this->_published))
  202. {
  203. $validation['Error'][] = 'Please insert a date.';
  204. } else {
  205. if ($dateValidation = $validator->isValidDate($this->_published, 'dd/mm/YYYY'))
  206. {
  207. if(is_array($dateValidation))
  208. {
  209. if (isset($dateValidation['Error']))
  210. {
  211. $validation['Error'][] = $dateValidation['Error'];
  212. }
  213. }
  214. }
  215. }
  216. if (!empty($validation))
  217. {
  218. return $validation;
  219. }else {
  220. return true;
  221. }
  222. }
  223. private function _uploadFile()
  224. {
  225. //upload files object
  226. $upload = new Showcase_Admin_Uploader();
  227. $return = array();
  228. $upload->setUploadPath($this->_uploadPathImages);
  229. $upload->setFile($this->_image);
  230. $r = $upload->execute();
  231. if ($r !== true)
  232. {
  233. $return['Error'][] = $r['Error'];
  234. }
  235. return $return;
  236. }
  237. private function _deleteFile($file)
  238. {
  239. if(file_exists($file['fullPath']))
  240. {
  241. @unlink($file['fullPath']);
  242. }
  243. }
  244. }