/**
  * getComments($project=false)
  * @@@ ToDo:
  * the following function should be moved to Comment-class
  */
 function getComments($args = array())
 {
     global $auth;
     $prefix = confGet('DB_TABLE_PREFIX');
     ### default params ###
     $order_by = 'name';
     $visible_only = true;
     # use project rights settings
     $alive_only = true;
     # ignore deleted
     $on_task = 0;
     # only project-tasks by default
     $limit = NULL;
     # limit number of results
     ### filter params ###
     if ($args) {
         foreach ($args as $key => $value) {
             if (!isset(${$key}) && !is_null(${$key}) && !${$key} === "") {
                 trigger_error("unknown parameter", E_USER_NOTICE);
             } else {
                 ${$key} = $value;
             }
         }
     }
     $str_parent_task = "";
     if ($on_task) {
         $str_parent_task = 'AND c.task=' . intVal($on_task);
     } else {
         $str_parent_task = "AND c.task=0";
     }
     $str_limit = $limit ? "LIMIT " . intval($limit) . ",0" : '';
     require_once confGet('DIR_STREBER') . 'db/class_comment.inc.php';
     $dbh = new DB_Mysql();
     $str_is_alive = $alive_only ? 'AND i.state=' . ITEM_STATE_OK : '';
     if ($visible_only) {
         $str_query = "SELECT i.*, c.* from {$prefix}item i, {$prefix}comment c, {$prefix}projectperson upp\r\n            WHERE\r\n                    upp.person = {$auth->cur_user->id}\r\n                AND upp.project = {$this->id}\r\n                AND upp.state = 1\r\n\r\n                AND i.type = '" . ITEM_COMMENT . "'\r\n                AND i.project = {$this->id}\r\n                {$str_is_alive}\r\n                AND ( i.pub_level >= upp.level_view\r\n                      OR\r\n                      i.created_by = {$auth->cur_user->id}\r\n                )\r\n\r\n                AND c.id = i.id\r\n                {$str_parent_task}\r\n\r\n            " . getOrderByString($order_by, 'i.created') . "\r\n            {$str_limit}";
     } else {
         $str_query = "SELECT i.*, c.* from {$prefix}item i, {$prefix}comment c\r\n            WHERE\r\n                    i.type = '" . ITEM_COMMENT . "'\r\n                AND i.project = {$this->id}\r\n                {$str_is_alive}\r\n\r\n                AND c.id = i.id\r\n                {$str_parent_task}\r\n\r\n            " . getOrderByString($order_by, 'i.created') . "\r\n            {$str_limit}";
     }
     $sth = $dbh->prepare($str_query);
     $sth->execute("", 1);
     $tmp = $sth->fetchall_assoc();
     $comments = array();
     foreach ($tmp as $n) {
         $comment = new Comment($n);
         $comments[] = $comment;
     }
     ### sort hierarchical ###
     /**
      * this is the second version for hierrachically sorting the comment tree.
      * It's working in two linear passes and a recursive roll out function.
      * For sorting this list correctly, the sorted object need to have a children-attribute.
      * Since the last recursive function is type independent, it can also be used
      * for other lists (like tasks).
      *
      * - The original flat list needs to be presorted, e.g. by creation date
      * - The hierarchy is flattened, if the parent objects are not part of the list.
      */
     $dict_id_comment = array();
     $dummy = new Comment(array('id' => 0));
     $dict_id_dict = array();
     # zero id item as root
     ### 1st pass: build dict for all ids ###
     foreach ($comments as $c) {
         $c->children = array(1 => 2);
         $dict_id_dict[$c->id] = $c;
         $dict_id_dict[$c->id]->children = array();
     }
     ### 2nd pass: build up tree structure ###
     foreach ($dict_id_dict as $id => $c) {
         if (isset($dict_id_dict[$c->comment])) {
             $dict_id_dict[$c->comment]->children[$c->id] = $c;
         } else {
             $dict_id_dict[0]->children[$c->id] = $c;
         }
     }
     ### 3rd pass: roll out tree
     $list = array();
     if (isset($dict_id_dict[0]->children)) {
         foreach ($dict_id_dict[0]->children as $c) {
             sortObjectsRecursively($c, $list);
         }
     }
     return $list;
 }
Exemple #2
0
/**
* Rollout a tree structure in a list
*
* - This function is used for comments, but it is object independant, as long as:
*   Objects have children and level attributes
*
* @params:
* - $obj_with_children - reference to object (e.g. of type Comment)
* - $list - reference to resulting, flat list of objects
* - $level recursion depth
*/
function sortObjectsRecursively(&$obj_with_children, &$list, $level = 0)
{
    $obj_with_children->level = $level;
    $list[] = $obj_with_children;
    foreach ($obj_with_children->children as $id => $child) {
        if ($child->id) {
            sortObjectsRecursively($child, $list, $level + 1);
        }
    }
    return $list;
}