Example #1
0
 public function onInit()
 {
     $wa = wa();
     $id = $wa->getUser()->getId();
     if ($id && $wa->getApp() == 'blog' && $wa->getEnv() == 'backend') {
         $this->setCount($this->onCount(false));
         blogActivity::setUserActivity($id, false);
     }
 }
 /**
  *
  * Update blog post item
  * @param int $id
  * @param array $data
  * @param array $current_data
  * @throws waException
  * @return int post id
  */
 public function updateItem($id, $data = array(), $current_data = array())
 {
     $plugin = array();
     $contact_id = wa()->getUser()->getId();
     foreach ($data as $field => $value) {
         if (!isset($this->fields[$field]) || $field == $this->id) {
             if (isset($data['plugin'])) {
                 $plugin = $data['plugin'];
             }
             unset($data[$field]);
         }
     }
     if ($id) {
         if (!$current_data) {
             $current_data = $this->getByField(array($this->id => $id));
             if (!$current_data) {
                 throw new waException(_w('Post not found'), 404);
             }
         }
         if (!$contact_id) {
             //use author id for cron task
             $contact_id = $current_data['contact_id'];
         }
     } else {
         $current_data = array();
         if (empty($data['contact_id'])) {
             $data['contact_id'] = $contact_id;
         } else {
             blogHelper::checkRights($data['blog_id'], $contact_id, $contact_id != $data['contact_id'] ? blogRightConfig::RIGHT_FULL : blogRightConfig::RIGHT_READ_WRITE);
         }
     }
     //check rights for non admin
     $source_data = array('contact_id' => isset($current_data['contact_id']) ? $current_data['contact_id'] : $data['contact_id'], 'blog_id' => isset($current_data['blog_id']) ? $current_data['blog_id'] : $data['blog_id']);
     $target_data = array('contact_id' => isset($data['contact_id']) ? $data['contact_id'] : $source_data['contact_id'], 'blog_id' => isset($data['blog_id']) ? $data['blog_id'] : $source_data['blog_id']);
     //check editor rights
     blogHelper::checkRights($source_data['blog_id'], $contact_id);
     //change blog
     if ($source_data['blog_id'] != $target_data['blog_id']) {
         //check editor rights for target blog
         blogHelper::checkRights($target_data['blog_id'], $contact_id, $contact_id != $target_data['contact_id'] ? blogRightConfig::RIGHT_FULL : blogRightConfig::RIGHT_READ_WRITE);
         //check (new) author rights
         if ($contact_id != $target_data['contact_id']) {
             //skip it = for admin it allowed
             //blogHelper::checkRights($target_data['blog_id'],$target_data['contact_id']);
         }
     } else {
         //check new author rights
         if ($contact_id != $target_data['contact_id'] && $target_data['contact_id'] != $source_data['contact_id']) {
             blogHelper::checkRights($target_data['blog_id'], $target_data['contact_id']);
         }
     }
     //status changes
     if (isset($data['status'])) {
         switch ($data['status']) {
             case self::STATUS_PUBLISHED:
                 if (!isset($data['datetime']) || !$data['datetime']) {
                     if (!isset($current_data['datetime']) || !$current_data['datetime']) {
                         $data['datetime'] = date("Y-m-d H:i:s");
                     } elseif (isset($current_data['status']) && !in_array($current_data['status'], array(self::STATUS_PUBLISHED, self::STATUS_SCHEDULED))) {
                         $data['datetime'] = date("Y-m-d H:i:s");
                     } else {
                         unset($data['datetime']);
                     }
                 }
                 break;
             case self::STATUS_DRAFT:
                 if (!isset($data['datetime']) || !$data['datetime']) {
                     if (!isset($current_data['datetime']) || !$current_data['datetime']) {
                         $data['datetime'] = date("Y-m-d H:i:s");
                     } else {
                         unset($data['datetime']);
                     }
                 }
                 break;
             case self::STATUS_SCHEDULED:
                 if (!isset($data['datetime']) || !$data['datetime']) {
                     unset($data['datetime']);
                 }
                 break;
             case self::STATUS_DEADLINE:
                 if (!isset($data['datetime']) || !$data['datetime'] || is_array($data['datetime']) && !$data['datetime'][0]) {
                     $data['status'] = self::STATUS_DRAFT;
                     $data['datetime'] = date("Y-m-d H:i:s");
                 }
                 break;
         }
     }
     if (!$id && (!isset($data['contact_id']) || !$data['contact_id'])) {
         $data['contact_id'] = wa()->getUser()->getId();
     }
     if (isset($data['url']) && strlen($data['url'])) {
         if (substr($data['url'], -1) == '/') {
             $data['url'] = preg_replace('~\\/+$~', '', $data['url']);
         }
         if (strpos($data['url'], '/') !== false) {
             throw new waException(_w('URL must not contain /'));
         }
         if ($this->checkUrl($data['url'], $id)) {
             throw new waException(_w('This address is already in use') . ' ' . $data['url']);
         }
     } else {
         //$data['url'] = blogHelper::transliterate($data['url']);
     }
     $edit = $id ? true : false;
     $event_map = array(0 => array(0 => array('post_presave', 'post_save'), self::STATUS_PUBLISHED => array('post_prepublish', 'post_publish'), self::STATUS_SCHEDULED => array('post_preshedule', 'post_shedule'), self::STATUS_DEADLINE => array('post_presave', 'post_save'), self::STATUS_DRAFT => array('post_presave', 'post_save')), self::STATUS_DRAFT => array(0 => array('post_presave', 'post_save'), self::STATUS_PUBLISHED => array('post_prepublish', 'post_publish'), self::STATUS_SCHEDULED => array('post_preshedule', 'post_shedule'), self::STATUS_DEADLINE => array('post_presave', 'post_save'), self::STATUS_DRAFT => array('post_presave', 'post_save')), self::STATUS_DEADLINE => array(0 => array('post_presave', 'post_save'), self::STATUS_PUBLISHED => array('post_prepublish', 'post_publish'), self::STATUS_SCHEDULED => array('post_preshedule', 'post_shedule'), self::STATUS_DEADLINE => array('post_presave', 'post_save'), self::STATUS_DRAFT => array('post_presave', 'post_save')), self::STATUS_SCHEDULED => array(0 => array('post_presave', 'post_save'), self::STATUS_PUBLISHED => array('post_prepublish', 'post_publish'), self::STATUS_SCHEDULED => array('post_presave', 'post_save'), self::STATUS_DEADLINE => array('post_presave', 'post_save'), self::STATUS_DRAFT => array('post_presave', 'post_save')), self::STATUS_PUBLISHED => array(0 => array('post_presave', 'post_save'), self::STATUS_PUBLISHED => array('post_presave', 'post_save'), self::STATUS_SCHEDULED => array('post_preshedule', 'post_shedule'), self::STATUS_DEADLINE => array('post_presave', 'post_save'), self::STATUS_DRAFT => array('post_presave', 'post_save')));
     $events = $event_map[isset($current_data['status']) ? $current_data['status'] : 0][isset($data['status']) ? $data['status'] : 0];
     $data['plugin'] = $plugin;
     /**
      * @event post_prepublish
      * @event post_preshedule
      * @event post_presave
      * @param array [string]mixed $data
      * @param array [string]int $data['id']
      * @param array [string][string]mixed $data['plugin']['%plugin_id']
      * @return array[%plugin_id%][%field%]string Error message for field %field%
      */
     $errors = wa()->event(array_shift($events), $data);
     if ($id) {
         if ($source_data['blog_id'] != $target_data['blog_id']) {
             $comment_model = new blogCommentModel();
             $comment_model->updateByField('post_id', $id, array('blog_id' => $target_data['blog_id']));
         }
         $this->updateById($id, $data);
         $data[$this->id] = $id;
     } else {
         $id = $this->insert($data);
         blogActivity::setUserActivity();
         $data[$this->id] = $id;
         if (!isset($data['url']) || strlen($data['url']) == 0) {
             $this->updateById($id, array('url' => $id));
         }
     }
     //status changed
     //blog_id changed
     $data = array_merge($current_data, $data);
     $blog_model = new blogBlogModel();
     if ($edit) {
         //unpublish
         if ($current_data['status'] == self::STATUS_PUBLISHED && $data['status'] != self::STATUS_PUBLISHED) {
             $blog_model->updateQty($data['blog_id'], '-1');
             //publish
         } elseif ($current_data['status'] != self::STATUS_PUBLISHED && $data['status'] == self::STATUS_PUBLISHED) {
             $blog_model->updateQty($data['blog_id'], '+1');
             //move
         } elseif (isset($current_data['blog_id']) && $current_data['status'] == self::STATUS_PUBLISHED && $data['status'] == self::STATUS_PUBLISHED && $current_data['blog_id'] != $data['blog_id']) {
             $blog_model->updateQty($data['blog_id'], '+1');
             $blog_model->updateQty($current_data['blog_id'], '-1');
         }
     } else {
         if ($data['status'] == self::STATUS_PUBLISHED) {
             $blog_model->updateQty($data['blog_id'], '+1');
         }
     }
     /**
      * @event post_publish
      * @event post_shedule
      * @event post_save
      * @param array [string]mixed $data
      * @param array [string]int $data['id']
      * @param array [string][string]mixed $data['plugin']['%plugin_id']
      * @return void
      */
     wa()->event(array_shift($events), $data);
     return $id;
 }
 public function add($comment, $parent = null)
 {
     if (!isset($comment['ip']) && ($ip = waRequest::getIp())) {
         $ip = ip2long($ip);
         if ($ip > 2147483647) {
             $ip -= 4294967296.0;
         }
         $comment['ip'] = $ip;
     }
     if (!isset($comment['datetime'])) {
         $comment['datetime'] = date('Y-m-d H:i:s');
     }
     if (isset($comment['site']) && $comment['site']) {
         if (!preg_match('@^https?://@', $comment['site'])) {
             $comment['site'] = 'http://' . $comment['site'];
         }
     }
     $comment[$this->parent] = $parent;
     blogActivity::setUserActivity();
     /**
      * @event comment_presave_frontend
      * @event comment_presave_backend
      * @param array $comment
      * @param int $comment.id
      * @param int $comment.parent
      * @return void
      */
     wa()->event('comment_presave_' . wa()->getEnv(), $comment);
     $comment['id'] = parent::add($comment, $parent);
     /**
      * @event comment_save_frontend
      * @event comment_save_backend
      * @param array $comment
      * @param int $comment.id
      * @param int $comment.parent
      * @return void
      */
     wa()->event('comment_save_' . wa()->getEnv(), $comment);
     return $comment['id'];
 }