/**
  * Mobile Access Homepage
  *
  */
 function index()
 {
     $pinned_project_ids = PinnedProjects::findProjectIdsByUser($this->logged_user);
     if (is_foreachable($pinned_project_ids)) {
         $pinned_projects = Projects::findByIds($pinned_project_ids);
     } else {
         $pinned_projects = null;
     }
     // if
     $this->smarty->assign(array("pinned_projects" => $pinned_projects));
 }
 /**
  * Show dashboard overview
  *
  * @param void
  * @return null
  */
 function index()
 {
     // Welcome message, displayed only to administrators
     if ($this->logged_user->isAdministrator() && ConfigOptions::getValue('show_welcome_message')) {
         $this->wireframe->addPageAction(lang('Hide Welcome Message'), assemble_url('admin_settings_hide_welcome_message'), null, array('method' => 'post', 'confirm' => lang('You are about to hide welcome message. If you wish to bring it back later on you can do it from General settings page in Administration. Hide now?')));
         $this->smarty->assign(array('show_welcome_message' => true, 'available_modules' => Modules::findNotInstalled()));
         //BOF: task 05 | AD
         $this->wireframe->addPageAction(lang('View All'), assemble_url('view_projects_info'));
         //EOF: task 05 | AD
         // Regular dashboard
     } else {
         if (Project::canAdd($this->logged_user)) {
             $this->wireframe->addPageAction(lang('New Project'), assemble_url('projects_add'));
             //BOF: task 05 | AD
             $this->wireframe->addPageAction(lang('View All'), assemble_url('view_projects_info'));
             //EOF: task 05 | AD
         }
         // if
         $this->wireframe->addRssFeed($this->owner_company->getName() . ' - ' . lang('Recent activities'), assemble_url('rss', array('token' => $this->logged_user->getToken(true))), FEED_RSS);
         $pinned_project_ids = PinnedProjects::findProjectIdsByUser($this->logged_user);
         if (is_foreachable($pinned_project_ids)) {
             $pinned_projects = Projects::findByIds($pinned_project_ids);
         } else {
             $pinned_projects = null;
         }
         // if
         $dashboard_sections = new NamedList();
         event_trigger('on_dashboard_sections', array(&$dashboard_sections, &$this->logged_user));
         $important_items = new NamedList();
         event_trigger('on_dashboard_important_section', array(&$important_items, &$this->logged_user));
         $this->smarty->assign(array('show_welcome_message' => false, 'important_items' => $important_items, 'pinned_projects' => $pinned_projects, 'dashboard_sections' => $dashboard_sections, 'online_users' => Users::findWhoIsOnline($this->logged_user), 'grouped_activities' => group_by_date(ActivityLogs::findActiveProjectsActivitiesByUser($this->logged_user, 20), $this->logged_user)));
     }
     // if
     //BOF:mod 20110623
     $tabs = new NamedList();
     $tabs->add('dashboard', array('text' => 'Active Teams', 'url' => assemble_url('dashboard')));
     $tabs->add('home_page', array('text' => 'Home Page', 'url' => assemble_url('goto_home_tab')));
     $tabs->add('assigned_action_request', array('text' => 'Assigned Action Requests', 'url' => assemble_url('assigned_action_request')));
     $tabs->add('owned_tickets', array('text' => 'Owned Tickets', 'url' => assemble_url('my_tickets')));
     $tabs->add('subscribed_tickets', array('text' => 'Subscribed Tickets', 'url' => assemble_url('my_subscribed_tickets')));
     $this->smarty->assign('page_tabs', $tabs);
     $this->smarty->assign('page_tab', 'dashboard');
     //EOF:mod 20110623
 }
 /**
  * Group objects by project
  *
  * @param array $objects
  * @return array
  */
 function groupByProject($objects)
 {
     $result = array();
     if (is_foreachable($objects)) {
         $project_ids = objects_array_extract($objects, 'getProjectId');
         if (is_foreachable($project_ids)) {
             $projects = Projects::findByIds($project_ids);
             if (is_foreachable($projects)) {
                 foreach ($projects as $project) {
                     $result[$project->getId()] = array('project' => $project, 'objects' => array());
                 }
                 // foreach
             }
             // if
         }
         // if
         foreach ($objects as $object) {
             if (isset($result[$object->getProjectId()])) {
                 $result[$object->getProjectId()]['objects'][] = $object;
             }
             // if
         }
         // foreach
     }
     // if
     return $result;
 }
 /**
  * Show and process add to projects page
  *
  * @param void
  * @return null
  */
 function add_to_projects()
 {
     if ($this->active_user->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->logged_user->isProjectManager()) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $add_to_projects_data = $this->request->post('add_to_projects');
     $this->smarty->assign(array('add_to_projects_data' => $add_to_projects_data, 'exclude_project_ids' => Projects::findProjectIdsByUser($this->active_user)));
     if ($this->request->isSubmitted()) {
         $errors = new ValidationErrors();
         $projects = null;
         if (is_foreachable($add_to_projects_data['projects'])) {
             $projects = Projects::findByIds($add_to_projects_data['projects']);
         }
         // if
         if (!is_foreachable($projects)) {
             $errors->addError(lang('Please select projects'), 'projects');
         }
         // if
         if ($add_to_projects_data['role_id']) {
             $role = Roles::findById($add_to_projects_data['role_id']);
             $permissions = null;
             if (!instance_of($role, 'Role') || !($role->getType() == ROLE_TYPE_PROJECT)) {
                 $errors->addError(lang('Invalid project role'), 'project_permissions');
             }
             // if
         } else {
             $role = null;
             $permissions = array_var($add_to_projects_data, 'permissions');
         }
         // if
         if ($errors->hasErrors()) {
             $this->smarty->assign('errors', $errors);
         } else {
             $added = 0;
             foreach ($projects as $project) {
                 $add = $project->addUser($this->active_user, $role, $permissions);
                 if ($add && !is_error($add)) {
                     $added++;
                 }
                 // if
             }
             // foreach
             if ($added == 1) {
                 flash_success(':name has been added to 1 project', array('name' => $this->active_user->getDisplayName()));
             } else {
                 flash_success(':name has been added to :count projects', array('name' => $this->active_user->getDisplayName(), 'count' => $added));
             }
             // if
             $this->redirectToUrl($this->active_user->getViewUrl());
         }
         // if
     }
     // if
 }
