/** * Get image comments * * @param Model_User $viewer * @return Database_Result */ public function comments(Model_User $viewer = null) { $query = Model_Comment::query_viewer(DB::select_array(Model_Image_Comment::factory()->fields()), $viewer); return $this->find_related('image_comments', $query); }
/** * Action: image */ public function action_image() { $gallery_id = (int) $this->request->param('gallery_id'); $image_id = $this->request->param('id'); /** @var Model_Gallery $gallery */ $gallery = Model_Gallery::factory($gallery_id); if (!$gallery->loaded()) { throw new Model_Exception($gallery, $gallery_id); } // Are we approving pending images? if ($this->request->action() == 'approve') { // Can we see galleries with un-approved images? Permission::required($gallery, Model_Gallery::PERMISSION_APPROVE_WAITING, self::$user); // Can we see all of them and approve? $approve = Permission::has($gallery, Model_Gallery::PERMISSION_APPROVE, self::$user); $images = $gallery->find_images_pending($approve ? null : self::$user); } else { Permission::required($gallery, Model_Gallery::PERMISSION_READ, self::$user); $images = $gallery->images(); } // Find current, previous and next images $i = 0; /** @var Model_Image $next */ /** @var Model_Image $previous */ /** @var Model_Image $current */ $previous = $next = $current = null; foreach ($images as $image) { $i++; if (!is_null($current)) { // Current was found last round $next = $image; $i--; break; } else { if ($image->id == $image_id) { // Current found now $current = $image; // Fix state to loaded to perform update instead of insert when saving $current->state(AutoModeler::STATE_LOADED); } else { // No current found $previous = $image; } } } // Show image if (!is_null($current)) { // Comments section if (!isset($approve) && Permission::has($gallery, Model_Gallery::PERMISSION_COMMENTS, self::$user)) { $errors = array(); $values = array(); // Handle comment if (Permission::has($gallery, Model_Gallery::PERMISSION_COMMENT, self::$user) && $_POST) { try { $comment = Model_Image_Comment::factory()->add(self::$user->id, $current, Arr::get($_POST, 'comment'), Arr::get($_POST, 'private')); $current->comment_count++; if ($current->author_id != self::$user->id) { $current->new_comment_count++; } $current->save(); $gallery->comment_count++; $gallery->save(); if (!$comment->private) { // Noted users foreach ($current->notes() as $note) { $note->state(AutoModeler::STATE_LOADED); $note->new_comment_count++; $note->save(); } // Newsfeed NewsfeedItem_Galleries::comment(self::$user, $gallery, $current); } // Redirect back to image if not ajax if ($this->_request_type !== Controller::REQUEST_AJAX) { $this->request->redirect(Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => ''))); return; } } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); $values = $comment; } } else { if (self::$user) { // Clear new comment count? if ($current->author_id == self::$user->id && $current->new_comment_count > 0) { $current->new_comment_count = 0; $current->save(); } foreach ($current->notes() as $note) { if ($note->user_id == self::$user->id) { $note->state(AutoModeler::STATE_LOADED); $save = false; if ($note->new_comment_count > 0) { $note->new_comment_count = 0; $save = true; } if ($note->new_note > 0) { $note->new_note = null; $save = true; } if ($save) { $note->save(); } } } } } // Get view $section_comments = $this->section_image_comments($current); $section_comments->errors = $errors; $section_comments->values = $values; } else { if (!self::$user) { // Guest user $section_comments = $this->section_image_comments_teaser($current->comment_count); } } if (isset($section_comments) && $this->_request_type === Controller::REQUEST_AJAX) { $this->response->body($section_comments); return; } // Build page $this->view = View_Page::factory(__('Gallery')); // Image actions if (Permission::has($gallery, Model_Gallery::PERMISSION_UPDATE, self::$user)) { $this->view->actions[] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => 'default')) . '?token=' . Security::csrf(), 'text' => '<i class="icon-home icon-white"></i> ' . __('Set default'), 'class' => 'btn-inverse image-default'); } if (Permission::has($current, Model_Image::PERMISSION_DELETE, self::$user)) { $this->view->actions[] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => 'delete')) . '?token=' . Security::csrf(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete'), 'class' => 'btn-inverse image-delete'); } // Gallery actions $this->_set_page_actions(Permission::has(new Model_Gallery(), Model_Gallery::PERMISSION_CREATE, self::$user)); $this->_set_gallery($gallery); array_unshift($this->view->tabs, array('link' => Route::model($gallery), 'text' => '« ' . __('Gallery'))); $this->view->tabs['gallery'] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id)), 'text' => '<i class="icon-camera icon-white"></i> ' . __('Photo')); // Pagination $previous_url = $previous ? Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $previous->id, 'action' => $approve ? 'approve' : '')) . '#title' : null; $next_url = $next ? Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $next->id, 'action' => $approve ? 'approve' : '')) . '#title' : null; $this->view->add(View_Page::COLUMN_TOP, $this->section_image_pagination($previous_url, $next_url, $i, count($images))); // Image if (!isset($approve)) { $current->view_count++; $current->save(); } $this->view->add(View_Page::COLUMN_TOP, $this->section_image($current, $gallery, $next_url, (bool) $approve)); // Comments if (isset($section_comments)) { $this->view->add(View_Page::COLUMN_MAIN, $section_comments); } // Share if (Kohana::$config->load('site.facebook')) { Anqh::open_graph('title', __('Image') . ': ' . $gallery->name); Anqh::open_graph('url', URL::site(Route::url('gallery_image', array('id' => $current->id, 'gallery_id' => $gallery->id, 'action' => '')), true)); if ($current->description) { Anqh::open_graph('description', $current->description); } Anqh::open_graph('image', URL::site($current->get_url('thumbnail'), true)); } Anqh::share(true); $this->view->add(View_Page::COLUMN_SIDE, $this->section_share()); // Image info $this->view->add(View_Page::COLUMN_SIDE, $this->section_image_info($current)); } }
/** * Action: image */ public function action_image() { $gallery_id = (int) $this->request->param('gallery_id'); $image_id = $this->request->param('id'); /** @var Model_Gallery $gallery */ $gallery = Model_Gallery::factory($gallery_id); if (!$gallery->loaded()) { throw new Model_Exception($gallery, $gallery_id); } Permission::required($gallery, Model_Gallery::PERMISSION_READ); $images = $gallery->images(); // Find current, previous and next images $i = 0; /** @var Model_Image $next */ /** @var Model_Image $previous */ /** @var Model_Image $current */ $previous = $next = $current = null; foreach ($images as $image) { $i++; if (!is_null($current)) { // Current was found last round $next = $image; $i--; break; } else { if ($image->id == $image_id) { // Current found now $current = $image; // Fix state to loaded to perform update instead of insert when saving $current->state(AutoModeler::STATE_LOADED); } else { // No current found $previous = $image; } } } // Show image if (!is_null($current)) { // Comments section if (Permission::has($gallery, Model_Gallery::PERMISSION_COMMENTS)) { $errors = array(); $values = array(); // Handle comment if (Permission::has($gallery, Model_Gallery::PERMISSION_COMMENT) && $_POST) { try { $comment = Model_Image_Comment::factory()->add(Visitor::$user->id, null, Arr::get($_POST, 'comment'), Arr::get($_POST, 'private'), $current); $current->comment_count++; $current->save(); if ($current->author_id != Visitor::$user->id) { $target = Model_User::find_user($current->author_id); Notification_Galleries::image_comment(Visitor::$user, $target, $current, $comment->comment); } $gallery->comment_count++; $gallery->save(); if (!$comment->private) { // Noted users foreach ($current->notes() as $note) { if ($note->user_id) { $target = Model_User::find_user($note->user_id); Notification_Galleries::image_comment(Visitor::$user, $target, $current, $comment->comment); } } // Newsfeed NewsfeedItem_Galleries::comment(Visitor::$user, $gallery, $current); } // Redirect back to image if not ajax if ($this->_request_type !== Controller::REQUEST_AJAX) { $this->request->redirect(Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => ''))); return; } } catch (Validation_Exception $e) { $errors = $e->array->errors('validation'); $values = $comment; } } else { if (Visitor::$user) { // Clear new comment count? // @TODO: Remove, deprecated after new notification system if ($current->author_id == Visitor::$user->id && $current->new_comment_count > 0) { $current->new_comment_count = 0; $current->save(); } foreach ($current->notes() as $note) { if ($note->user_id == Visitor::$user->id) { $note->state(AutoModeler::STATE_LOADED); $save = false; if ($note->new_comment_count > 0) { $note->new_comment_count = 0; $save = true; } if ($note->new_note > 0) { $note->new_note = null; $save = true; } if ($save) { $note->save(); } } } } } // Get view $section_comments = $this->section_image_comments($current); $section_comments->errors = $errors; $section_comments->values = $values; } else { if (!Visitor::$user) { // Guest user $section_comments = $this->section_image_comments_teaser($current->comment_count); } } if (isset($section_comments) && $this->_request_type === Controller::REQUEST_AJAX) { $this->response->body($section_comments); return; } // Build page // Image actions if (Permission::has($gallery, Model_Gallery::PERMISSION_UPDATE)) { $this->view->actions[] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => 'default')) . '?token=' . Security::csrf(), 'text' => '<i class="icon-home"></i> ' . __('Set gallery default'), 'class' => 'btn-inverse image-default'); } if (Permission::has($current, Model_Image::PERMISSION_DELETE)) { $this->view->actions[] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => 'delete')) . '?token=' . Security::csrf(), 'text' => '<i class="icon-trash"></i> ' . __('Delete'), 'class' => 'btn-inverse image-delete'); } if (Permission::has($current, Model_Image::PERMISSION_REPORT)) { $this->view->actions[] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id, 'action' => 'report')), 'text' => '<i class="icon-warning-sign"></i> ' . __('Report'), 'class' => 'btn-inverse dialogify', 'data-dialog-title' => __('Report image')); } // Gallery actions $this->_set_page_actions(Permission::has(new Model_Gallery(), Model_Gallery::PERMISSION_CREATE)); $this->_set_gallery($gallery); array_unshift($this->view->tabs, array('link' => Route::model($gallery), 'text' => '« ' . __('Gallery'))); $this->view->tabs['gallery'] = array('link' => Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $current->id)), 'text' => '<i class="icon-camera-retro"></i> ' . __('Photo')); // Event info if ($event = $gallery->event()) { $this->view->subtitle = Controller_Events::_event_subtitle($event); } // Pagination $previous_url = $previous ? Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $previous->id, 'action' => '')) . '#title' : null; $next_url = $next ? Route::url('gallery_image', array('gallery_id' => Route::model_id($gallery), 'id' => $next->id, 'action' => '')) . '#title' : null; $this->view->add(View_Page::COLUMN_TOP, $this->section_image_pagination($previous_url, $next_url)); // Image $current->view_count++; $current->save(); $this->view->add(View_Page::COLUMN_TOP, $this->section_image($current, $gallery, $next_url)); // Comments if (isset($section_comments)) { $this->view->add(View_Page::COLUMN_CENTER, $section_comments); } // Share Anqh::page_meta('title', __('Image') . ': ' . $gallery->name); Anqh::page_meta('url', URL::site(Route::url('gallery_image', array('id' => $current->id, 'gallery_id' => $gallery->id, 'action' => '')), true)); if ($current->description) { Anqh::page_meta('description', $current->description); } Anqh::page_meta('image', URL::site($current->get_url('thumbnail'), true)); Anqh::page_meta('twitter:card', 'photo'); Anqh::share(true); } }