/** * save category * * @todo only INSERTs are supported, UPDATE has to be implemented * @throws ArticleCategoryException */ public function save() { $db = Application::getInstance()->getDb(); if (is_null($this->parentCategory)) { // prepare to insert top level category $rows = $db->doPreparedQuery('SELECT MAX(r) + 1 AS l FROM articlecategories'); $this->l = !isset($rows[0]['l']) ? 0 : $rows[0]['l']; $this->r = $rows[0]['l'] + 1; $this->level = 0; } else { // prepare to insert subcategory // in case parent category has not been saved - save it if (is_null($this->parentCategory->id)) { $this->parentCategory->save(); } try { $nsData = $this->parentCategory->getNsData(); } catch (ArticleCategoryException $e) { throw $e; } $this->l = $nsData['r']; $this->r = $nsData['r'] + 1; $this->level = $nsData['level'] + 1; $db->execute('UPDATE articlecategories SET r = r + 2 WHERE r >= ?', array($this->l)); $db->execute('UPDATE articlecategories SET l = l + 2 WHERE l > ?', array($this->r)); } // insert category data $this->alias = MysqlPDOUtil::getAlias($db, $this->title, 'articlecategories'); $this->id = $db->insertRecord('articlecategories', array('Alias' => $this->alias, 'l' => $this->l, 'r' => $this->r, 'level' => $this->level, 'Title' => $this->title, 'customSort' => $this->customSort)); self::$instancesByAlias[$this->alias] = $this; self::$instancesById[$this->id] = $this; }
/** * 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(); }