Exemplo n.º 1
0
 /**
  * Action: flyer
  */
 public function action_flyer()
 {
     $flyer_id = $this->request->param('id');
     switch ($flyer_id) {
         // Random flyer
         case 'random':
             // Random undated flyer
         // Random undated flyer
         case 'undated':
             $flyer = Model_Flyer::factory()->find_random($flyer_id == 'undated');
             $this->request->redirect(Route::get('flyer')->uri(array('id' => $flyer->id)));
             break;
             // Known flyer
         // Known flyer
         default:
             /** @var  Model_Flyer  $flyer */
             $flyer = new Model_Flyer((int) $flyer_id);
             if (!$flyer->loaded()) {
                 throw new Model_Exception($flyer, $flyer_id);
             }
     }
     /** @var  Model_Image  $image */
     $image = $flyer->image();
     /** @var  Model_Event  $event */
     $event = $flyer->event();
     // Handle post
     $errors = array();
     if ((isset($_POST['event_id']) || isset($_POST['name'])) && Security::csrf_valid()) {
         Permission::required($flyer, Model_Flyer::PERMISSION_UPDATE);
         try {
             if ($event_id = (int) Arr::get($_POST, 'event_id')) {
                 // Event given?
                 /** @var  Model_Event  $event */
                 $event = new Model_Event($event_id);
                 if ($event->loaded()) {
                     $flyer->set_fields(array('event_id' => $event->id, 'stamp_begin' => $event->stamp_begin));
                 }
             } else {
                 if (Arr::get($_POST, 'name')) {
                     // Name and stamp given
                     $flyer->set_fields(Arr::intersect($_POST, array('name', 'stamp_begin')));
                 }
             }
             // Save only if we got full date
             if ($flyer->has_full_date()) {
                 $flyer->save();
                 // Newsfeed
                 NewsfeedItem_Galleries::flyer_edit(Visitor::$user, $flyer);
                 if ($event_id && $event->loaded() && !$event->flyer_front_image_id) {
                     $event->flyer_front_image_id = $flyer->image_id;
                     $event->flyer_front_url = $flyer->image()->get_url();
                     $event->save();
                 }
             }
             $this->request->redirect(Route::get('flyer')->uri(array('id' => $flyer->id)));
         } catch (Validation_Exception $e) {
             $errors = $e->array->errors('validation');
         }
     }
     // Comments section
     if (false && Permission::has($flyer, Model_Flyer::PERMISSION_COMMENTS)) {
         $errors = array();
         $values = array();
         // Handle comment
         if ($_POST && Permission::has($flyer, Model_Flyer::PERMISSION_COMMENT)) {
             try {
                 $comment = Model_Image_Comment::factory()->add(Visitor::$user->id, null, Arr::get($_POST, 'comment'), Arr::get($_POST, 'private'), $image);
                 $image->comment_count++;
                 if ($image->author_id != Visitor::$user->id) {
                     $image->new_comment_count++;
                 }
                 $image->save();
                 // Newsfeed
                 if (!$comment->private) {
                     NewsfeedItem_Galleries::comment_flyer(Visitor::$user, $flyer, $image);
                 }
                 if (!$this->ajax) {
                     $this->request->redirect(Route::get('flyer')->uri(array('id' => $image->id, 'action' => '')));
                 }
             } catch (Validation_Exception $e) {
                 $errors = $e->array->errors('validation');
                 $values = $comment;
             }
         } else {
             if (Visitor::$user && $image->author_id == Visitor::$user->id && $image->new_comment_count > 0) {
                 // Clear new comment count?
                 $image->new_comment_count = 0;
                 $image->save();
             }
         }
         // Get view
         $section_comments = $this->section_image_comments($image, 'flyer_comment');
         $section_comments->errors = $errors;
         $section_comments->values = $values;
     } else {
         if (!Visitor::$user) {
             // Guest user
             $section_comments = $this->section_image_comments_teaser($image->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(__('Flyer'));
     $this->_set_page_actions();
     $this->_set_flyer_actions($flyer);
     // Set title
     Anqh::page_meta('title', __('Flyer') . ': ' . $event->name);
     Anqh::page_meta('url', URL::site(Route::get('flyer')->uri(array('id' => $flyer->id, 'action' => '')), true));
     Anqh::page_meta('twitter:card', 'photo');
     Anqh::page_meta('image', URL::site($flyer->image_url('thumbnail'), true));
     if ($event) {
         // Flyer is linked to an event
         $this->view->title = $event->name;
         $this->view->subtitle = Controller_Events::_event_subtitle($event);
         // Open graph
         Anqh::page_meta('description', date('l ', $event->stamp_begin) . Date::format(Date::DMY_SHORT, $event->stamp_begin) . ' @ ' . $event->venue_name);
     } else {
         // Flyer is not linked to an event
         $this->view->title = $flyer->name;
         $this->view->subtitle = $flyer->has_full_date() ? HTML::time(date('l ', $flyer->stamp_begin) . Date::format(Date::DMY_SHORT, $flyer->stamp_begin), $flyer->stamp_begin, true) : __('Date unknown');
         // Open graph
         $flyer->has_full_date() and Anqh::page_meta('description', date('l ', $flyer->stamp_begin) . Date::format(Date::DMY_SHORT, $flyer->stamp_begin));
     }
     Anqh::share(true);
     // Edit flyer
     if (Permission::has($flyer, Model_Flyer::PERMISSION_UPDATE)) {
         $section = $this->section_flyer_edit($flyer);
         $section->error = $errors;
         $this->view->add(View_Page::COLUMN_TOP, $section);
     }
     // Flyer
     if ($image) {
         $image->view_count++;
         $image->save();
     }
     $this->view->add(View_Page::COLUMN_TOP, $this->section_flyer($flyer));
     // Comments
     if (isset($section_comments)) {
         $this->view->add(View_Page::COLUMN_CENTER, $section_comments);
     }
 }