public function execute()
 {
     $post_title = waRequest::post('post_title', '', waRequest::TYPE_STRING_TRIM);
     $blog_id = waRequest::post('blog_id', 0, waRequest::TYPE_INT);
     $slug = waRequest::post('slug', '', waRequest::TYPE_STRING_TRIM);
     $blog_model = new blogBlogModel();
     $blog = $blog_model->getById($blog_id);
     if (!$blog) {
         throw new waException(_w("Can't find corresponding blog"));
     }
     $this->response['is_private_blog'] = $blog['status'] == blogBlogModel::STATUS_PRIVATE;
     $post_id = waRequest::post('post_id', 0, waRequest::TYPE_INT);
     $post_model = new blogPostModel();
     if ($post_id) {
         $post = $post_model->getById($post_id, array('text', 'text_before_cut'));
         if (!$post) {
             throw new waException(_w("Can't find corresponding post"));
         }
         if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
             $options = array('contact_id' => $post['contact_id'], 'blog_id' => $blog_id, 'post_id' => $post['id'], 'user_id' => wa()->getUser()->getId());
             $this->response['preview_hash'] = blogPostModel::getPreviewHash($options);
             $this->response['preview_hash'] = base64_encode($this->response['preview_hash'] . $options['user_id']);
         }
         $this->response['slug'] = $post['url'];
         $this->response['is_published'] = $post['status'] == blogPostModel::STATUS_PUBLISHED;
         $this->response['is_adding'] = false;
     } else {
         $post = array();
         $this->response['slug'] = $slug ? $slug : blogHelper::transliterate($post_title);
         $this->response['is_published'] = false;
         $this->response['is_adding'] = true;
     }
     $post['blog_id'] = $blog_id;
     $post['album_link_type'] = 'blog';
     $other_links = blogPostModel::getPureUrls($post);
     $this->response['link'] = array_shift($other_links);
     if (!$this->response['link']) {
         $this->response['is_private_blog'] = true;
     }
     $this->response['other_links'] = $other_links;
     foreach ($this->response as $k => &$item) {
         if (!$item || !is_string($item) && !is_array($item)) {
             continue;
         }
         if (is_array($item)) {
             $item = array_map('htmlspecialchars', $item, array_fill(0, count($item), ENT_QUOTES));
             continue;
         }
         $item = htmlspecialchars($item, ENT_QUOTES);
     }
     unset($item);
     $this->getResponse()->addHeader('Content-type', 'application/json');
 }
 public function execute()
 {
     $post_id = waRequest::get('id', null, waRequest::TYPE_INT);
     $blog_model = new blogBlogModel();
     $blogs = $blog_model->getAvailable();
     if (!$blogs) {
         $this->setTemplate('BlogNotFound');
         return;
     }
     $blogs = $blog_model->prepareView($blogs);
     if ($post_id) {
         // edit post
         $post_model = new blogPostModel();
         $post = $post_model->getById($post_id);
         if (!$post) {
             throw new waException(_w('Post not found'), 404);
         }
         //check rights
         if (blogHelper::checkRights($post['blog_id']) < blogRightConfig::RIGHT_FULL && $post['contact_id'] != $this->getUser()->getId()) {
             throw new waRightsException(_w('Access denied'));
         }
         $post['datetime'] = $post['datetime'] >= 1971 ? $post['datetime'] : '';
         $blog_id = $post['blog_id'];
         $blog = $blogs[$blog_id];
         $title = trim(sprintf(_w('Editing post %s'), $post['title']));
     } else {
         // add post
         $date = waRequest::get('date', '');
         $blog = $this->getAllowedBlog($blogs, wa()->getStorage()->read('blog_last_id'));
         if (!$blog) {
             throw new waRightsException(_w('Access denied'));
         }
         $blog_id = $blog['id'];
         $post = array('title' => $this->getRequest()->post('title', '', waRequest::TYPE_STRING_TRIM), 'text' => $this->getRequest()->post('text', '', waRequest::TYPE_STRING_TRIM), 'continued_text' => null, 'categories' => array(), 'contact_id' => wa()->getUser()->getId(), 'url' => '', 'blog_id' => $blog_id, 'comments_allowed' => true);
         $post['id'] = '';
         $post['status'] = $date ? blogPostModel::STATUS_DEADLINE : blogPostModel::STATUS_DRAFT;
         $post['datetime'] = '';
         $post['meta_title'] = null;
         $post['meta_keywords'] = null;
         $post['meta_description'] = null;
         $title = _w('Adding new post');
     }
     $all_links = blogPostModel::getPureUrls($post);
     $post['other_links'] = $all_links;
     $post['link'] = array_shift($post['other_links']);
     $post['remaining_time'] = null;
     if ($post['status'] == blogPostModel::STATUS_SCHEDULED && $post['datetime']) {
         $post['remaining_time'] = $this->calculateRemainingTime($post['datetime']);
     }
     if ($blog['rights'] >= blogRightConfig::RIGHT_FULL) {
         $users = blogHelper::getAuthors($post['blog_id']);
     } else {
         $user = $this->getUser();
         $users = array($user->getId() => $user->getName());
     }
     // preview hash for all type of drafts
     if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
         $options = array('contact_id' => $post['contact_id'], 'blog_id' => $blog_id, 'post_id' => $post['id'], 'user_id' => wa()->getUser()->getId());
         $preview_hash = blogPostModel::getPreviewHash($options);
         $this->view->assign('preview_hash', base64_encode($preview_hash . $options['user_id']));
     }
     $this->view->assign('no_settlements', empty($all_links) ? true : false);
     $this->view->assign('params', $this->getPostParams($post['id']));
     $this->view->assign('blog', $blog);
     $this->view->assign('users', $users);
     $this->view->assign('blogs', $blogs);
     $allow_change_blog = 0;
     foreach ($blogs as $blog_item) {
         if ($blog_item['rights'] >= blogRightConfig::RIGHT_READ_WRITE) {
             ++$allow_change_blog;
         }
     }
     $this->view->assign('allow_change_blog', $allow_change_blog);
     $this->view->assign('post_id', $post_id);
     $this->view->assign('datetime_timezone', waDateTime::date("T", null, wa()->getUser()->getTimezone()));
     /**
      * Backend post edit page
      * UI hook allow extends post edit page
      * @event backend_post_edit
      * @param array[string]mixed $post
      * @param array[string]int $post['id']
      * @param  array[string]int $post['blog_id']
      * @return array[string][string]string $return[%plugin_id%]['sidebar'] Plugin sidebar html output
      * @return array[string][string]string $return[%plugin_id%]['toolbar'] Plugin toolbar html output
      * @return array[string][string]string $return[%plugin_id%]['editor_tab'] Plugin editor tab html output
      */
     $this->view->assign('backend_post_edit', wa()->event('backend_post_edit', $post, array('sidebar', 'toolbar', 'editor_tab')));
     $app_settings = new waAppSettingsModel();
     $show_comments = $app_settings->get($this->getApp(), 'show_comments', true);
     $this->view->assign('show_comments', $show_comments);
     $this->view->assign('post', $post);
     /**
      * @deprecated 
      * For backward compatibility reason
      */
     $this->view->assign('cron_schedule_time', waSystem::getSetting('cron_schedule', 0, 'blog'));
     $this->view->assign('last_schedule_cron_time', waSystem::getSetting('last_schedule_cron_time', 0, 'blog'));
     $this->view->assign('cron_command', 'php ' . wa()->getConfig()->getRootPath() . '/cli.php blog schedule');
     $this->setLayout(new blogDefaultLayout());
     $this->getResponse()->setTitle($title);
 }