/** * Render view. * * @return string */ public function content() { ob_start(); // Title if ($this->area->description) { echo $this->area->description . '<hr>'; } if ($this->area->topic_count) { // Area has topics $last_topic = $this->area->last_topic(); $last_poster = $last_topic->last_post()->author(); ?> <div class="media"> <div class="pull-left"> <?php echo HTML::avatar($last_poster ? $last_poster['avatar'] : null, $last_poster ? $last_poster['username'] : null, false); ?> </div> <div class="media-body"> <small class="ago"><?php echo HTML::time(Date::short_span($last_topic->last_posted, true, true), $last_topic->last_posted); ?> </small> <?php echo $last_poster ? HTML::user($last_poster) : HTML::chars($last_topic->last_poster); ?> <br> <?php echo HTML::anchor(Route::model($last_topic, '?page=last#last'), Forum::topic($last_topic), array('title' => HTML::chars($last_topic->name))); ?> <br /> </div> </div> <small class="stats muted"> <i class="icon-comments"></i> <?php echo Num::format($this->area->topic_count, 0); ?> <i class="icon-comment"></i> <?php echo Num::format($this->area->post_count, 0); ?> </small> <?php } else { // Empty area echo __('No topics yet.'); } return ob_get_clean(); }
/** * Add new forum topic for music. * * @return boolean */ public function add_forum_topic() { $forum_areas = Kohana::$config->load('music.forum_areas'); if ($forum_areas && $forum_areas[$this->type]) { $forum_area = Model_Forum_Area::factory($forum_areas[$this->type]); if ($forum_area->loaded()) { try { // Generate post $content = ''; // Cover if (Valid::url($this->cover)) { $content .= '[img]' . $this->cover . "[/img]\n\n"; } // Description $content .= $this->description . "\n\n"; if ($this->type == Model_Music_Track::TYPE_MIX && $this->tracklist) { $content .= '[b]' . __('Tracklist') . "[/b]\n"; $content .= $this->tracklist . "\n\n"; } // Tags if ($tags = $this->tags()) { $content .= '[i]' . implode(', ', $tags) . "[/i]\n\n"; } else { if (!empty($this->music)) { $content .= '[i]' . $this->music . "[/i]\n\n"; } } // Links $content .= '[url=' . URL::site(Route::model($this), true) . ']' . __('Show details') . '[/url] - '; $content .= '[url=' . URL::site(Route::model($this, 'listen'), true) . ']' . __('Listen') . '[/url]'; // Create topic $forum_topic = $forum_area->create_topic($this->name, $content, $this->author()); $forum_topic->save_post(); } catch (Kohana_Exception $e) { return false; } $this->forum_topic_id = $forum_topic->id; $this->save(); return true; } } return false; }
/** * Get group areas * * @return Model_Forum_Area[] */ public function areas() { return $this->find_related('forum_areas', DB::select_array(Model_Forum_Area::factory()->fields())->where('status', '<>', Model_Forum_Area::STATUS_HIDDEN)->order_by('sort', 'ASC')); }
<legend><?php echo __('Settings'); ?> </legend> <ul> <?php echo Form::select_wrap('access_read', array(Model_Forum_Area::READ_NORMAL => __('Everybody'), Model_Forum_Area::READ_MEMBERS => __('Members only')), $area, null, __('Read access')); ?> <?php echo Form::select_wrap('access_write', array(Model_Forum_Area::WRITE_NORMAL => __('Members'), Model_Forum_Area::WRITE_ADMINS => __('Admins only')), $area, null, __('Write access')); ?> <?php echo Form::select_wrap('type', array(Model_Forum_Area::TYPE_NORMAL => __('Normal'), Model_Forum_Area::TYPE_BIND => __('Bind, topics bound to content')), $area, null, __('Type')); ?> <?php echo Form::select_wrap('bind', array('' => __('None')) + Model_Forum_Area::get_binds(), $area, null, __('Bind config')); ?> <?php echo Form::select_wrap('status', array(Model_Forum_Area::STATUS_NORMAL => __('Normal'), Model_Forum_Area::STATUS_HIDDEN => __('Hidden')), $area, null, __('Status')); ?> </ul> </fieldset> <fieldset> <?php echo Form::submit_wrap('save', __('Save'), null, Request::back(Route::get('forum_group')->uri(), true)); ?> </fieldset> <?php echo Form::close();
/** * Edit forum topic * * @param integer $area_id * @param integer $topic_id * * @throws Model_Exception invalid area, invalid topic * @throws InvalidArgumentException missing area and topic */ protected function _edit_topic($area_id = null, $topic_id = null) { $this->history = false; $this->view = new View_Page(); if ($area_id && !$topic_id) { // Start new topic $mode = View_Forum_PostEdit::NEW_TOPIC; /** @var Model_Forum_Private_Area|Model_Forum_Area $area */ $area = $this->private ? Model_Forum_Private_Area::factory($area_id) : Model_Forum_Area::factory($area_id); if (!$area->loaded()) { throw new Model_Exception($area, $area_id); } Permission::required($area, Model_Forum_Area::PERMISSION_POST, self::$user); $this->view->title = HTML::chars($area->name); if ($this->private) { $topic = new Model_Forum_Private_Topic(); $post = new Model_Forum_Private_Post(); $cancel = Route::url('forum_area', array('id' => 'private', 'action' => '')); $recipients = array(); } else { $topic = new Model_Forum_Topic(); $post = new Model_Forum_Post(); $cancel = Route::model($area); } } else { if ($topic_id) { // Edit old topic $mode = View_Forum_PostEdit::EDIT_TOPIC; /** @var Model_Forum_Private_Topic|Model_Forum_Topic $topic */ $topic = $this->private ? Model_Forum_Private_Topic::factory($topic_id) : Model_Forum_Topic::factory($topic_id); if (!$topic->loaded()) { throw new Model_Exception($topic, $topic_id); } Permission::required($topic, Model_Forum_Topic::PERMISSION_UPDATE, self::$user); // Build recipients list if ($this->private) { $recipients = $topic->find_recipient_names(); } $this->view->title_html = Forum::topic($topic); $cancel = Route::model($topic); // Set actions if (Permission::has($topic, Model_Forum_Topic::PERMISSION_DELETE, self::$user)) { $this->view->actions[] = array('link' => Route::model($topic, 'delete') . '?' . Security::csrf_query(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete topic'), 'class' => 'btn btn-danger topic-delete'); } } else { throw new InvalidArgumentException('Topic and area missing'); } } $errors = array(); if ($_POST && Security::csrf_valid()) { // Get recipients if ($this->private) { $post_recipients = array(); foreach (explode(',', Arr::get_once($_POST, 'recipients')) as $recipient) { if ($user = Model_User::find_user_light(trim($recipient))) { $post_recipients[$user['id']] = $user['username']; } } // Make sure author is included $post_recipients[self::$user->id] = self::$user->username; } if (isset($post)) { // New topic $post->post = $_POST['post']; $post->forum_area_id = $area->id; $post->author_id = self::$user->id; $post->author_name = self::$user->username; $post->author_ip = Request::$client_ip; $post->author_host = Request::host_name(); $post->created = time(); try { $post->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } $topic->author_id = self::$user->id; $topic->author_name = self::$user->username; $topic->name = $_POST['name']; $topic->forum_area_id = $area->id; $topic->created = time(); try { $topic->is_valid(); } catch (Validation_Exception $e) { $errors += $e->array->errors('validate'); } // If no errors found, save models if (empty($errors)) { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } // Post $post->forum_topic_id = $topic->id; $post->save(); // Topic $topic->first_post_id = $topic->last_post_id = $post->id; $topic->last_poster = self::$user->username; $topic->last_posted = time(); $topic->post_count = 1; $topic->save(); // Area, only public forums if (!$this->private) { $area->last_topic_id = $topic->id; $area->post_count++; $area->topic_count++; $area->save(); } // User self::$user->post_count++; self::$user->save(); // News feed if (!$this->private) { NewsfeedItem_Forum::topic(self::$user, $topic); } $this->request->redirect(Route::model($topic)); } isset($post_recipients) and $recipients = $post_recipients; } else { // Old topic $topic->set_fields(Arr::intersect($_POST, array('name', 'status', 'sticky'))); try { $topic->save(); // Recipients if ($this->private) { $topic->set_recipients($post_recipients); } $this->request->redirect(Route::model($topic)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validate'); } } } $form['errors'] = $errors; $section = $this->section_post_edit($mode, isset($post) ? $post : null); $section->forum_topic = $topic; $section->errors = $errors; $section->cancel = $cancel; $section->recipients = isset($recipients) ? implode(', ', $recipients) : null; $this->view->add(View_Page::COLUMN_MAIN, $section); }
/** * Load topic by bound model * * @static * @param Model $bind_model Bound model * @param string $bind_name Bind config if multiple binds per model * @return Model_Forum_Topic */ public static function find_by_bind(Model $bind_model, $bind_name = null) { $model = Model::model_name($bind_model); // Get correct bind config if (!$bind_name) { foreach (Model_Forum_Area::get_binds(false) as $bind_name => $bind_config) { if ($bind_config['model'] == $model) { $config = $bind_config; break; } } } else { $config = Model_Forum_Area::get_binds($bind_name); } if ($config) { // Get area $area = Model_Forum_Area::factory(); $area = $area->load(DB::select_array($area->fields())->where('area_type', '=', Model_Forum_Area::TYPE_BIND)->where('bind', '=', $bind_name)); if ($area->loaded()) { // Get topic $topic = Model_Forum_Topic::factory(); $topic = $topic->load(DB::select_array($topic->fields())->where('forum_area_id', '=', $area->id)->where('bind_id', '=', $bind_model->id())); // If topic found, go there! if ($topic->loaded()) { return $topic; } } } return null; }
/** * Render view. * * @return string */ public function content() { ob_start(); // Build available groups list $groups = array(); foreach (Model_Forum_Group::factory()->find_all() as $_group) { $groups[$_group->id] = $_group->name; } echo Form::open(); ?> <fieldset> <?php echo Form::control_group(Form::select('forum_group_id', $groups, $this->area->id), array('forum_group_id' => __('Group')), Arr::get($this->errors, 'forum_group_id')); ?> <?php echo Form::control_group(Form::input('name', $this->area->name), array('name' => __('Name')), Arr::get($this->errors, 'name')); ?> <?php echo Form::control_group(Form::input('description', $this->area->description), array('description' => __('Description')), Arr::get($this->errors, 'description')); ?> <?php echo Form::control_group(Form::input('sort', $this->area->sort, array('class' => 'input-mini')), array('sort' => __('Sort')), Arr::get($this->errors, 'sort')); ?> </fieldset> <fieldset> <legend><?php echo __('Settings'); ?> </legend> <?php echo Form::control_group(Form::select('access_read', array(Model_Forum_Area::READ_NORMAL => __('Everybody'), Model_Forum_Area::READ_MEMBERS => __('Members only')), $this->area->access_read), array('access_read' => __('Read access')), Arr::get($this->errors, 'access_read')); ?> <?php echo Form::control_group(Form::select('access_write', array(Model_Forum_Area::WRITE_NORMAL => __('Members'), Model_Forum_Area::WRITE_ADMINS => __('Admins only')), $this->area->access_write), array('access_write' => __('Write access')), Arr::get($this->errors, 'access_write')); ?> <?php echo Form::control_group(Form::select('type', array(Model_Forum_Area::TYPE_NORMAL => __('Normal'), Model_Forum_Area::TYPE_BIND => __('Bind, topics bound to content')), $this->area->type), array('type' => __('Type')), Arr::get($this->errors, 'type')); ?> <?php echo Form::control_group(Form::select('bind', array('' => __('None')) + Model_Forum_Area::get_binds(), $this->area->bind), array('bind' => __('Bind config')), Arr::get($this->errors, 'bind')); ?> <?php echo Form::control_group(Form::select('status', array(Model_Forum_Area::STATUS_NORMAL => __('Normal'), Model_Forum_Area::STATUS_HIDDEN => __('Hidden')), $this->area->status), array('status' => __('Status')), Arr::get($this->errors, 'status')); ?> </fieldset> <fieldset class="form-actions"> <?php echo Form::button('save', __('Save'), array('type' => 'submit', 'class' => 'btn btn-success btn-large')); ?> <?php echo HTML::anchor(Request::back(Route::url('forum_group'), true), __('Cancel'), array('class' => 'cancel')); ?> <?php echo Form::csrf(); ?> </fieldset> <?php echo Form::close(); return ob_get_clean(); }
/** * Add forum topic. * * @return boolean */ public function add_forum_topic() { // Get forum area $area = Model_Forum_Area::factory()->find_by_bind('events'); if ($area->loaded()) { try { // Topic $name = $this->name . ' ' . Date::format('DMYYYY', $this->stamp_begin); if ($this->city_name) { $name .= ' @ ' . $this->city_name; } // Post $content = $this->info; if ($flyer = $this->flyer()) { $content = '[url=' . Route::model($flyer) . '][img]' . $flyer->image_url() . '[/img][/url]' . "\n\n" . $content; } $topic = $area->create_topic($name, $content, $this->author()); $topic->bind_id = $this->id; $topic->save_post(); } catch (Kohana_Exception $forum_validation) { return false; } return true; } return false; }
/** * Action: index */ public function action_index() { // Load area $area_id = $this->request->param('id'); // Private area? if ($area_id == 'private') { return $this->action_messages(); } /** @var Model_Forum_Area $area */ $area = Model_Forum_Area::factory((int) $area_id); if (!$area->loaded()) { throw new Model_Exception($area, (int) $area_id); } Permission::required($area, Model_Forum_Area::PERMISSION_READ, self::$user); // Build page $this->view = new View_Page($area->name); $this->view->tab = 'areas'; $this->view->subtitle = $area->description; // Set actions if (Permission::has($area, Model_Forum_Area::PERMISSION_POST, self::$user)) { $this->view->actions[] = array('link' => Route::model($area, 'post'), 'text' => '<i class="icon-plus-sign icon-white"></i> ' . __('New topic'), 'class' => 'btn btn-primary topic-add'); } if (Permission::has($area, Model_Forum_Area::PERMISSION_UPDATE, self::$user)) { $this->view->actions[] = array('link' => Route::model($area, 'edit', false), 'text' => '<i class="icon-edit icon-white"></i> ' . __('Edit area')); } // Pagination $pagination = $this->section_pagination($area->topic_count); $this->view->add(View_Page::COLUMN_MAIN, $pagination); // Posts $this->view->add(View_Page::COLUMN_MAIN, $this->section_topics($area->find_active_topics($pagination->offset, $pagination->items_per_page))); // Pagination $this->view->add(View_Page::COLUMN_MAIN, $pagination); $this->_side_views(); }