/**
  * Show tasks for a given user
  *
  * @param void
  * @return null
  */
 function user_tasks()
 {
     $filter = new AssignmentFilter();
     $filter->setUserFilter(USER_FILTER_LOGGED_USER);
     $filter->setProjectFilter(PROJECT_FILTER_SELECTED);
     $filter->setProjectFilterData(array($this->active_project->getId()));
     $filter->setStatusFilter(STATUS_FILTER_ACTIVE);
     $filter->setOrderBy('priority DESC');
     $filter->setObjectsPerPage(30);
     if ($this->request->isApiCall()) {
         $this->serveData(AssignmentFilters::executeFilter($this->logged_user, $filter, false), 'assignments');
     } else {
         list($assignments, $pagination) = AssignmentFilters::executeFilter($this->logged_user, $filter, null, (int) $this->request->get('page'));
         $this->smarty->assign(array('assignments' => $assignments, 'pagination' => $pagination));
     }
     // if
 }
 /**
  * Create new filter
  *
  * @param void
  * @return null
  */
 function add()
 {
     $this->wireframe->print_button = false;
     if (!AssignmentFilter::canAdd($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $filter_data = $this->request->post('filter');
     if (!is_array($filter_data)) {
         $filter_data = array('user_filter' => USER_FILTER_LOGGED_USER, 'objects_per_page' => 30);
     }
     // if
     $this->smarty->assign('filter_data', $filter_data);
     if ($this->request->isSubmitted()) {
         $this->active_filter = new AssignmentFilter();
         $this->active_filter->setAttributes($filter_data);
         $save = $this->active_filter->save();
         if ($save && !is_error($save)) {
             flash_success("Filter ':name' has been created", array('name' => $this->active_filter->getName()));
             $this->redirectToUrl($this->active_filter->getUrl());
         } else {
             $this->smarty->assign('errors', $save);
         }
         // if
     }
     // if
 }
 /**
  * Show assignments index
  *
  * @param void
  * @return null
  */
 function index()
 {
     list($assignments, $pagination) = AssignmentFilters::executeFilter($this->logged_user, $this->active_filter, null, (int) $this->request->get('page'));
     $this->wireframe->addRssFeed($this->owner_company->getName() . ' - ' . $this->active_filter->getName(), $this->active_filter->getRssUrl($this->logged_user), FEED_RSS);
     $this->smarty->assign(array('assignments' => $assignments, 'pagination' => $pagination, 'grouped_filters' => AssignmentFilters::findGrouped($this->logged_user)));
 }
 /**
  * Execute specific filter
  * 
  * Possible $paginate values:
  * 
  * - NULL - base pagination on filter settings
  * - true - force pagination with given params ($page and $per_page)
  * - false - don't paginate
  *
  * @param User $user
  * @param AssignmentFilter $filter
  * @param mixed $paginate
  * @param integer $page
  * @param integer $per_page
  * @return array
  */
 function executeFilter($user, $filter, $paginate = null, $page = null, $per_page = null)
 {
     $project_objects_table = TABLE_PREFIX . 'project_objects';
     $assignments_table = TABLE_PREFIX . 'assignments';
     $conditions = $filter->prepareConditions($user);
     if (empty($conditions)) {
         if ($paginate === null) {
             return $filter->getObjectsPerPage() ? array(null, new Pager(1, 0, $filter->getObjectsPerPage())) : null;
         } elseif ($paginate) {
             return array(null, new Pager(1, 0, $filter->getObjectsPerPage()));
         } else {
             return null;
         }
         // if
     }
     // if
     $order_by = '';
     if ($filter->orderedByDueDate()) {
         $order_by = $filter->orderDescending() ? 'ORDER BY ISNULL(due_on), due_on DESC, priority DESC' : 'ORDER BY ISNULL(due_on), due_on, priority DESC';
     } elseif ($filter->getOrderBy()) {
         $order_by = 'ORDER BY ' . $filter->getOrderBy();
     }
     // if
     $total = 0;
     $object_ids = array();
     // Lets get object ID-s
     $rows = db_execute_all("SELECT DISTINCT {$project_objects_table}.id FROM {$project_objects_table} LEFT JOIN {$assignments_table} ON {$project_objects_table}.id = {$assignments_table}.object_id WHERE {$conditions}");
     if (is_foreachable($rows)) {
         $total = count($rows);
         foreach ($rows as $row) {
             $object_ids[] = (int) $row['id'];
         }
         // foreach
     }
     // if
     // Use filter pagination settings
     if ($paginate === null || $paginate === true) {
         if ($total) {
             $per_page = $paginate === null ? (int) $filter->getObjectsPerPage() : (int) $per_page;
             if ($per_page < 1) {
                 $per_page = 30;
             }
             // if
             $page = (int) $page;
             if ($page < 1) {
                 $page = 1;
             }
             // if
             $offset = ($page - 1) * $per_page;
             $limit = "LIMIT {$offset}, {$per_page}";
             return array(ProjectObjects::findBySQL("SELECT * FROM {$project_objects_table} WHERE id IN (?) {$order_by} {$limit}", array($object_ids)), new Pager($page, $total, $per_page));
         } else {
             return array(null, new Pager(1, 0, $filter->getObjectsPerPage()));
         }
         // if
         // Don't paginate
     } elseif ($paginate === false) {
         if ($total) {
             return ProjectObjects::findBySQL("SELECT {$project_objects_table}.* FROM {$project_objects_table} WHERE id IN (?) {$order_by}", array($object_ids));
         } else {
             return null;
         }
         // if
     }
     // if
 }
 /**
  * Render iCalendar feed
  *
  * @param void
  * @return null
  */
 function ical()
 {
     $filter = new AssignmentFilter();
     $filter->setUserFilter(USER_FILTER_SELECTED);
     $filter->setUserFilterData(array($this->active_user->getId()));
     $filter->setProjectFilter(PROJECT_FILTER_ACTIVE);
     $filter->setStatusFilter(STATUS_FILTER_ACTIVE);
     render_icalendar(lang(":user's calendar", array('user' => $this->active_user->getDisplayName())), AssignmentFilters::executeFilter($this->logged_user, $filter, false));
     die;
 }