/**
  * Paginate attachments by project
  *
  * @param Project $project
  * @param User $user
  * @param integer $page
  * @param integer $per_page
  * @param integer $min_state
  * @return array
  */
 function paginateByProject($project, $user, $page = 1, $per_page = 30, $min_state = STATE_VISIBLE)
 {
     $attachments_table = TABLE_PREFIX . 'attachments';
     $project_objects_table = TABLE_PREFIX . 'project_objects';
     $type_filter = ProjectUsers::getVisibleTypesFilterByProject($user, $project);
     if ($type_filter) {
         $total = array_var(db_execute_one("SELECT COUNT({$attachments_table}.id) AS 'row_count' FROM {$attachments_table}, {$project_objects_table} WHERE {$attachments_table}.attachment_type = ? AND {$attachments_table}.parent_id = {$project_objects_table}.id AND {$type_filter} AND {$project_objects_table}.state >= ? AND {$project_objects_table}.visibility >= ?", ATTACHMENT_TYPE_ATTACHMENT, $min_state, $user->getVisibility()), 'row_count');
         if ($total) {
             $offset = ($page - 1) * $per_page;
             $attachments = Attachments::findBySQL("SELECT {$attachments_table}.* FROM {$attachments_table}, {$project_objects_table} WHERE {$attachments_table}.attachment_type = ? AND {$attachments_table}.parent_id = {$project_objects_table}.id AND {$type_filter} AND {$project_objects_table}.state >= ? AND {$project_objects_table}.visibility >= ? ORDER BY `created_on` DESC LIMIT {$offset}, {$per_page}", array(ATTACHMENT_TYPE_ATTACHMENT, $min_state, $user->getVisibility()));
             if ($attachments) {
                 return array($attachments, new Pager($page, $total, $per_page));
             }
             // if
         }
         // if
     }
     // if
     return array(null, new Pager(1, 0, $per_page));
 }
 /**
  * Return upcoming objects in a given projects
  *
  * @param User $user
  * @param Project $project
  * @param array $types
  * @param integer $page
  * @param integer $per_page
  * @return array
  */
 function findUpcoming($user, $project = null, $types = null, $page = null, $per_page = null)
 {
     if (instance_of($project, 'Project')) {
         $type_filter = ProjectUsers::getVisibleTypesFilterByProject($user, $project, $types);
     } else {
         $type_filter = ProjectUsers::getVisibleTypesFilter($user, array(PROJECT_STATUS_ACTIVE), $types);
     }
     if ($type_filter) {
         $today = new DateTimeValue();
         $today->advance(get_user_gmt_offset());
         $newer_than = $today->endOfDay();
         $conditions = array($type_filter . ' AND due_on > ? AND state >= ? AND visibility >= ? AND completed_on IS NULL', $newer_than, STATE_VISIBLE, $user->getVisibility());
         if ($page !== null && $per_page !== null) {
             return ProjectObjects::paginate(array('conditions' => $conditions, 'order' => 'due_on, priority DESC'), $page, $per_page);
         } else {
             return ProjectObjects::find(array('conditions' => $conditions, 'order' => 'due_on, priority DESC'));
         }
         // if
     }
     // if
     return null;
 }
 /**
  * Serve iCal data
  *
  * @param void
  * @return null
  */
 function ical()
 {
     if ($this->active_project->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $filter = ProjectUsers::getVisibleTypesFilterByProject($this->logged_user, $this->active_project, 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($this->active_project->getName() . ' ' . lang('calendar'), $objects);
         die;
     } else {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
 }