/**
  * Add a new news
  *
  * @param array $newsInfo
  *      string title
  *      string intro
  *      string text
  *      string meta_description
  *      string meta_keywords
  *      integer created optional
  * @param array $categories
  * @param array $image
  * @param boolean $approved
  * @return boolean|string
  */
 public function addNews(array $newsInfo, $categories = null, array $image = [], $approved = false)
 {
     try {
         $this->adapter->getDriver()->getConnection()->beginTransaction();
         // check the news create date
         if (empty($newsInfo['created'])) {
             $newsInfo['created'] = time();
         }
         $insert = $this->insert()->into('news_list')->values(array_merge($newsInfo, ['status' => $approved ? self::STATUS_APPROVED : self::STATUS_DISAPPROVED, 'language' => $this->getCurrentLanguage(), 'date_edited' => date('Y-m-d')]));
         $statement = $this->prepareStatementForSqlObject($insert);
         $statement->execute();
         $insertId = $this->adapter->getDriver()->getLastGeneratedValue();
         // generate a slug automatically
         if (empty($newsInfo['slug'])) {
             $update = $this->update()->table('news_list')->set(['slug' => $this->generateSlug($insertId, $newsInfo['title'], 'news_list', 'id', self::NEWS_SLUG_LENGTH)])->where(['id' => $insertId]);
             $statement = $this->prepareStatementForSqlObject($update);
             $statement->execute();
         }
         // update categories
         $this->updateCategories($insertId, $categories);
         // upload the news's image
         $this->uploadImage($insertId, $image);
         $this->adapter->getDriver()->getConnection()->commit();
     } catch (Exception $e) {
         $this->adapter->getDriver()->getConnection()->rollback();
         ApplicationErrorLogger::log($e);
         return $e->getMessage();
     }
     // fire the add news event
     NewsEvent::fireAddNewsEvent($insertId);
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Delete a news
  *
  * @param array $newsInfo
  *      integer id
  *      string title
  *      string slug
  *      string intro
  *      string text
  *      string status
  *      string image
  *      string meta_description
  *      string meta_keywords
  *      integer created
  *      string language
  *      array categories
  *      string date_edited
  * @throws \News\Exception\NewsException
  * @return boolean|string
  */
 public function deleteNews(array $newsInfo)
 {
     try {
         $this->adapter->getDriver()->getConnection()->beginTransaction();
         $delete = $this->delete()->from('news_list')->where(['id' => $newsInfo['id']]);
         $statement = $this->prepareStatementForSqlObject($delete);
         $result = $statement->execute();
         // delete an image
         if ($newsInfo['image']) {
             if (true !== ($imageDeleteResult = $this->deleteNewsImage($newsInfo['image']))) {
                 throw new NewsException('Image deleting failed');
             }
         }
         $this->adapter->getDriver()->getConnection()->commit();
     } catch (Exception $e) {
         $this->adapter->getDriver()->getConnection()->rollback();
         ApplicationErrorLogger::log($e);
         return $e->getMessage();
     }
     $result = $result->count() ? true : false;
     // fire the delete news event
     if ($result) {
         NewsEvent::fireDeleteNewsEvent($newsInfo['id']);
     }
     return $result;
 }