public function execute()
 {
     $this->setLayout(new blogDefaultLayout());
     $blog_id = (int) waRequest::get('blog');
     $blog_model = new blogBlogModel();
     if ($blog_id && $this->getRights("blog.{$blog_id}") < blogRightConfig::RIGHT_FULL || !$blog_id && !$this->getRights(blogRightConfig::RIGHT_ADD_BLOG)) {
         throw new waRightsException(_w('Access denied'));
     }
     // save settings (POST)
     $settings = waRequest::post('settings');
     $draft_data = array();
     if ($settings) {
         $settings['status'] = isset($settings['status']) ? blogBlogModel::STATUS_PUBLIC : blogBlogModel::STATUS_PRIVATE;
         $settings['name'] = trim($settings['name']);
         $settings['icon'] = !empty($settings['icon_url']) ? $settings['icon_url'] : $settings['icon'];
         if (isset($settings['qty'])) {
             unset($settings['qty']);
         }
         if (isset($settings['sort'])) {
             unset($settings['sort']);
         }
         $settings['id'] = $blog_id;
         $validate_massages = $this->validate($settings);
         if (!$validate_massages) {
             //TODO handle settings
             if ($blog_id) {
                 $blog_model->updateById($blog_id, $settings);
                 $this->log('blog_modify');
             } else {
                 $settings['sort'] = (int) $blog_model->select('MAX(`sort`)')->fetchField() + 1;
                 $blog_id = $blog_model->insert($settings);
                 $this->getUser()->setRight($this->getApp(), "blog.{$blog_id}", blogRightConfig::RIGHT_FULL);
                 $this->log('blog_add');
             }
             // refresh qty post in blogs
             $blog_model->recalculate($blog_id);
             $this->redirect(array('blog' => $blog_id));
         } else {
             $this->view->assign('messages', $validate_massages);
             $draft_data = $settings;
         }
     }
     $colors = $this->getConfig()->getColors();
     $icons = $this->getConfig()->getIcons();
     if ($blog_id) {
         if (!($blog = $blog_model->search(array('blog' => $blog_id), array('link' => false))->fetchSearchItem())) {
             throw new waException(_w('Blog not found'), 404);
         }
         $blog['other_settlements'] = blogBlogModel::getPureSettlements($blog);
         $blog['settlement'] = array_shift($blog['other_settlements']);
     } else {
         $blog = array('id' => false, 'name' => '', 'status' => blogBlogModel::STATUS_PUBLIC, 'icon' => current($icons), 'color' => current($colors), 'url' => false);
         $blogs = array($blog);
         $blogs = $blog_model->prepareView($blogs, array('link' => false));
         $blog = array_shift($blogs);
         $blog['other_settlements'] = blogBlogModel::getPureSettlements($blog);
         $blog['settlement'] = array_shift($blog['other_settlements']);
     }
     $this->getResponse()->setTitle($blog_id ? trim(sprintf(_w('%s settings'), $blog['name'])) : _w('New blog'));
     $blog = !$draft_data ? $blog : array_merge($blog, $draft_data);
     $posts_total_count = 0;
     if ($blog_id) {
         $post_model = new blogPostModel();
         $posts_total_count = $post_model->countByField('blog_id', $blog_id);
         if ($posts_total_count) {
             $blog_model = new blogBlogModel();
             $blogs = $blog_model->getAvailable($this->getUser());
             $this->view->assign('blogs', $blogs);
         }
     }
     /**
      * Backend blog settings
      * UI hook allow extends backend blog settings page
      * @event backend_blog_edit
      * @param array[string]mixed $blog Blog data
      * @param array['id']int $blog['id'] Blog ID
      * @return array[string][string]string $return['%plugin_id%']['settings'] Blog extra settings html fields
      */
     $this->view->assign('backend_blog_edit', wa()->event('backend_blog_edit', $blog));
     $this->view->assign('posts_total_count', $posts_total_count);
     $this->view->assign('blog_id', $blog_id);
     $this->view->assign('blog', $blog);
     $this->view->assign('colors', $colors);
     $this->view->assign('icons', $icons);
 }
 public function execute()
 {
     $this->post_id = max(0, $this->getRequest()->get('id', 0, waRequest::TYPE_INT));
     $this->parent_id = max(0, $this->getRequest()->post('parent', 0, waRequest::TYPE_INT));
     $comment_model = new blogCommentModel();
     $post_model = new blogPostModel();
     /**
      *
      * Parent comment data
      * @var array
      */
     $parent = null;
     $stream = false;
     //find comment parent
     if ($this->parent_id && ($parent = $comment_model->getById($this->parent_id))) {
         if ($this->post_id && $this->post_id != $parent['post_id']) {
             throw new waRightsException(_w('Access denied'));
         }
         if (!$this->post_id) {
             $stream = true;
         }
         $this->post_id = $parent['post_id'];
     } else {
         $this->parent_id = 0;
     }
     //find post
     if (!$this->post_id || !($post = $post_model->getBlogPost($this->post_id))) {
         throw new waException(_w('Post not found'), 404);
     }
     $contact_id = $this->getUser()->getId();
     #check rights
     $rights = blogHelper::checkRights($post['blog_id'], $contact_id, blogRightConfig::RIGHT_READ);
     //check comment mode
     if (!$post['comments_allowed']) {
         throw new waException(_w("Isn't allowed comment to this post"));
     }
     $comment = array('blog_id' => $post['blog_id'], 'post_id' => $this->post_id, 'contact_id' => $contact_id, 'text' => $this->getRequest()->post('text'), 'auth_provider' => blogCommentModel::AUTH_USER);
     $this->errors += $comment_model->validate($comment);
     if (count($this->errors) > 0) {
         return;
     }
     $id = $comment_model->add($comment, $this->parent_id);
     $this->logAction('comment_add', $id);
     $comment = $comment_model->getById($id);
     //$comment['new'] = false;
     $comment['parent'] = $this->parent_id;
     if ($stream) {
         $comment['parent_text'] = $parent ? $parent['text'] : null;
         $comment['parent_status'] = $parent ? $parent['status'] : null;
     } else {
         $count = $comment_model->getCount($post['blog_id'], $this->post_id);
         $this->response['count_str'] = $count . " " . _w('comment', 'comments', $count);
     }
     $comment['rights'] = $rights;
     $comment['post'] =& $post;
     $post['comments'] = $comment_model->prepareView(array($comment), array('photo_url_20'));
     blogHelper::extendRights($post['comments'], array(), $contact_id);
     if ($stream) {
         $posts = array($this->post_id => &$post);
         $blog_model = new blogBlogModel();
         $extend_data = array('blog' => $blog_model->search(array('id' => $this->post_id))->fetchSearchAll());
         $post_model->prepareView($posts, array('link' => true), $extend_data);
     } else {
         unset($comment['post']);
     }
     $view = wa()->getView();
     $view->assign('post', $post);
     $view->assign('contact_rights', $this->getUser()->getRights('contacts', 'backend'));
     $template = $view->fetch('templates/actions/post/include.comments.html');
     $this->getResponse()->addHeader('Content-type', 'application/json');
     $this->response['template'] = $template;
 }