/**
 * Render link to a specific category
 * 
 * Parameters:
 * 
 * - object - ProjectObject, We need to extract category data from this object
 * - getter - string, Name of the function that will return parent category ID. 
 *   By default getParentId() will be used, but it can be any method
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_category_link($params, &$smarty)
{
    $object = array_var($params, 'object');
    if (!instance_of($object, 'ProjectObject')) {
        return new InvalidParamError('object', $object, 'Object is expected to be an instance of ProjectObject class', true);
    }
    // if
    $getter = trim(array_var($params, 'getter', 'getParentId'));
    if ($getter == '') {
        return new InvalidParamError('getter', $getter, 'Getter is expected to be valid method name', true);
    }
    // if
    $category_id = (int) $object->{$getter}();
    if ($category_id > 0) {
        $category = Categories::findById($category_id);
        if (instance_of($category, 'Category')) {
            return '<a href="' . $category->getViewUrl() . '">' . clean($category->getName()) . '</a>';
        }
        // if
    }
    // if
    return '<span class="unknown_category_link unknown_object_link">' . lang('Unknown Category') . '</span>';
}
 /**
  * Construct project controller
  *
  * @param Request $request
  * @return ProjectController
  */
 function __construct($request)
 {
     parent::__construct($request);
     $this->wireframe->page_actions = array();
     // Reset page actions
     $project_id = $this->request->get('project_id');
     if ($project_id) {
         $this->active_project = Projects::findById($project_id);
     }
     // if
     if (instance_of($this->active_project, 'Project')) {
         if (!$this->logged_user->isProjectMember($this->active_project)) {
             $this->httpError(HTTP_ERR_FORBIDDEN);
         }
         // if
         if ($this->active_project->getType() == PROJECT_TYPE_SYSTEM) {
             $this->httpError(HTTP_ERR_NOT_FOUND);
         }
         // if
         if ($this->active_project->isCompleted()) {
             $this->wireframe->addBreadCrumb(lang('Archive'), assemble_url('projects_archive'));
         }
         // if
         $this->wireframe->addBreadCrumb($this->active_project->getName(), $this->active_project->getOverviewUrl());
         $tabs = new NamedList();
         $tabs->add('overview', array('text' => str_excerpt($this->active_project->getName(), 25), 'url' => $this->active_project->getOverviewUrl()));
         event_trigger('on_project_tabs', array(&$tabs, &$this->logged_user, &$this->active_project));
         $tabs->add('people', array('text' => lang('People'), 'url' => $this->active_project->getPeopleUrl()));
         $tabs->add('recent_pages', array('text' => lang('Recent Pages'), 'url' => assemble_url('recent_pages') . '&project_id=' . $this->active_project->getId()));
         /*$tabs->add('recent_pages_1', array(
             'text' => lang('Recent Pages'),
             'url' => assemble_url('project_recent_pages_1'),
           ));*/
         //BOF:mod
         /*$tabs->add('attachments', array(
             'text' => lang('Attachments'),
             'url' => assemble_url('attachments_list', array('project_id' => $this->active_project->getId())) ,
           ));*/
         //EOF:mod
         $tabs->add('reminders', array('text' => lang('Notifications'), 'url' => assemble_url('reminders_list', array('project_id' => $this->active_project->getId()))));
         $tabs->add('calendar', array('text' => lang('Calendar'), 'url' => Calendar::getProjectCalendarUrl($this->active_project)));
         js_assign('image_picker_url', assemble_url('image_picker', array('project_id' => $this->active_project->getId())));
         js_assign('active_project_id', $this->active_project->getId());
         $this->smarty->assign('page_tabs', $tabs);
         // ---------------------------------------------------
         //  Set page company and page project
         // ---------------------------------------------------
         $page_company = $this->active_project->getCompany();
         if (instance_of($page_company, 'Company')) {
             $this->wireframe->page_company = $page_company;
         }
         // if
         $this->wireframe->page_project = $this->active_project;
         // New project
     } else {
         if ($this->controller_name == 'project') {
             $this->active_project = new Project();
         } else {
             $this->httpError(HTTP_ERR_NOT_FOUND);
         }
         // if
     }
     // if
     $this->smarty->assign(array('active_project' => $this->active_project, 'page_tab' => 'overview'));
     // -----------------------------------------------------------------------
     //  Do category related voodoo if categories are enabled. Categories are
     //  not initialized if we don't have a loaded project (no project ID)
     // -----------------------------------------------------------------------
     if ($this->active_project->isLoaded() && $this->enable_categories) {
         $category_id = $this->request->get('category_id');
         if ($category_id) {
             $this->active_category = Categories::findById($category_id);
         }
         // if
         if (instance_of($this->active_category, 'Category')) {
             if ($this->active_category->getProjectId() != $this->active_project->getId()) {
                 $this->active_category = new Category();
                 // this category is not part of selected project
             }
             // if
         } else {
             $this->active_category = new Category();
         }
         // if
         $this->smarty->assign(array('active_category' => $this->active_category, 'categories_url' => Category::getSectionUrl($this->active_project, $this->getControllerName(), $this->active_module), 'add_category_url' => Category::getAddUrl($this->active_project, $this->getControllerName(), $this->active_module)));
     }
     // if
 }
 /**
  * Update multiple tickets
  *
  * @param void
  * @return null
  */
 function mass_edit()
 {
     if (!$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     $action = $this->request->post('with_selected');
     if (trim($action) == '') {
         flash_error('Please select what you want to do with selected tickets');
         $this->redirectToReferer($this->smarty->get_template_vars('files_url'));
     }
     // if
     $files_ids = $this->request->post('files');
     $object_types = $this->request->post('object_types');
     if ($object_types == 'files') {
         $files = Files::findByIds($files_ids, STATE_VISIBLE, $this->logged_user->getVisibility());
         $redirect_url = $this->smarty->get_template_vars('files_url');
     } else {
         if ($object_types == 'attachments') {
             $files = Attachments::findbyids($files_ids, STATE_VISIBLE, $this->logged_user->getVisibility());
             $redirect_url = $this->smarty->get_template_vars('attachments_url');
         } else {
             $files = array();
             $redirect_url = $this->smarty->get_template_vars('files_url');
         }
     }
     // if
     if (!is_foreachable($files)) {
         flash_error('Please select files that you would like to update');
         $this->redirectToReferer($this->smarty->get_template_vars('files_url'));
     }
     // if
     $updated = 0;
     if ($action == 'delete') {
         // delete attachments
         $message = lang(':count attachments deleted');
         foreach ($files as $file) {
             if ($file->canDelete($this->logged_user)) {
                 $delete = $file->delete();
                 if ($delete && !is_error($delete)) {
                     $updated++;
                 }
                 // if
             }
             // if
         }
         // foreach
     } else {
         if ($action == 'move_to_trash') {
             // move files to trash
             $message = lang(':count files moved to trash');
             foreach ($files as $file) {
                 if ($file->canDelete($this->logged_user)) {
                     $delete = $file->moveToTrash();
                     if ($delete && !is_error($delete)) {
                         $updated++;
                     }
                     // if
                 }
                 // if
             }
             // foreach
         } else {
             if (str_starts_with($action, 'move_to_category')) {
                 // chage files category
                 $message = lang(':count files updated');
                 if ($action == 'move_to_category') {
                     $category_id = 0;
                 } else {
                     $category_id = (int) substr($action, 17);
                 }
                 // if
                 $category = $category_id ? Categories::findById($category_id) : null;
                 foreach ($files as $file) {
                     if ($file->canEdit($this->logged_user)) {
                         $file->setParent($category, false);
                         $save = $file->save();
                         if ($save && !is_error($save)) {
                             $updated++;
                         }
                         // if
                     }
                 }
                 // foreach
             } else {
                 // invalid action
                 $this->httpError(HTTP_ERR_BAD_REQUEST);
             }
         }
     }
     flash_success($message, array('count' => $updated));
     $this->redirectToReferer($redirect_url);
 }
 /**
  * Redirect to category view
  *
  */
 function view_category()
 {
     $category_id = (int) $this->request->get('object_id');
     if ($category_id) {
         $category = Categories::findById($category_id);
     } else {
         $category = new Category();
     }
     // if
     if ($category->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $project = $category->getProject();
     if (!instance_of($project, 'Project')) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $base_url = assemble_url('mobile_access_view_' . $category->getModule(), array('project_id' => $project->getId()));
     $this->redirectToUrl($base_url . '/?category_id=' . $category_id);
 }
 /**
  * enable Categories
  *
  */
 function enableCategories()
 {
     $this->enable_categories = false;
     if ($this->active_project->isLoaded()) {
         $this->enable_categories = true;
         $category_id = $this->request->get('category_id');
         if ($category_id) {
             $this->active_category = Categories::findById($category_id);
         }
         // if
         if (instance_of($this->active_category, 'Category')) {
             if ($this->active_category->getProjectId() != $this->active_project->getId()) {
                 $this->active_category = new Category();
                 // this category is not part of selected project
             }
             // if
         } else {
             $this->active_category = new Category();
             // invalid category instance
         }
         // if
     } else {
         $this->active_category = new Category();
         // categories disabled or category not selected
     }
     // if
     $this->smarty->assign(array("enable_categories" => $this->enable_categories, "active_category" => $this->active_category));
 }
 /**
  * Update multiple tickets
  *
  * @param void
  * @return null
  */
 function mass_update()
 {
     if ($this->request->isSubmitted()) {
         $action = $this->request->post('with_selected');
         if (trim($action) == '') {
             flash_error('Please select what you want to do with selected tickets');
             $this->redirectToReferer($this->smarty->get_template_vars('tickets_url'));
         }
         // if
         $ticket_ids = $this->request->post('tickets');
         $tickets = Tickets::findByIds($ticket_ids, STATE_VISIBLE, $this->logged_user->getVisibility());
         $updated = 0;
         if (is_foreachable($tickets)) {
             // Complete selected tickets
             if ($action == 'complete') {
                 $message = lang(':count tickets completed');
                 //BOF:mod 20110617
                 //BOF:mod 20120917 (reversed by shawn)
                 /*
                 //EOF:mod 20120917
                     		$warning = '';
                     		foreach($tickets as $ticket) {
                 	if($ticket->isOpen() && $ticket->canChangeCompleteStatus($this->logged_user)) {
                       				$responsible_assignee = $ticket->getResponsibleAssignee();
                       				$created_by_id = $ticket->getCreatedById();
                       				$project_leader = $this->active_project->getLeaderId();
                       				if ( (!is_null($responsible_assignee) && $responsible_assignee->getId()==$this->logged_user->getId()) 
                 					|| $created_by_id==$this->logged_user->getId() 
                 					|| $project_leader==$this->logged_user->getId() 
                 					|| $this->logged_user->isAdministrator() ){
                 			$warning .= '';
                 		} else {
                 			$warning .= '"' . $ticket->getName() . '", ';
                 		}
                 	}
                 }
                 if (!empty($warning)){
                                             $temp = new User(!empty($created_by_id) ? $created_by_id : $project_leader);
                                             $warning = 'Ticket ' . substr($warning, 0, -2) . ' cannot be closed at this time. Please send message to ' . $temp->getName() . ' to close this ticket.';
                                             unset($temp);
                           		flash_error($warning, null, true);
                           		$this->redirectToReferer($this->smarty->get_template_vars('tickets_url'));
                 } else {
                 //BOF:mod 20120917 (reversed by shawn)
                 */
                 //EOF:mod 20120917
                 //EOF:mod 20110617
                 foreach ($tickets as $ticket) {
                     if ($ticket->isOpen() && $ticket->canChangeCompleteStatus($this->logged_user)) {
                         $complete = $ticket->complete($this->logged_user);
                         if ($complete && !is_error($complete)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 //BOF:mod 20110617
                 //BOF:mod 20120917 (reversed by shawn)
                 /*
                 //EOF:mod 20120917
                     		}
                 //BOF:mod 20120917 (reversed by shawn)
                 */
                 //EOF:mod 20120917
                 //EOF:mod 20110617
                 // Open selected tickets
             } elseif ($action == 'open') {
                 $message = lang(':count tickets opened');
                 foreach ($tickets as $ticket) {
                     if ($ticket->isCompleted() && $ticket->canChangeCompleteStatus($this->logged_user)) {
                         $open = $ticket->open($this->logged_user);
                         if ($open && !is_error($open)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 // Mark object as starred
             } elseif ($action == 'star') {
                 $message = lang(':count tickets starred');
                 foreach ($tickets as $ticket) {
                     $star = $ticket->star($this->logged_user);
                     if ($star && !is_error($star)) {
                         $updated++;
                     }
                     // if
                 }
                 // foreach
                 // Unstar objects
             } elseif ($action == 'unstar') {
                 $message = lang(':count tickets unstarred');
                 foreach ($tickets as $ticket) {
                     $unstar = $ticket->unstar($this->logged_user);
                     if ($unstar && !is_error($unstar)) {
                         $updated++;
                     }
                     // if
                 }
                 // foreach
                 // Move selected objects to Trash
             } elseif ($action == 'trash') {
                 $message = lang(':count tickets moved to Trash');
                 foreach ($tickets as $ticket) {
                     if ($ticket->canDelete($this->logged_user)) {
                         $delete = $ticket->moveToTrash();
                         if ($delete && !is_error($delete)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 // Set a selected priority
             } elseif (str_starts_with($action, 'set_priority')) {
                 $priority = (int) substr($action, 13);
                 $message = lang(':count tickets updated');
                 foreach ($tickets as $ticket) {
                     if ($ticket->canEdit($this->logged_user)) {
                         $ticket->setPriority($priority);
                         $save = $ticket->save();
                         if ($save && !is_error($save)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 // Set visibility
             } elseif (str_starts_with($action, 'set_visibility')) {
                 $visibility = (int) substr($action, 15);
                 $message = lang(':count tickets updated');
                 foreach ($tickets as $ticket) {
                     if ($ticket->canEdit($this->logged_user)) {
                         $ticket->setVisibility($visibility);
                         $save = $ticket->save();
                         if ($save && !is_error($save)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 // Move this ticket to a given milestone
             } elseif (str_starts_with($action, 'move_to_milestone')) {
                 if ($action == 'move_to_milestone') {
                     $milestone_id = null;
                 } else {
                     $milestone_id = (int) substr($action, 18);
                 }
                 // if
                 $message = lang(':count tickets updated');
                 foreach ($tickets as $ticket) {
                     if ($ticket->canEdit($this->logged_user)) {
                         $ticket->setMilestoneId($milestone_id);
                         $save = $ticket->save();
                         if ($save && !is_error($save)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
                 // Move selected tickets to selected category
             } elseif (str_starts_with($action, 'move_to_category')) {
                 if ($action == 'move_to_category') {
                     $category_id = null;
                 } else {
                     $category_id = (int) substr($action, 17);
                 }
                 // if
                 $category = $category_id ? Categories::findById($category_id) : null;
                 $message = lang(':count tickets updated');
                 foreach ($tickets as $ticket) {
                     if ($ticket->canEdit($this->logged_user)) {
                         $ticket->setParent($category, false);
                         $save = $ticket->save();
                         if ($save && !is_error($save)) {
                             $updated++;
                         }
                         // if
                     }
                     // if
                 }
                 // foreach
             } else {
                 $this->httpError(HTTP_ERR_BAD_REQUEST);
             }
             // if
             flash_success($message, array('count' => $updated));
             $this->redirectToReferer($this->smarty->get_template_vars('tickets_url'));
         } else {
             flash_error('Please select tickets that you would like to update');
             $this->redirectToReferer($this->smarty->get_template_vars('tickets_url'));
         }
         // if
     } else {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
 }