/**
 * Render select projects widget
 * 
 * Parameters:
 * 
 * - user - Instance of user accesing the page, required
 * - exclude - Single project or array of projects that need to be excluded
 * - value - Array of selected projects
 * - active_only - List only active projects
 * - show_all - If true and user is project manager / administrator, all 
 *   projects will be listed
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_projects($params, &$smarty)
{
    static $ids = array();
    $name = array_var($params, 'name');
    if ($name == '') {
        return new InvalidParamError('name', $name, '$name is expected to be a valid control name', true);
    }
    // if
    $user = array_var($params, 'user', null, true);
    if (!instance_of($user, 'User')) {
        return new InvalidParamError('user', $user, '$user is expected to be an instance of User class', true);
    }
    // if
    $id = array_var($params, 'id', null, true);
    if (empty($id)) {
        $counter = 1;
        do {
            $id = "select_projects_{$counter}";
        } while (in_array($id, $ids));
    }
    // if
    $ids[] = $id;
    $show_all = array_var($params, 'show_all', false) && $user->isProjectManager();
    $exclude = array_var($params, 'exclude', array(), true);
    if (!is_array($exclude)) {
        $exclude = array($exclude);
    }
    // if
    $value = array_var($params, 'value', null, true);
    if (is_foreachable($value) && count($exclude)) {
        foreach ($value as $k => $v) {
            if (in_array($v, $exclude)) {
                unset($value[$k]);
            }
            // if
        }
        // foreach
    }
    // if
    $selected_projects = is_foreachable($value) ? Projects::findByIds($value) : null;
    require_once ANGIE_PATH . '/classes/json/init.php';
    $smarty->assign(array('_select_projects_id' => $id, '_select_projects_name' => array_var($params, 'name'), '_select_projects_user' => $user, '_select_projects_projects' => $selected_projects, '_select_projects_exclude_ids' => do_json_encode($exclude), '_select_projects_active_only' => array_var($params, 'active_only', true), '_select_projects_show_all' => $show_all));
    return $smarty->fetch(get_template_path('_select_projects', null, SYSTEM_MODULE));
    // ---------------------------------------------------
    //  Old!
    // ---------------------------------------------------
    $projects_table = TABLE_PREFIX . 'projects';
    $project_users_table = TABLE_PREFIX . 'project_users';
    if ($show_all) {
        $projects = db_execute_all("SELECT {$projects_table}.id, {$projects_table}.name, {$projects_table}.status FROM {$projects_table} WHERE {$projects_table}.type = ? ORDER BY {$projects_table}.name", PROJECT_TYPE_NORMAL);
    } else {
        $projects = db_execute_all("SELECT {$projects_table}.id, {$projects_table}.name, {$projects_table}.status FROM {$projects_table}, {$project_users_table} WHERE {$project_users_table}.user_id = ? AND {$project_users_table}.project_id = {$projects_table}.id AND {$projects_table}.type = ? ORDER BY {$projects_table}.name", $user->getId(), PROJECT_TYPE_NORMAL);
    }
    // if
    $active_options = array();
    $archived_options = array();
    if (is_foreachable($projects)) {
        foreach ($projects as $k => $project) {
            if (in_array($project['id'], $exclude)) {
                continue;
            }
            // if
            $option_attributes = $project['id'] == $value ? array('selected' => true) : null;
            if ($project['status'] == PROJECT_STATUS_ACTIVE) {
                $active_options[] = option_tag($project['name'], $project['id'], $option_attributes);
            } else {
                $archived_options[] = option_tag($project['name'], $project['id'], $option_attributes);
            }
            // if
        }
        // if
    }
    // if
    $optional = array_var($params, 'optional', false, true);
    $options = array();
    if ($optional) {
        $options[] = option_tag(lang(array_var($params, 'optional_caption', '-- Select Project --')), '');
        $options[] = option_tag('', '');
    }
    // if
    if (is_foreachable($active_options)) {
        $options[] = option_group_tag(lang('Active'), $active_options);
    }
    // if
    if (is_foreachable($active_options) && is_foreachable($archived_options)) {
        $options[] = option_tag('', '');
    }
    // if
    if (is_foreachable($archived_options)) {
        $options[] = option_group_tag(lang('Archive'), $archived_options);
    }
    // if
    return select_box($options, $params);
}