/**
  * Return activities of all active projects by user
  *
  * @param User $user
  * @param integer $count
  * @return array
  */
 function findActiveProjectsActivitiesByUser($user, $count = 30)
 {
     $type_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE));
     if ($type_filter) {
         $objects_table = TABLE_PREFIX . 'project_objects';
         $logs_table = TABLE_PREFIX . 'activity_logs';
         $count = (int) $count;
         if ($count < 1) {
             $count = 30;
         }
         // if
         return ActivityLogs::findBySQL("SELECT {$logs_table}.* FROM {$logs_table}, {$objects_table} WHERE {$logs_table}.object_id = {$objects_table}.id AND {$type_filter} AND {$objects_table}.state >= ? AND {$objects_table}.visibility >= ? ORDER BY {$logs_table}.created_on DESC LIMIT 0, {$count}", array(STATE_DELETED, $user->getVisibility()));
     } else {
         return null;
     }
     // if
 }
 /**
  * Return day data for active projects
  *
  * @param User $user
  * @param DateValue $day
  * @return array
  */
 function getActiveProjectsDayData($user, $day)
 {
     $types = get_completable_project_object_types();
     $filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE), $types);
     if ($filter) {
         $filter .= db_prepare_string(' AND (state >= ? AND visibility >= ?)', array(STATE_VISIBLE, $user->getVisibility()));
         return Calendar::getDayData($day, $filter);
     }
     // if
     return null;
 }
 /**
  * Paginate trashed objects
  *
  * @param User $user
  * @param integer $page
  * @param integer $per_page
  * @return null
  */
 function paginateTrashed($user, $page = 1, $per_page = 30)
 {
     if ($user->isAdministrator() || $user->isProjectManager()) {
         return ProjectObjects::paginate(array('conditions' => array("state = ? AND visibility >= ?", STATE_DELETED, $user->getVisibility()), 'order' => 'updated_on'), $page, $per_page);
     } else {
         $type_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE, PROJECT_STATUS_PAUSED, PROJECT_STATUS_CANCELED, PROJECT_STATUS_COMPLETED));
         if ($type_filter) {
             return ProjectObjects::paginate(array('conditions' => array($type_filter . ' AND state = ? AND visibility >= ?', STATE_DELETED, $user->getVisibility()), 'order' => 'updated_on'), $page, $per_page);
         } else {
             return array(null, new Pager(1, 0, $per_page));
         }
         // if
     }
     // if
 }
 function search($search_for, $type, $user, $page = 1, $per_page = 30, $search_object_type = '', $search_under_project_id = '', $datesort = '')
 {
     //EOF:mod 20120711
     $page = (int) $page;
     $per_page = (int) $per_page;
     $search_index_table = TABLE_PREFIX . 'search_index';
     $offset = ($page - 1) * $per_page;
     // Search in projects
     if ($type == 'ProjectObject') {
         $type_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE, PROJECT_STATUS_PAUSED, PROJECT_STATUS_COMPLETED, PROJECT_STATUS_CANCELED));
         if (empty($type_filter)) {
             return array(null, new Pager(1, 0, $per_page));
         }
         // if
         if (strlen($search_for) <= 2) {
             return array(null, new Pager(1, 0, $per_page));
         }
         //BOF:mod 20111102
         // $search_for = str_replace(' ', '% %', $search_for);
         //EOF:mod 20111102
         $project_objects_table = TABLE_PREFIX . 'project_objects';
         //$total_items = (integer) array_var(db_execute_one("SELECT COUNT($project_objects_table.id) AS 'row_count' FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ?", $search_for, $type, STATE_VISIBLE, $user->getVisibility()), 'row_count');
         /*if (empty($search_object_type)){
           	$total_items = (integer) array_var(db_execute_one("SELECT COUNT($project_objects_table.id) AS 'row_count' FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ?", $search_for, $type, STATE_VISIBLE, $user->getVisibility()), 'row_count');
           } else {
           	$total_items = (integer) array_var(db_execute_one("SELECT COUNT($project_objects_table.id) AS 'row_count' FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ? AND $project_objects_table.type = ?", $search_for, $type, STATE_VISIBLE, $user->getVisibility(), $search_object_type), 'row_count');
           }*/
         $complete_str = '';
         if ($_GET['complete'] != '1') {
             $complete_str = " and healingcrystals_project_objects.completed_on is null and (healingcrystals_project_objects.completed_by_id is null or healingcrystals_project_objects.completed_by_id='0') and healingcrystals_project_objects.boolean_field_1 is null ";
         }
         $link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
         mysql_select_db(DB_NAME);
         $query_main = "(select healingcrystals_sort_order_for_search.sort_order, if(healingcrystals_project_objects.completed_on is null, '0', '1'), '0' as new_order, healingcrystals_project_objects.* \n\t\t  \t\t from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " \n\t\t\t\t and (healingcrystals_project_objects.name is not null) \n\t\t\t\t and (healingcrystals_project_objects.name like '%" . addslashes($search_for) . "%') \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )\n\t\t\t\t UNION \n\t\t\t\t (select healingcrystals_sort_order_for_search.sort_order, if(healingcrystals_project_objects.completed_on is null, '0', '1'), '0' as new_order, healingcrystals_project_objects.* \n\t\t  \t\t from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " \n\t\t\t\t and (healingcrystals_project_objects.body like '%" . addslashes($search_for) . "%') and \n\t\t\t\t (healingcrystals_project_objects.name is null or healingcrystals_project_objects.name='') \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " ) ";
         $count_query_main = "(select * \n\t\t  \t\t from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " and (healingcrystals_project_objects.name is not null) \n\t\t\t\t and (healingcrystals_project_objects.name like '%" . addslashes($search_for) . "%') \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )\n\t\t\t\t UNION \n\t\t\t\t (select * from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " \n\t\t\t\t and (healingcrystals_project_objects.body like '%" . addslashes($search_for) . "%')  and \n\t\t\t\t (healingcrystals_project_objects.name is null or healingcrystals_project_objects.name='') \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " ) ";
         //BOF-20120216
         if (strpos($search_for, ' ') !== false) {
             $all_parts_in_name_string = '';
             $all_parts_in_body_string = '';
             $few_parts_in_name_string = '';
             $string_parts = explode(' ', $search_for);
             $query_parts_name = array();
             $query_parts_description = array();
             foreach ($string_parts as $part) {
                 $query_parts_name[] = "healingcrystals_project_objects.name like '%" . addslashes($part) . "%'";
                 $query_parts_description[] = "healingcrystals_project_objects.body like '%" . addslashes($part) . "%'";
             }
             $all_parts_in_name_string = implode(" and ", $query_parts_name);
             $all_parts_in_body_string = implode(" and ", $query_parts_description);
             $few_parts_in_name_string = implode(" or ", $query_parts_name);
             $few_parts_in_body_string = implode(" or ", $query_parts_description);
             $query_main = "(select distinct(healingcrystals_project_objects.id), healingcrystals_sort_order_for_search.sort_order, if(healingcrystals_project_objects.completed_on is null, '0', '1'), if(healingcrystals_project_objects.name like '%" . addslashes($search_for) . "%','1', if(" . $all_parts_in_name_string . ",'3',if(" . $few_parts_in_name_string . ",'5','99'))) as new_order, healingcrystals_project_objects.* \n\t\t  \t\t from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " \n\t\t\t\t and (healingcrystals_project_objects.name is not null) \n\t\t\t\t and ( (healingcrystals_project_objects.name like '%" . addslashes($search_for) . "%') or ( " . $all_parts_in_name_string . " )  or ( " . $few_parts_in_name_string . " ) ) \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )\n\t\t\t\t UNION \n\t\t\t\t (select distinct(healingcrystals_project_objects.id), healingcrystals_sort_order_for_search.sort_order, if(healingcrystals_project_objects.completed_on is null, '0', '1'), if(healingcrystals_project_objects.body like '%" . addslashes($search_for) . "%','2', if(" . $all_parts_in_body_string . ",'4',if(" . $few_parts_in_body_string . ",'6','99'))) as new_order, healingcrystals_project_objects.* \n\t\t  \t\t from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " \n\t\t\t\t and ( (healingcrystals_project_objects.body like '%" . addslashes($search_for) . "%') or ( " . $all_parts_in_body_string . " ) or ( " . $few_parts_in_body_string . " ) ) \n\t\t\t\t and (healingcrystals_project_objects.name is null or healingcrystals_project_objects.name='') \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )";
             $count_query_main = "(select * from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . " and (healingcrystals_project_objects.name is not null) \n\t\t\t\t and ( (healingcrystals_project_objects.name like '%" . addslashes($search_for) . "%' ) or ( " . $all_parts_in_name_string . " ) or ( " . $few_parts_in_name_string . " ) ) \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )\n\t\t\t\t UNION \n\t\t\t\t (select * from healingcrystals_project_objects " . (!empty($search_under_project_id) ? " inner join healingcrystals_projects on (healingcrystals_project_objects.project_id=healingcrystals_projects.id and  healingcrystals_project_objects.project_id='" . (int) $search_under_project_id . "') " : " ") . " inner join healingcrystals_search_index on healingcrystals_project_objects.id=healingcrystals_search_index.object_id \n\t\t  \t\t left join healingcrystals_sort_order_for_search on healingcrystals_sort_order_for_search.type=healingcrystals_project_objects.type \n\t\t\t\t where " . $type_filter . $complete_str . "\n\t\t\t\t and ( (healingcrystals_project_objects.body like '%" . addslashes($search_for) . "%' ) or ( " . $all_parts_in_body_string . " ) or ( " . $few_parts_in_body_string . " ) ) \n\t\t\t\t and (healingcrystals_project_objects.name is null or healingcrystals_project_objects.name='')  \n\t\t\t\t and healingcrystals_search_index.type='" . $type . "' \n\t\t\t\t and healingcrystals_project_objects.state='" . STATE_VISIBLE . "' " . (empty($search_object_type) ? "" : " and healingcrystals_project_objects.type='" . $search_object_type . "' ") . " )";
         }
         $result = mysql_query($query_main, $link);
         $count_result = mysql_query($count_query_main, $link);
         $count = mysql_num_rows($count_result);
         $total_items = mysql_num_rows($result);
         //mysql_query("insert into testing (content, date_added) values ('" . mysql_real_escape_string($query_main) . "', now())");
         if ($total_items) {
             $rows = array();
             $items = array();
             //$items = ProjectObjects::findBySQL("SELECT $project_objects_table.* FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ? LIMIT $offset, $per_page", array($search_for, $type, STATE_VISIBLE, $user->getVisibility()));
             $ids = array();
             //BOF:mod 20110706 ticketid222
             /*
             //EOF:mod 20110706 ticketid222
             $query = $query_main . " order by 2, 1 LIMIT " . $offset . " ," . $per_page;
             //BOF:mod 20110706 ticketid222
             */
             //BOF:mod 20120711
             if (empty($datesort)) {
                 //EOF:mod 20120711
                 $query = $query_main . " order by new_order ASC, 2, 1, created_on desc LIMIT " . $offset . " ," . $per_page;
                 //BOF:mod 20120711
             } elseif ($datesort == 'a') {
                 $query = $query_main . " order by created_on, new_order ASC, 2, 1 LIMIT " . $offset . " ," . $per_page;
             } elseif ($datesort == 'd') {
                 $query = $query_main . " order by created_on desc, new_order ASC, 2, 1 LIMIT " . $offset . " ," . $per_page;
             }
             //EOF:mod 20120711
             //EOF:mod 20110706 ticketid222
             $result = mysql_query($query, $link);
             while ($info = mysql_fetch_assoc($result)) {
                 $ids[] = $info['id'];
                 $rows[] = $info;
             }
             foreach ($rows as $row) {
                 $item_class = array_var($row, 'type');
                 $item = new $item_class();
                 $item->loadFromRow($row);
                 $add_item = true;
                 if ($_GET['complete'] != '1') {
                     if ($item->getParentType() == 'Page') {
                         $temp_page = new Page($item->getParentId());
                         $is_archived = $temp_page->getIsArchived();
                         if ($is_archived) {
                             $add_item = false;
                         }
                     }
                     if ($add_item) {
                         $temp_obj = new ProjectObject($item->getParentId());
                         if ($temp_obj->isCompleted()) {
                             $add_item = false;
                         }
                     }
                 }
                 if ($add_item) {
                     $items[] = $item;
                 }
             }
             if (empty($search_object_type)) {
                 //$items = ProjectObjects::findBySQL("SELECT $project_objects_table.* FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ? LIMIT $offset, $per_page", array($search_for, $type, STATE_VISIBLE, $user->getVisibility()));
             } else {
                 //$items = ProjectObjects::findBySQL("SELECT $project_objects_table.* FROM $project_objects_table, $search_index_table WHERE $type_filter AND MATCH ($search_index_table.content) AGAINST (? IN BOOLEAN MODE) AND $project_objects_table.id = $search_index_table.object_id AND $search_index_table.type = ? AND state >= ? AND visibility >= ? AND $project_objects_table.type = ? LIMIT $offset, $per_page", array($search_for, $type, STATE_VISIBLE, $user->getVisibility(), $search_object_type));
             }
             //mysql_query("insert into healingcrystals_testing (query, fired_at) values ('" . mysql_real_escape_string($query) . "', now())", $link);
         } else {
             $items = null;
         }
         // if
         mysql_close($link);
         return array($items, new Pager($page, $total_items, $per_page), $count);
         // Search for projects
     } elseif ($type == 'Project') {
         $project_ids = Projects::findProjectIdsByUser($user, null, true);
         if (!is_foreachable($project_ids)) {
             return array(null, new Pager(1, 0, $per_page));
         }
         // if
         $projects_table = TABLE_PREFIX . 'projects';
         $total_items = (int) array_var(db_execute_one("SELECT COUNT({$projects_table}.id) AS 'row_count' FROM {$projects_table}, {$search_index_table} WHERE {$projects_table}.id IN (?) AND MATCH ({$search_index_table}.content) AGAINST (? IN BOOLEAN MODE) AND {$projects_table}.id = {$search_index_table}.object_id AND {$search_index_table}.type = ?", $project_ids, $search_for, 'Project'), 'row_count');
         if ($total_items) {
             $items = Projects::findBySQL("SELECT * FROM {$projects_table}, {$search_index_table} WHERE {$projects_table}.id IN (?) AND MATCH ({$search_index_table}.content) AGAINST (? IN BOOLEAN MODE) AND {$projects_table}.id = {$search_index_table}.object_id AND {$search_index_table}.type = ? LIMIT {$offset}, {$per_page}", array($project_ids, $search_for, 'Project'));
         } else {
             $items = null;
         }
         // if
         return array($items, new Pager($page, $total_items, $per_page));
         // Search for users
     } elseif ($type == 'User') {
         $user_ids = $user->visibleUserIds();
         if (!is_foreachable($user_ids)) {
             return array(null, new Pager(1, 0, $per_page));
         }
         // if
         $users_table = TABLE_PREFIX . 'users';
         $total_items = (int) array_var(db_execute_one("SELECT COUNT({$users_table}.id) AS 'row_count' FROM {$users_table}, {$search_index_table} WHERE {$users_table}.id IN (?) AND MATCH ({$search_index_table}.content) AGAINST (? IN BOOLEAN MODE) AND {$users_table}.id = {$search_index_table}.object_id AND {$search_index_table}.type = ?", $user_ids, $search_for, 'User'), 'row_count');
         if ($total_items) {
             $items = Users::findBySQL("SELECT * FROM {$users_table}, {$search_index_table} WHERE {$users_table}.id IN (?) AND MATCH ({$search_index_table}.content) AGAINST (? IN BOOLEAN MODE) AND {$users_table}.id = {$search_index_table}.object_id AND {$search_index_table}.type = ? LIMIT {$offset}, {$per_page}", array($user_ids, $search_for, 'User'));
         } else {
             $items = null;
         }
         // if
         return array($items, new Pager($page, $total_items, $per_page));
         // Unknown search type
     } else {
         return array(null, new Pager(1, 0, $per_page));
     }
     // if
 }
 /**
  * Prepare conditions based on filter settings
  *
  * @param User $user
  * @return string
  */
 function prepareConditions($user)
 {
     $project_objects_table = TABLE_PREFIX . 'project_objects';
     $assignments_table = TABLE_PREFIX . 'assignments';
     $completable_types = get_completable_project_object_types();
     $conditions = array();
     // Selected projects filter
     if ($this->getProjectFilter() == PROJECT_FILTER_SELECTED) {
         $project_ids = $this->getProjectFilterData();
         if ($project_ids) {
             $conditions[] = db_prepare_string("({$project_objects_table}.project_id IN (?))", array($project_ids));
             $types_filter = ProjectUsers::getVisibleTypesFilter($user, null, $completable_types);
             if ($types_filter) {
                 $conditions[] = $types_filter;
             } else {
                 return false;
                 // No access to any of the projects?
             }
             // if
         }
         // if
     }
     // if
     // All projects
     if (count($conditions) == 0) {
         $types_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE), $completable_types);
         if ($types_filter) {
             $conditions[] = $types_filter;
         } else {
             return false;
             // No access to any of the projects?
         }
         // if
     }
     // if
     // User filter
     switch ($this->getUserFilter()) {
         // Not assigned to anyone
         case USER_FILTER_NOT_ASSIGNED:
             $user_id = $user->getId();
             $conditions[] = "({$assignments_table}.user_id IS NULL)";
             break;
             // Logged user
         // Logged user
         case USER_FILTER_LOGGED_USER:
             $user_id = $user->getId();
             $conditions[] = "({$assignments_table}.user_id = {$user_id})";
             break;
             // Logged user is responsible
         // Logged user is responsible
         case USER_FILTER_LOGGED_USER_RESPONSIBLE:
             $user_id = $user->getId();
             $conditions[] = "({$assignments_table}.user_id = {$user_id}) AND ({$assignments_table}.is_owner = 1)";
             break;
             // All members of a specific company
         // All members of a specific company
         case USER_FILTER_COMPANY:
             $visible_user_ids = $user->visibleUserIds();
             if (!is_foreachable($visible_user_ids)) {
                 return false;
             }
             // if
             $company_id = $this->getUserFilterData();
             if ($company_id) {
                 $company = Companies::findById($company_id);
                 if (instance_of($company, 'Company')) {
                     $user_ids = Users::findUserIdsByCompany($company);
                     if (is_foreachable($user_ids)) {
                         foreach ($user_ids as $k => $v) {
                             if (!in_array($v, $visible_user_ids)) {
                                 unset($user_ids[$k]);
                             }
                             // if
                         }
                         // if
                         if (count($user_ids) > 0) {
                             $imploded = implode(', ', $user_ids);
                             $conditions[] = "({$assignments_table}.user_id IN ({$imploded}))";
                         } else {
                             return false;
                         }
                         // if
                     }
                     // if
                 }
                 // if
             }
             // if
             break;
             // Selected users
         // Selected users
         case USER_FILTER_SELECTED:
             $visible_user_ids = $user->visibleUserIds();
             if (!is_foreachable($visible_user_ids)) {
                 return false;
             }
             // if
             $user_ids = $this->getUserFilterData();
             if (is_foreachable($user_ids)) {
                 foreach ($user_ids as $k => $v) {
                     if (!in_array($v, $visible_user_ids)) {
                         unset($user_ids[$k]);
                     }
                     // if
                 }
                 // foreach
                 if (count($user_ids) > 0) {
                     $imploded = implode(', ', $user_ids);
                     $conditions[] = "({$assignments_table}.user_id IN ({$imploded}))";
                 } else {
                     return false;
                 }
                 // if
             }
             // if
             break;
     }
     // switch
     $today = new DateValue(time() + get_user_gmt_offset($user));
     // Calculate user timezone when determining today
     switch ($this->getDateFilter()) {
         // List late assignments
         case DATE_FILTER_LATE:
             $today_str = db_escape($today->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on < {$today_str})";
             break;
             // List today assignments
         // List today assignments
         case DATE_FILTER_TODAY:
             $today_str = db_escape($today->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on = {$today_str})";
             break;
             // List tomorrow assignments
         // List tomorrow assignments
         case DATE_FILTER_TOMORROW:
             $tomorrow = $today->advance(86400, false);
             $tomorrow_str = db_escape($tomorrow->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on = {$tomorrow_str})";
             break;
             // List this week assignments
         // List this week assignments
         case DATE_FILTER_THIS_WEEK:
             $first_day_sunday = UserConfigOptions::getValue('time_first_week_day', $user) == 0;
             $week_start = $today->beginningOfWeek($first_day_sunday);
             $week_end = $today->endOfWeek($first_day_sunday);
             $week_start_str = db_escape($week_start->toMySQL());
             $week_end_str = db_escape($week_end->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on >= {$week_start_str} AND {$project_objects_table}.due_on <= {$week_end_str})";
             break;
             // List next week assignments
         // List next week assignments
         case DATE_FILTER_NEXT_WEEK:
             $first_day_sunday = UserConfigOptions::getValue('time_first_week_day', $user) == 0;
             $next_week = $today->advance(604800, false);
             $week_start = $next_week->beginningOfWeek($first_day_sunday);
             $week_end = $next_week->endOfWeek($first_day_sunday);
             $week_start_str = db_escape($week_start->toMySQL());
             $week_end_str = db_escape($week_end->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on >= {$week_start_str} AND {$project_objects_table}.due_on <= {$week_end_str})";
             break;
             // List this month assignments
         // List this month assignments
         case DATE_FILTER_THIS_MONTH:
             $month_start = DateTimeValue::beginningOfMonth($today->getMonth(), $today->getYear());
             $month_end = DateTimeValue::endOfMonth($today->getMonth(), $today->getYear());
             $month_start_str = db_escape($month_start->toMySQL());
             $month_end_str = db_escape($month_end->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on >= {$month_start_str} AND {$project_objects_table}.due_on <= {$month_end_str})";
             break;
             // List next month assignments
         // List next month assignments
         case DATE_FILTER_NEXT_MONTH:
             $month = $today->getMonth() + 1;
             $year = $today->getYear();
             if ($month == 13) {
                 $month = 1;
                 $year += 1;
             }
             // if
             $month_start = DateTimeValue::beginningOfMonth($month, $year);
             $month_end = DateTimeValue::endOfMonth($month, $year);
             $month_start_str = db_escape($month_start->toMySQL());
             $month_end_str = db_escape($month_end->toMySQL());
             $conditions[] = "({$project_objects_table}.due_on >= {$month_start_str} AND {$project_objects_table}.due_on <= {$month_end_str})";
             break;
             // Specific date
         // Specific date
         case DATE_FILTER_SELECTED_DATE:
             $date_from = $this->getDateFrom();
             if (instance_of($date_from, 'DateTimeValue')) {
                 $date_from_str = db_escape($date_from->toMySql());
                 $conditions[] = "({$project_objects_table}.due_on = {$date_from_str})";
             }
             // if
             break;
             // Specific range
         // Specific range
         case DATE_FILTER_SELECTED_RANGE:
             $date_from = $this->getDateFrom();
             $date_to = $this->getDateTo();
             if (instance_of($date_from, 'DateValue') && instance_of($date_to, 'DateValue')) {
                 $date_from_str = db_escape($date_from->toMySQL());
                 $date_to_str = db_escape($date_to->toMySQL());
                 $conditions[] = "({$project_objects_table}.due_on >= {$date_from_str} AND {$project_objects_table}.due_on <= {$date_to_str})";
             }
             // if
             break;
     }
     // switch
     // Status filter
     switch ($this->getStatusFilter()) {
         case STATUS_FILTER_ACTIVE:
             $conditions[] = "({$project_objects_table}.completed_on IS NULL)";
             break;
         case STATUS_FILTER_COMPLETED:
             $conditions[] = "({$project_objects_table}.completed_on IS NOT NULL)";
             break;
     }
     // if
     // Additional filters
     $state = STATE_VISIBLE;
     $visibility = $user->getVisibility();
     $conditions[] = "({$project_objects_table}.state >= {$state} AND {$project_objects_table}.visibility >= {$visibility})";
     return implode(' AND ', $conditions);
 }
 /**
  * Return object ID-s by user
  *
  * @param User $user
  * @return array
  */
 function findObjectIdsByUser($user)
 {
     $project_objects_table = TABLE_PREFIX . 'project_objects';
     $starred_objects_table = TABLE_PREFIX . 'starred_objects';
     if ($user->isProjectManager()) {
         $rows = db_execute_all("SELECT object_id FROM {$starred_objects_table} WHERE user_id = ?", $user->getId());
     } else {
         $type_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE, PROJECT_STATUS_PAUSED, PROJECT_STATUS_CANCELED, PROJECT_STATUS_COMPLETED));
         if ($type_filter) {
             $rows = db_execute_all("SELECT {$project_objects_table}.id AS 'object_id' FROM {$project_objects_table}, {$starred_objects_table} WHERE {$type_filter} AND {$starred_objects_table}.object_id = {$project_objects_table}.id AND {$starred_objects_table}.user_id = ? AND {$project_objects_table}.state >= ? AND {$project_objects_table}.visibility >= ? ORDER BY {$project_objects_table}.priority DESC", $user->getId(), STATE_VISIBLE, $user->getVisibility());
         } else {
             $rows = null;
         }
         // if
     }
     // if
     $result = array();
     if (is_foreachable($rows)) {
         foreach ($rows as $row) {
             $result[] = (int) $row['object_id'];
         }
         // foreach
     }
     // if
     return $result;
 }
 /**
  * Render global iCalendar feed
  *
  * @param void
  * @return null
  */
 function ical()
 {
     $filter = ProjectUsers::getVisibleTypesFilter($this->logged_user, array(PROJECT_STATUS_ACTIVE), get_completable_project_object_types());
     if ($filter) {
         $objects = ProjectObjects::find(array('conditions' => array($filter . ' AND completed_on IS NULL AND state >= ? AND visibility >= ?', STATE_VISIBLE, $this->logged_user->getVisibility()), 'order' => 'priority DESC'));
         render_icalendar(lang('Global Calendar'), $objects, true);
         die;
     } elseif ($this->request->get('subscribe')) {
         flash_error(lang('You are not able to download .ics file because you are not participating in any of the active projects at the moment'));
         $this->redirectTo('ical_subscribe');
     } else {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
 }