public function execute() { if ($target_blog = max(0, $this->getRequest()->post('blog', 0, waRequest::TYPE_INT))) { $blog_model = new blogBlogModel(); if ($blog = $blog_model->getById($target_blog)) { if ($ids = $this->getRequest()->post('id', null, waRequest::TYPE_ARRAY_INT)) { $post_model = new blogPostModel(); $comment_model = new blogCommentModel(); $this->response['moved'] = array(); foreach ($ids as $id) { try { //rights will checked for each record separately $post_model->updateItem($id, array('blog_id' => $target_blog)); $comment_model->updateByField('post_id', $id, array('blog_id' => $target_blog)); $this->response['moved'][$id] = $id; } catch (Exception $ex) { if (!isset($this->response['error'])) { $this->response['error'] = array(); } $this->response['error'][$id] = $ex->getMessage(); } } $this->response['style'] = $blog['color']; $blog_model->recalculate(); } } else { } } }
static function move($blog_id, $move_blog_id) { if ($blog_id != $move_blog_id) { $post_model = new blogPostModel(); $post_model->updateByField('blog_id', $blog_id, array('blog_id' => $move_blog_id)); $comment_model = new blogCommentModel(); $comment_model->updateByField('blog_id', $blog_id, array('blog_id' => $move_blog_id)); $blog_model = new blogBlogModel(); $blog_model->recalculate(array($blog_id, $move_blog_id)); } }
/** * Delete records from table and related data * * @param $field * @param $value * @return bool */ public function deleteByField($field, $value = null) { if (is_array($field)) { $items = $this->getByField($field, $this->id); } else { $items = $this->getByField($field, $value, $this->id); } $res = false; if ($post_ids = array_keys($items)) { /** * @event post_predelete * @param array[] int $post_ids array of post's ID * @return void */ wa()->event('post_predelete', $post_ids); $res = parent::deleteByField('id', $post_ids); if ($res) { $comment_model = new blogCommentModel(); $comment_model->deleteByField('post_id', $post_ids); $params_model = new blogPostParamsModel(); $params_model->deleteByField('post_id', $post_ids); $blog_model = new blogBlogModel(); $blogs = array(); foreach ($items as $item) { $blogs[] = $item['blog_id']; } $blogs = array_unique($blogs); $blog_model->recalculate($blogs); /** * @event post_delete * @param array[] int $post_ids array of post's ID * @return void */ wa()->event('post_delete', $post_ids); } } return $res; }
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); }