예제 #1
0
 public function submitAction()
 {
     // Is the form correctly posted ?
     if (!$this->getRequest()->isPost()) {
         return $this->_forward('index');
     }
     // Is the form valid ?
     $form = $this->getForm($this->getAvailableSources());
     if (!$form->isValid($_POST)) {
         $this->view->form = $form;
         $this->addErrorMessage("Please check your input and try again.");
         return $this->_forward('index');
     }
     // Get the values
     // TODO SECURE THIS ! Especially verify that the user
     // has access to the given sources !
     $values = $form->getValues();
     $title = $values['title'];
     $subtitle = $values['subtitle'];
     $date_from = Stuffpress_Date::strToTimezone($values['date_from'], $this->_properties->getProperty("timezone"));
     $date_to = Stuffpress_Date::strToTimezone($values['date_to'], $this->_properties->getProperty("timezone")) + 86400;
     $sources = $values['sources'];
     if (count($sources) == 0) {
         $this->view->form = $form;
         $this->addErrorMessage("You must select at least one source to build a story.");
         return $this->_forward('index');
     }
     // Fetch the items
     $data = new Data();
     $storyItems = new StoryItems();
     $items = array();
     foreach ($sources as $source_id) {
         $i = $data->getItemsByDate($date_from, $date_to, $source_id, true);
         foreach ($i as $item) {
             $type = $item->getType();
             $items[] = $item;
         }
     }
     // If no items, we have an error
     if (!$items || count($items) == 0) {
         $this->view->form = $form;
         $this->addErrorMessage("Your story does not contain any items.");
         return $this->_forward('index');
     }
     // Create the new story
     $stories = new Stories();
     $story_id = $stories->addStory($date_from, $date_to, $title, $subtitle, serialize($sources));
     // Add the story items
     $images = array();
     foreach ($items as $item) {
         if ($item->getType() == SourceItem::IMAGE_TYPE) {
             $images[] = $item->getImageUrl(ImageItem::SIZE_MEDIUM);
         }
         $storyItems->addItem($story_id, $item->getSource(), $item->getID(), $item->getPrefix(), $item->getTimestamp(), $item->ishidden());
     }
     // Pick an image randomly and save it
     if (count($images) > 0) {
         $image = $images[rand(0, count($images) - 1)];
         $this->setKeyImage($story_id, $image);
     }
     // Forward to the story page
     $config = Zend_Registry::get('configuration');
     $host = $config->web->host;
     $url = $this->getUrl($this->_application->user->username, "/story/edit/id/{$story_id}");
     return $this->_redirect($url);
 }