Beispiel #1
0
 /**
  * Get a list or count of comments
  *
  * @param      string  $rtrn    Data format to return
  * @param      array   $filters Filters to apply to data fetch
  * @param      boolean $clear   Clear cached data?
  * @return     mixed
  */
 public function replies($rtrn = 'list', $filters = array(), $clear = false)
 {
     if (!isset($filters['parent']) && $this->exists()) {
         $filters['parent'] = $this->get('id');
     }
     if (!isset($filters['item_type'])) {
         $filters['item_type'] = $this->get('item_type');
     }
     if (!isset($filters['item_id'])) {
         $filters['item_id'] = $this->get('item_id');
     }
     if (!isset($filters['state'])) {
         $filters['state'] = array(1, 3);
     }
     switch (strtolower($rtrn)) {
         case 'count':
             if (!is_numeric($this->get('replies.count')) || $clear) {
                 $total = 0;
                 foreach ($this->replies() as $com) {
                     // Increment for this comment
                     $total++;
                     // Add the comment's replies to the total
                     $total = $total + $com->replies('count');
                 }
                 $this->set('replies.count', $total);
             }
             return $this->get('replies.count');
             break;
         case 'list':
         case 'results':
         default:
             if (!$this->get('replies') instanceof ItemList || $clear) {
                 $results = $this->get('replies');
                 if (!$results) {
                     $results = $this->_tbl->find($filters);
                     if ($results) {
                         $children = array(0 => array());
                         $levellimit = $filters['limit'] == 0 ? 500 : $filters['limit'];
                         foreach ($results as $v) {
                             $v = new Comment($v);
                             $pt = $v->get('parent');
                             $list = @$children[$pt] ? $children[$pt] : array();
                             array_push($list, $v);
                             $children[$pt] = $list;
                         }
                         $results = $this->_treeRecurse($children[0], $children);
                     } else {
                         $results = array();
                     }
                 }
                 $this->set('replies', new ItemList($results));
             }
             return $this->get('replies');
             break;
     }
 }