public function execute()
 {
     $data = $this->getReqiestData();
     $comment_id = $data['comment_id'];
     unset($data['comment_id']);
     $data['datetime'] = date('Y-m-d H:i:s');
     $contact_data = $this->getContactData();
     $data = array_merge($data, $contact_data);
     $this->errors += $this->comment_model->validate($data);
     if ($this->errors) {
         return false;
     }
     // taking into account possibility of stack
     $photo_id = $data['photo_id'];
     $photo_model = new photosPhotoModel();
     $parent_id = $photo_model->getStackParentId($photo_id);
     if ($parent_id) {
         // if it is stack work with parent
         $photo_id = $parent_id;
     }
     $data['photo_id'] = $photo_id;
     if (!isset($data['ip']) && ($ip = waRequest::getIp())) {
         $ip = ip2long($ip);
         if ($ip > 2147483647) {
             $ip -= 4294967296;
         }
         $data['ip'] = $ip;
     }
     $id = $this->comment_model->add($data, $comment_id);
     $this->added_comment = $this->comment_model->getById($id);
     if (preg_match('/(\\d+)/', $data['photo_comments_count_text'], $m)) {
         $count = $m[1] + 1;
         $this->response['photo_comments_count_text'] = _wp('%d comment', '%d comments', $count);
     }
     $comment = $data;
     $comment['id'] = $id;
     $comment['author'] = $this->getResponseAuthorData();
     $comment['status'] = photosCommentModel::STATUS_PUBLISHED;
     $photo_id = $comment['photo_id'];
     $this->view->assign('wrap_li', true);
     $this->view->assign('comment', $comment);
     $this->view->assign('contact_rights', wa()->getUser()->getRights('contacts', 'backend'));
     $this->response['html'] = $this->view->fetch($this->template);
 }
 public function execute()
 {
     $photo_id = waRequest::post('photo_id', array(), waRequest::TYPE_ARRAY_INT);
     $one_photo = waRequest::post('one_photo', 0, waRequest::TYPE_INT);
     $tags = waRequest::post('tags', '', waRequest::TYPE_STRING_TRIM);
     $tags = $tags ? explode(',', $tags) : array();
     $delete_tags = waRequest::post('delete_tags', array(), waRequest::TYPE_ARRAY_INT);
     $tag_model = new photosTagModel();
     $photo_tag_model = new photosPhotoTagsModel();
     $photo_rights_model = new photosPhotoRightsModel();
     $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($photo_id, true);
     $denied_photo_id = array_values(array_diff($photo_id, $allowed_photo_id));
     if ($allowed_photo_id) {
         if ($one_photo) {
             $allowed_photo_id = $allowed_photo_id[0];
             $photo_tag_model->set($allowed_photo_id, $tags);
             $photo_model = new photosPhotoModel();
             if ($parent_id = $photo_model->getStackParentId($allowed_photo_id)) {
                 $this->response['parent_id'] = $parent_id;
             }
         } else {
             if ($delete_tags) {
                 $photo_tag_model->delete($allowed_photo_id, $delete_tags);
             }
             $photo_tag_model->assign($allowed_photo_id, $tag_model->getIds($tags, true));
         }
         $allowed_photo_id = (array) $allowed_photo_id;
         $tags = $photo_tag_model->getTags($allowed_photo_id);
         if (!$tags && $allowed_photo_id) {
             $tags = array_fill_keys($allowed_photo_id, array());
         }
         $this->response['tags'] = $tags;
     }
     if ($denied_photo_id) {
         $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", count($denied_photo_id), _w("out of %d selected", "out of %d selected", count($photo_id))) . ', ' . _w("because you don't have sufficient access rights") . '.';
     }
     $this->response['cloud'] = $tag_model->getCloud();
 }
 public function frontendPhoto($photo)
 {
     $this->view = $this->view();
     $photo_model = new photosPhotoModel();
     $photo_id = $photo['id'];
     if ($parent_id = $photo_model->getStackParentId($photo)) {
         $photo_id = $parent_id;
     }
     $comments = $this->comment()->getFullTree($photo_id, array('author' => true));
     $storage = wa()->getStorage();
     $adapters = array();
     $current_auth = null;
     $current_auth_source = null;
     $user = wa()->getUser();
     if ($user->isAuth()) {
         $comment_author = photosCommentModel::getAuthorInfo($user->getId(), photosCommentModel::BIG_AUTHOR_PHOTO_SIZE);
         $storage->del('auth_user_data');
         $this->view->assign('comment_author', $comment_author);
     } else {
         $current_auth = $storage->read('auth_user_data');
         $adapters = wa()->getAuthAdapters();
         if (!$adapters && $current_auth) {
             $current_auth = null;
             $storage->del('auth_user_data');
         }
         $current_auth_source = $current_auth ? $current_auth['source'] : null;
         $this->view->assign('comment_author', null);
     }
     $app_url = wa()->getAppStaticUrl();
     $this->view->assign('current_auth_source', $current_auth_source);
     $this->view->assign('current_auth', $current_auth);
     $this->view->assign('auth_adapters', $adapters);
     $this->view->assign('require_authorization', $this->getSettings('require_authorization'));
     $this->view->assign('comments', $comments);
     $this->view->assign('photo_id', $photo_id);
     return array('bottom' => $this->view()->fetch($this->path . '/templates/FrontendPhoto.html'));
 }
 protected function workupPhotos(&$photos)
 {
     $renderer = new photosPhotoHtmlRenderer($this->getTheme());
     $photo_model = new photosPhotoModel();
     // parent of current photo (that stacked, i.e. in stack)
     $parent_id = null;
     if ($this->photo_url) {
         $stacked_photo = $photo_model->getByField('url', $this->photo_url);
         if (!$stacked_photo) {
             throw new waException(_w('Page not found', 404));
         }
         $parent_id = $photo_model->getStackParentId($stacked_photo);
     }
     // During going over all photos we also look if some photo is a parent of current stacked photo
     foreach ($photos as &$photo) {
         $photo['stack_nav'] = '';
         $stack = (array) $photo_model->getStack($photo['id'], array('tags' => true));
         if ($stack) {
             foreach ($stack as &$item) {
                 $item['thumb_custom'] = array('url' => photosPhoto::getPhotoUrlTemplate($item));
                 $item['full_url'] = photosFrontendAlbum::getLink($this->album) . $item['url'] . '/';
             }
             unset($item);
             // iterable photo is parent of current stacked photo - replace
             if ($parent_id == $photo['id']) {
                 $photo = $stacked_photo;
                 $photo['full_url'] = photosFrontendAlbum::getLink($this->album) . $photo['url'] . '/';
                 $photo['stack_nav'] = $renderer->getStackNavigationPanel($stack, $photo);
             } else {
                 $photo['stack_nav'] = $renderer->getStackNavigationPanel($stack, $photo);
             }
         }
         $photo['frontend_link'] = photosFrontendPhoto::getLink($photo, array('full_url' => $this->album_url));
     }
     unset($photo);
 }
 public function execute()
 {
     $name = waRequest::post('name', '', waRequest::TYPE_STRING_TRIM);
     if (in_array($name, $this->availableFields) === false) {
         throw new waException("Can't update photo: unknown field");
     }
     $photo_id = waRequest::post('id', null, waRequest::TYPE_ARRAY_INT);
     $value = waRequest::post('value', '', waRequest::TYPE_STRING_TRIM);
     if ($photo_id) {
         $photo_rights_model = new photosPhotoRightsModel();
         if (count($photo_id) == 1) {
             // editing only one photo
             if (!$photo_rights_model->checkRights(current($photo_id), true)) {
                 throw new waException(_w("You don't have sufficient access rights"));
             }
             // validations for one photo
             if ($name == 'url') {
                 if (!$this->validateUrl($value, current($photo_id))) {
                     // $photo_id is array of ids, so make current()
                     $this->errors['url'] = _w('URL is in use');
                     return;
                 }
             }
             $allowed_photo_id = $photo_id;
             $denied_photo_id = array();
         } else {
             $allowed_photo_id = $photo_rights_model->filterAllowedPhotoIds($photo_id, true);
             $denied_photo_id = array_diff($photo_id, $allowed_photo_id);
         }
         if ($allowed_photo_id) {
             if ($name == 'rate') {
                 $value = (int) $value;
                 if ($value < 0 || $value > 5) {
                     $value = 0;
                 }
             }
             $data[$name] = $value;
             $this->photo_model = new photosPhotoModel();
             if ($name == 'description' || $name == 'rate') {
                 $this->photo_model->update($allowed_photo_id, $data);
                 if (count($photo_id) == 1 && $allowed_photo_id) {
                     // means that we edit field in one-photo page
                     $photo_id = current($photo_id);
                     if ($parent_id = $this->photo_model->getStackParentId($photo_id)) {
                         $this->response['parent_id'] = $parent_id;
                     }
                 }
                 // change count of rated
                 if ($name == 'rate') {
                     $this->response['count'] = $this->photo_model->countRated();
                     $this->log('photos_rate', 1);
                 }
             } else {
                 // update only parent photo(s)
                 $this->photo_model->updateById($allowed_photo_id, $data);
             }
             if ($name == 'name') {
                 $this->response['value'] = photosPhoto::escape($value);
             }
         }
         if (count($denied_photo_id) > 0 && count($photo_id) > 0) {
             $this->response['alert_msg'] = photosPhoto::sprintf_wplural("The operation was not performed to %d photo (%%s)", "The operation was not performed to %d photos (%%s)", count($denied_photo_id), _w("out of %d selected", "out of %d selected", count($photo_id))) . ', ' . _w("because you don't have sufficient access rights") . '.';
         }
         $allowed_photo_id_map = array();
         foreach ($allowed_photo_id as $id) {
             $allowed_photo_id_map[$id] = true;
         }
         $this->response['allowed_photo_id'] = $allowed_photo_id_map;
     }
 }
 public function execute()
 {
     $id = waRequest::post('id', 0, waRequest::TYPE_INT);
     $in_stack = waRequest::post('in_stack', 0, waRequest::TYPE_INT);
     $hash = waRequest::post('hash', null, waRequest::TYPE_STRING_TRIM);
     $hash = urldecode($hash);
     // get photo
     $this->photo_model = new photosPhotoModel();
     $this->photo = $this->photo_model->getById($id);
     if (!$this->photo) {
         throw new waException(_w("Photo doesn't exists"), 404);
     }
     $photo_rights_model = new photosPhotoRightsModel();
     if (!$photo_rights_model->checkRights($this->photo)) {
         throw new waRightsException(_w("You don't have sufficient access rights"));
     }
     $this->photo['name_not_escaped'] = $this->photo['name'];
     $this->photo = photosPhoto::escapeFields($this->photo);
     $this->photo['upload_datetime_formatted'] = waDateTime::format('humandate', $this->photo['upload_datetime']);
     $this->photo['upload_timestamp'] = strtotime($this->photo['upload_datetime']);
     $this->photo['edit_rights'] = $photo_rights_model->checkRights($this->photo, true);
     $this->photo['private_url'] = photosPhotoModel::getPrivateUrl($this->photo);
     $this->photo['thumb'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getThumbPhotoSize());
     $this->photo['thumb_big'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getBigPhotoSize());
     $this->photo['thumb_middle'] = photosPhoto::getThumbInfo($this->photo, photosPhoto::getMiddlePhotoSize());
     $original_photo_path = photosPhoto::getOriginalPhotoPath($this->photo);
     if (wa('photos')->getConfig()->getOption('save_original') && file_exists($original_photo_path)) {
         $this->photo['original_exists'] = true;
     } else {
         $this->photo['original_exists'] = false;
     }
     $photo_tags_model = new photosPhotoTagsModel();
     $tags = $photo_tags_model->getTags($id);
     $this->photo['tags'] = $tags;
     $this->response['photo'] = $this->photo;
     // get stack if it's possible
     if (!$in_stack && ($stack = $this->photo_model->getStack($id, array('thumb' => true, 'thumb_crop' => true, 'thumb_big' => true, 'thumb_middle' => true)))) {
         $this->response['stack'] = $stack;
     }
     // get albums
     $album_photos_model = new photosAlbumPhotosModel();
     $albums = $album_photos_model->getAlbums($id, array('id', 'name'));
     $this->response['albums'] = isset($albums[$id]) ? array_values($albums[$id]) : array();
     // exif info
     $exif_model = new photosPhotoExifModel();
     $exif = $exif_model->getByPhoto($this->photo['id']);
     if (isset($exif['DateTimeOriginal'])) {
         $exif['DateTimeOriginal'] = waDateTime::format('humandatetime', $exif['DateTimeOriginal'], date_default_timezone_get());
     }
     $this->response['exif'] = $exif;
     // get author
     $contact = new waContact($this->photo['contact_id']);
     $this->response['author'] = array('id' => $contact['id'], 'name' => photosPhoto::escape($contact['name']), 'photo_url' => $contact->getPhoto(photosPhoto::AUTHOR_PHOTO_SIZE), 'backend_url' => $this->getConfig()->getBackendUrl(true) . 'contacts/#/contact/' . $contact['id']);
     // for making inline-editable widget
     $this->response['frontend_link_template'] = photosFrontendPhoto::getLink(array('url' => '%url%'));
     $hooks = array();
     $parent_id = $this->photo_model->getStackParentId($this->photo);
     $photo_id = $parent_id ? $parent_id : $id;
     /**
      * Extend photo page
      * Add extra widget(s)
      * @event backend_photo
      * @return array[string][string]string $return[%plugin_id%]['bottom'] In bottom, under photo any widget
      */
     $hooks['backend_photo'] = wa()->event('backend_photo', $photo_id);
     $this->response['hooks'] = $hooks;
     if ($hash !== null) {
         $collection = new photosCollection($hash);
         if (strstr($hash, 'rate>0') !== false) {
             $collection->orderBy('p.rate DESC, p.id');
         }
         $this->response['photo_stream'] = $this->getPhotoStream($collection);
         if ($collection->getAlbum()) {
             $this->response['album'] = $collection->getAlbum();
         }
     }
 }