Advertisement
Guest User

CreatePinController

a guest
Jun 4th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Uploadpin;
  4.  
  5. class CreatePinController extends \Core\Base\Action {
  6.  
  7. /**
  8. * @var null|array
  9. */
  10. public $errors;
  11.  
  12. public function init() {
  13. $request = $this->getRequest();
  14. if($request->isXmlHttpRequest()) {
  15. $this->noLayout(true);
  16. }
  17. set_time_limit(0);
  18. $this->_ = new \Translate\Locale('Front\\'.__NAMESPACE__, $this->getModule('Language')->getLanguageId());
  19. }
  20.  
  21. public function indexAction() {
  22. $request = $this->getRequest();
  23.  
  24. $data = array(
  25. 'location' => false
  26. );
  27. $userInfo = \User\User::getUserData();
  28.  
  29. if($userInfo->id) {
  30.  
  31. $pinTable = new \Pin\Pin();
  32.  
  33. $this->x_form_cmd = $pinTable->getXFormCmd();
  34.  
  35. $data['pin_media'] = $request->getRequest('media');
  36.  
  37. if( $this->validate() ) {
  38. //create pin
  39. $sourceTable = new \Source\Source();
  40. $new = $pinTable->fetchNew();
  41. $new->board_id = $request->getRequest('board_id');
  42. $new->user_id = $userInfo->id;
  43. $new->pinned_from = 'Uploaded';
  44. $new->description = $this->escape($request->getRequest('description'));
  45.  
  46. $pinTable->getAdapter()->beginTransaction();
  47. $image = \Base\Config::getUploadMethod('pinthumbs');
  48. try {
  49. $pin_id = $new->save();
  50. if($pin_id) {
  51. //upload image
  52. $image_path = '/pins' . \Core\Date::getInstance($new->date_added, '/yy/mm/', true);
  53. if( is_array($image_info = $image->upload($data['pin_media'], $image_path)) ) {
  54. $new->image = $image_info['file'];
  55. $new->width = $image_info['width'];
  56. $new->height = $image_info['height'];
  57. $new->store = $image_info['store'];
  58. $new->store_host = $image_info['store_host'];
  59. $new->save();
  60. } else {
  61. $this->errors['uploadError'] = $image->getError();
  62. }
  63. }
  64. if($this->errors) {
  65. $image->delete();
  66. $pinTable->getAdapter()->rollBack();
  67. } else {
  68. $pinTable->getAdapter()->commit();
  69. $url = $this->url(array('pin_id'=>$new->id),'pin');
  70. @unlink(BASE_PATH . DIRECTORY_SEPARATOR . $data['pin_media']);
  71. ////////////////// send notification shared board
  72. $boardTable = new \Board\Board();
  73. $board_info = $boardTable->getWithShared(\User\User::getUserData()->id,$new->board_id);
  74. if($board_info->count() && $board_info->offsetGet(0)->user_id != \User\User::getUserData()->id) {
  75. $userTable = new \User\User();
  76. $user_info = $userTable->fetchRow($userTable->makeWhere(array('id'=>$board_info->offsetGet(0)->user_id)));
  77. if($user_info && $user_info->notification_group_board) {
  78. $self_data = \User\User::getUserData();
  79. $NotificationTable = new \Notification\Notification();
  80. $Notification = $NotificationTable->setLanguageId($user_info->language_id)->setReplace(array(
  81. 'user_id' => $user_info->id,
  82. 'user_firstname' => $user_info->firstname,
  83. 'user_lastname' => $user_info->lastname,
  84. 'user_username' => $user_info->username,
  85. 'user_fullname' => $user_info->getUserFullname(),
  86. 'board_url' => $this->url(array('board_id'=>$new->board_id,'query'=>$this->urlQuery($board_info->offsetGet(0)->title)),'board'),
  87. 'board_name'=> $board_info->offsetGet(0)->title,
  88. 'pin_url' => $this->url(array('pin_id'=>$new->id),'pin'),
  89. 'author_url' => $this->url(array('user_id'=>$self_data->id,'query'=>$self_data->username),'user'),
  90. 'author_fullname' => $self_data->getUserFullname()
  91. ))->get('group_board');
  92. if($Notification) {
  93. $email = new \Helper\Email();
  94. $email->addFrom(\Base\Config::get('no_reply'));
  95. $email->addTo($user_info->email, $user_info->getUserFullname());
  96. $email->addTitle($Notification->title);
  97. $email->addHtml($Notification->description);
  98. $email->send();
  99. }
  100. //add activity
  101. \Activity\Activity::set($user_info->id, 'ADDPIN',$new->id,$new->board_id);
  102. }
  103. }
  104. //////////////////////////////
  105. if($request->isXmlHttpRequest()) {
  106. $data['location'] = $url;
  107. } else {
  108. $this->redirect($url);
  109. }
  110. }
  111. } catch (\Core\Exception $e) {
  112. $pinTable->getAdapter()->rollBack();
  113. $image->delete();
  114. $this->errors['saveData'] = $e->getMessage();
  115. }
  116.  
  117. }
  118. if($this->errors) {
  119. $data['errors'] = $this->errors;
  120. }
  121.  
  122. } else {
  123. $data['location'] = $this->url(array('controller' => 'login'),'user_c');
  124. }
  125.  
  126. $this->responseJsonCallback($data);
  127.  
  128. }
  129.  
  130. private function validate() {
  131. $request = $this->getRequest();
  132. if($request->isPost()) {
  133. if( $request->getPost('X-form-cmd') == $this->x_form_cmd ) {
  134. $validator = new \Core\Form\Validator(array(
  135. 'translate' => $this->_
  136. ));
  137. $validator->addNumber('board_id', array(
  138. 'min' => 0,
  139. 'error_text' => $this->_('You have to choose which board to pin to')
  140. ));
  141. $validator->addText('description', array(
  142. 'min' => 3,
  143. 'error_text_min' => $this->_('Description must contain no less than %d characters')
  144. ));
  145. if($validator->validate()) {
  146. $boardTable = new \Board\Board();
  147. if(!$boardTable->getWithShared(\User\User::getUserData()->id,$request->getPost('board_id'))->count()) {
  148. $this->errors['board_id'] = $this->_('You do not have permission to pin in this board');
  149. }
  150. } else {
  151. $this->errors = $validator->getErrors();
  152. }
  153. if(!$this->errors) {
  154. $checkImageMedia = new \Core\Image\Getimagesize($request->getPost('media'));
  155. if( !is_array($info = $checkImageMedia->getSize()) ) {
  156. $this->errors['media'] = $this->_('Invalid image type!');
  157. } else {
  158. $allowed = $this->getAllowedTypes();
  159. if( !array_key_exists($info['mime'], $allowed) ) {
  160. $this->errors['media'] = $this->_('Image type is invalid!');
  161. } else {
  162. $config_image_minimum_size = (int)\Base\Config::get('config_image_minimum_size');
  163. if(!$config_image_minimum_size) { $config_image_minimum_size = 80; }
  164. if( min($info[0],$info[1]) >= $config_image_minimum_size ) {
  165.  
  166. } else {
  167. $this->errors['media'] = sprintf($this->_('Photo size must be larger width and height of %s px'), $config_image_minimum_size);
  168. }
  169. }
  170. }
  171. }
  172. } else {
  173. $this->errors['x-form-cmd'] = $this->_('Incorrect form data');
  174. }
  175. return $this->errors ? false : true;
  176. }
  177. return false;
  178. }
  179.  
  180. private function getAllowedTypes() {
  181. $allowedUploadMimes = \Core\Registry::get('allowedmimeimages');
  182. if(is_array($allowedUploadMimes)) {
  183. $tmp = array();
  184. foreach($allowedUploadMimes AS $mime) {
  185. $part = explode('/', $mime);
  186. $tmp[$mime] = '.' . $part[1];
  187. }
  188. return $tmp;
  189. }
  190. return array();
  191. }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement