Example #1
0
 /**
  * Create a reply for this topic.
  *
  * The only required key in $values is 'content',
  * the logged in user's id can still be retrieved
  * through the config defined helper function(default_user_id)
  *
  * Updates reply count for the topic, if enabled.
  * Adds last_post_user_id if enabled.
  *
  * if none of the above is enabled it 'touches' the topic to update the column 'updated_at' for ordering.
  *
  * @param array $values
  * @param null|Kohana_Validation $extra_validation
  * @return Model_Quill_Reply
  * @throws Kohana_Exception
  */
 public function create_reply($values, $extra_validation = null)
 {
     // are we able to post a reply to the topic?
     if ($this->status != 'active') {
         throw new Quill_Exception_Reply_Status('You can\'t post a reply on a :status topic', array(':status' => $this->status));
     }
     if (!isset($values['user_id'])) {
         $user = Kohana::$config->load('quill.default_user_id');
         $values['user_id'] = $user();
     }
     $values['topic_id'] = $this->id;
     // save the reply
     $reply = ORM::factory('Quill_Reply')->values($values, array('topic_id', 'user_id', 'content'))->save($extra_validation, $this->category->location->count_replies || $this->category->location->record_last_post);
     // if we need to keep reply count, calculate before updating
     if ($this->category->location->count_replies == true) {
         $this->reply_count += 1;
     }
     // if we need to record the last post's user
     if ($this->category->location->record_last_post) {
         $this->last_post_user_id = $values['user_id'];
     }
     // If Kohana-Plugin-System is installed let's fire a reply event
     // and supply the reply and the user that made the reply
     if (class_exists('Plug')) {
         Plug::fire('quill.reply', array($reply, $reply->user));
     }
     $this->save();
     return $reply;
 }
Example #2
0
 /**
  * Initialise the plugin and register all events that were defined in $this->_events.
  *
  * @return Kohana_Plugin
  * @throws Kohana_Exception
  */
 public final function init()
 {
     if ($this->_init() == true) {
         if (count($this->_events) > 0) {
             foreach ($this->_events as $id => $event) {
                 $class_event = Valid::digit($id) ? str_replace('.', '_', $event) : $id;
                 Plug::listen($event, array($this, 'on_' . $class_event));
             }
         }
         return $this;
     } else {
         throw new Kohana_Exception('Failed to initialise the ":plugin" plugin', array(':plugin' => $this->info['name']));
     }
 }
Example #3
0
 /**
  * Create a topic for this category.
  *
  * Required $value keys:
  *  - user_id
  *  - title
  *  - content
  *
  * Optionally you can define the $value key 'stickied' (1 or 0) defaults to 0
  *
  * @param array $values
  * @param Kohana_Validation|null $extra_validation
  * @return Model_Quill_Topic
  */
 public function create_topic(array $values, $extra_validation = null)
 {
     // are we able to create a new topic in this category
     if ($this->_category->status == 'closed') {
         throw new Quill_Exception_Topic_Create('You can not create a topic in a closed category.');
     }
     if (!isset($values['user_id'])) {
         $user = Kohana::$config->load('quill.default_user_id');
         $values['user_id'] = $user();
     }
     $values['category_id'] = $this->_category->id;
     // this is required for ordering topics, so set it to creation time
     $values['updated_at'] = time();
     // defaults
     $values['reply_count'] = 0;
     $values['status'] = 'active';
     // optional
     if (!isset($values['stickied'])) {
         $values['stickied'] = 0;
     }
     // save the topic
     $topic = ORM::factory('Quill_Topic')->values($values, array('category_id', 'user_id', 'title', 'content', 'status', 'stickied', 'updated_at', 'reply_count'))->save($extra_validation);
     // if we're keeping track of active topic count, update it
     if ($this->_category->location->count_topics == true) {
         $this->_category->topic_count = DB::select(array(DB::expr('COUNT(*)'), 'topics'))->from('quill_topics')->where('category_id', '=', $this->_category->id)->where('status', '=', 'active')->execute()->get('topics');
         $this->_category->save();
     }
     // If Kohana-Plugin-System was installed fire a topic event
     // by supplying the topic and the user
     if (class_exists('Plug')) {
         Plug::fire('quill.topic', array($topic, $topic->user));
     }
     return $topic;
 }