Exemple #1
0
 /**
  * store new article in database or update changes to existing article
  *
  * @throws ArticleException
  * @todo consider transactions
  */
 public function save()
 {
     $db = Application::getInstance()->getDb();
     // a headline is a required attribute
     if (is_null($this->headline) || trim($this->headline) == '') {
         throw new ArticleException("Headline not set. Article can't be inserted", ArticleException::ARTICLE_HEADLINE_NOT_SET);
     }
     // a category is a required attribute
     if (is_null($this->category)) {
         throw new ArticleException("Category not set. Article can't be inserted", ArticleException::ARTICLE_CATEGORY_NOT_SET);
     }
     // allow listeners to react to event
     ArticleEvent::create(ArticleEvent::BEFORE_ARTICLE_SAVE, $this)->trigger();
     // afterwards collect all current data in array
     $cols = array_merge((array) $this->getData(), array('Alias' => $this->alias, 'articlecategoriesID' => $this->category->getId(), 'Headline' => $this->headline, 'Article_Date' => is_null($this->articleDate) ? NULL : $this->articleDate->format('Y-m-d H:i:s'), 'Display_from' => is_null($this->displayFrom) ? NULL : $this->displayFrom->format('Y-m-d H:i:s'), 'Display_until' => is_null($this->displayUntil) ? NULL : $this->displayUntil->format('Y-m-d H:i:s'), 'published' => $this->published, 'customFlags' => $this->customFlags, 'customSort' => $this->customSort, 'publishedBy' => $this->publishedBy ? $this->publishedBy->getAdminId() : NULL, 'updatedBy' => $this->updatedBy ? $this->updatedBy->getAdminId() : NULL));
     if (!is_null($this->id)) {
         // is a full update necessary?
         if ($this->wasChanged()) {
             // update
             $this->alias = MysqlPDOUtil::getAlias($db, $this->headline, 'articles', $this->id);
             $db->updateRecord('articles', $this->id, $cols);
         } else {
             if ($this->wasChanged(TRUE)) {
                 // update, but don't set lastUpdated and updatedBy
                 unset($cols['updatedBy']);
                 $this->alias = MysqlPDOUtil::getAlias($db, $this->headline, 'articles', $this->id);
                 $db->ignoreLastUpdated()->updateRecord('articles', $this->id, $cols);
             }
         }
     } else {
         // insert
         $this->alias = MysqlPDOUtil::getAlias($db, $this->headline, 'articles');
         $cols = array_merge((array) $this->getData(), array('Alias' => $this->alias, 'articlecategoriesID' => $this->category->getId(), 'Headline' => $this->headline, 'Article_Date' => is_null($this->articleDate) ? NULL : $this->articleDate->format('Y-m-d H:i:s'), 'Display_from' => is_null($this->displayFrom) ? NULL : $this->displayFrom->format('Y-m-d H:i:s'), 'Display_until' => is_null($this->displayUntil) ? NULL : $this->displayUntil->format('Y-m-d H:i:s'), 'published' => $this->published, 'customFlags' => $this->customFlags, 'customSort' => $this->customSort, 'publishedBy' => $this->publishedBy ? $this->publishedBy->getAdminId() : NULL, 'createdBy' => $this->createdBy ? $this->createdBy->getAdminId() : NULL));
         $this->id = $db->insertRecord('articles', $cols);
     }
     // store link information for linked files if linked files were changed in any way
     if ($this->updateLinkedFiles) {
         // delete all previous entries
         $db->deleteRecord('articles_files', array('articlesID' => $this->id), TRUE);
         // save new references and use position in array as customSort value
         foreach ($this->linkedFiles as $sortPosition => $file) {
             $db->insertRecord('articles_files', array('articlesID' => $this->id, 'filesID' => $file->getId(), 'customSort' => $sortPosition));
         }
         $this->updateLinkedFiles = FALSE;
     }
     ArticleEvent::create(ArticleEvent::AFTER_ARTICLE_SAVE, $this)->trigger();
 }