Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Uploadpin;
- class CreatePinController extends \Core\Base\Action {
- /**
- * @var null|array
- */
- public $errors;
- public function init() {
- $request = $this->getRequest();
- if($request->isXmlHttpRequest()) {
- $this->noLayout(true);
- }
- set_time_limit(0);
- $this->_ = new \Translate\Locale('Front\\'.__NAMESPACE__, $this->getModule('Language')->getLanguageId());
- }
- public function indexAction() {
- $request = $this->getRequest();
- $data = array(
- 'location' => false
- );
- $userInfo = \User\User::getUserData();
- if($userInfo->id) {
- $pinTable = new \Pin\Pin();
- $this->x_form_cmd = $pinTable->getXFormCmd();
- $data['pin_media'] = $request->getRequest('media');
- if( $this->validate() ) {
- //create pin
- $sourceTable = new \Source\Source();
- $new = $pinTable->fetchNew();
- $new->board_id = $request->getRequest('board_id');
- $new->user_id = $userInfo->id;
- $new->pinned_from = 'Uploaded';
- $new->description = $this->escape($request->getRequest('description'));
- $pinTable->getAdapter()->beginTransaction();
- $image = \Base\Config::getUploadMethod('pinthumbs');
- try {
- $pin_id = $new->save();
- if($pin_id) {
- //upload image
- $image_path = '/pins' . \Core\Date::getInstance($new->date_added, '/yy/mm/', true);
- if( is_array($image_info = $image->upload($data['pin_media'], $image_path)) ) {
- $new->image = $image_info['file'];
- $new->width = $image_info['width'];
- $new->height = $image_info['height'];
- $new->store = $image_info['store'];
- $new->store_host = $image_info['store_host'];
- $new->save();
- } else {
- $this->errors['uploadError'] = $image->getError();
- }
- }
- if($this->errors) {
- $image->delete();
- $pinTable->getAdapter()->rollBack();
- } else {
- $pinTable->getAdapter()->commit();
- $url = $this->url(array('pin_id'=>$new->id),'pin');
- @unlink(BASE_PATH . DIRECTORY_SEPARATOR . $data['pin_media']);
- ////////////////// send notification shared board
- $boardTable = new \Board\Board();
- $board_info = $boardTable->getWithShared(\User\User::getUserData()->id,$new->board_id);
- if($board_info->count() && $board_info->offsetGet(0)->user_id != \User\User::getUserData()->id) {
- $userTable = new \User\User();
- $user_info = $userTable->fetchRow($userTable->makeWhere(array('id'=>$board_info->offsetGet(0)->user_id)));
- if($user_info && $user_info->notification_group_board) {
- $self_data = \User\User::getUserData();
- $NotificationTable = new \Notification\Notification();
- $Notification = $NotificationTable->setLanguageId($user_info->language_id)->setReplace(array(
- 'user_id' => $user_info->id,
- 'user_firstname' => $user_info->firstname,
- 'user_lastname' => $user_info->lastname,
- 'user_username' => $user_info->username,
- 'user_fullname' => $user_info->getUserFullname(),
- 'board_url' => $this->url(array('board_id'=>$new->board_id,'query'=>$this->urlQuery($board_info->offsetGet(0)->title)),'board'),
- 'board_name'=> $board_info->offsetGet(0)->title,
- 'pin_url' => $this->url(array('pin_id'=>$new->id),'pin'),
- 'author_url' => $this->url(array('user_id'=>$self_data->id,'query'=>$self_data->username),'user'),
- 'author_fullname' => $self_data->getUserFullname()
- ))->get('group_board');
- if($Notification) {
- $email = new \Helper\Email();
- $email->addFrom(\Base\Config::get('no_reply'));
- $email->addTo($user_info->email, $user_info->getUserFullname());
- $email->addTitle($Notification->title);
- $email->addHtml($Notification->description);
- $email->send();
- }
- //add activity
- \Activity\Activity::set($user_info->id, 'ADDPIN',$new->id,$new->board_id);
- }
- }
- //////////////////////////////
- if($request->isXmlHttpRequest()) {
- $data['location'] = $url;
- } else {
- $this->redirect($url);
- }
- }
- } catch (\Core\Exception $e) {
- $pinTable->getAdapter()->rollBack();
- $image->delete();
- $this->errors['saveData'] = $e->getMessage();
- }
- }
- if($this->errors) {
- $data['errors'] = $this->errors;
- }
- } else {
- $data['location'] = $this->url(array('controller' => 'login'),'user_c');
- }
- $this->responseJsonCallback($data);
- }
- private function validate() {
- $request = $this->getRequest();
- if($request->isPost()) {
- if( $request->getPost('X-form-cmd') == $this->x_form_cmd ) {
- $validator = new \Core\Form\Validator(array(
- 'translate' => $this->_
- ));
- $validator->addNumber('board_id', array(
- 'min' => 0,
- 'error_text' => $this->_('You have to choose which board to pin to')
- ));
- $validator->addText('description', array(
- 'min' => 3,
- 'error_text_min' => $this->_('Description must contain no less than %d characters')
- ));
- if($validator->validate()) {
- $boardTable = new \Board\Board();
- if(!$boardTable->getWithShared(\User\User::getUserData()->id,$request->getPost('board_id'))->count()) {
- $this->errors['board_id'] = $this->_('You do not have permission to pin in this board');
- }
- } else {
- $this->errors = $validator->getErrors();
- }
- if(!$this->errors) {
- $checkImageMedia = new \Core\Image\Getimagesize($request->getPost('media'));
- if( !is_array($info = $checkImageMedia->getSize()) ) {
- $this->errors['media'] = $this->_('Invalid image type!');
- } else {
- $allowed = $this->getAllowedTypes();
- if( !array_key_exists($info['mime'], $allowed) ) {
- $this->errors['media'] = $this->_('Image type is invalid!');
- } else {
- $config_image_minimum_size = (int)\Base\Config::get('config_image_minimum_size');
- if(!$config_image_minimum_size) { $config_image_minimum_size = 80; }
- if( min($info[0],$info[1]) >= $config_image_minimum_size ) {
- } else {
- $this->errors['media'] = sprintf($this->_('Photo size must be larger width and height of %s px'), $config_image_minimum_size);
- }
- }
- }
- }
- } else {
- $this->errors['x-form-cmd'] = $this->_('Incorrect form data');
- }
- return $this->errors ? false : true;
- }
- return false;
- }
- private function getAllowedTypes() {
- $allowedUploadMimes = \Core\Registry::get('allowedmimeimages');
- if(is_array($allowedUploadMimes)) {
- $tmp = array();
- foreach($allowedUploadMimes AS $mime) {
- $part = explode('/', $mime);
- $tmp[$mime] = '.' . $part[1];
- }
- return $tmp;
- }
- return array();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement