/**
 * Render select_default_assignment_filter control
 * 
 * Parameters:
 * 
 * - user - User - User using the page
 * - value - integer - ID of selected filter
 * - optional - boolean - Value is optional?
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_default_assignment_filter($params, &$smarty)
{
    $user = array_var($params, 'user', null, true);
    $value = array_var($params, 'value', null, true);
    $optional = array_var($params, 'optional', true, true);
    $default_filter_id = (int) ConfigOptions::getValue('default_assignments_filter');
    $default_filter = $default_filter_id ? AssignmentFilters::findById($default_filter_id) : null;
    $options = array();
    if (instance_of($default_filter, 'AssignmentFilter') && $optional) {
        $options[] = option_tag(lang('-- System Default (:filter) --', array('filter' => $default_filter->getName())), '');
    }
    // if
    $grouped_filters = AssignmentFilters::findGrouped($user, true);
    if (is_foreachable($grouped_filters)) {
        foreach ($grouped_filters as $group_name => $filters) {
            $group_options = array();
            foreach ($filters as $filter) {
                $group_options[] = option_tag($filter->getName(), $filter->getId(), array('selected' => $value == $filter->getId()));
            }
            // foreach
            if (count($options) > 0) {
                $options[] = option_tag('', '');
            }
            // if
            $options[] = option_group_tag($group_name, $group_options);
        }
        // foreach
    }
    // if
    return select_box($options, $params);
}
/**
 * Render select user control
 * 
 * Supported paramteres:
 * 
 * - all HTML attributes
 * - value - ID of selected user
 * - project - Instance of selected project, if NULL all users will be listed
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_incoming_mail_select_user($params, &$smarty)
{
    $project = array_var($params, 'project');
    if (!instance_of($project, 'Project')) {
        return new InvalidParamError('object', $project, '$project is expected to be an instance of Project class', true);
    }
    // if
    $users = Users::findForSelect(null, array_var($params, 'project'));
    $value = array_var($params, 'value', null, true);
    $options = array(option_tag(lang('Original Author'), 'original_author', $value == 'original_author' ? array('selected' => true) : null));
    if (is_foreachable($users)) {
        foreach ($users as $company_name => $company_users) {
            $company_options = array();
            foreach ($company_users as $user) {
                $option_attributes = $user['id'] == $value ? array('selected' => true) : null;
                $company_options[] = option_tag($user['display_name'], $user['id'], $option_attributes);
            }
            // foreach
            $options[] = option_group_tag($company_name, $company_options);
        }
        // if
    }
    // if
    return select_box($options, $params);
}
/**
 * Render select parent object for provided project
 * 
 * Supported paramteres:
 * 
 * - types - type of of parent objects to be listed
 * - project - Instance of selected project (required)
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_project_object($params, &$smarty)
{
    $project = array_var($params, 'project');
    if (!instance_of($project, 'Project')) {
        return new InvalidParamError('project', $project, '$project is expected to be an instance of Project class', true);
    }
    // if
    $value = array_var($params, 'value');
    unset($params['project']);
    $types = array_var($params, 'types', null);
    if (!$types || !is_foreachable($types = explode(',', $types))) {
        $types = array('ticket', 'file', 'discussion', 'page');
    }
    // if
    $id_name_map = ProjectObjects::getIdNameMapForProject($project, $types);
    if (!is_foreachable($id_name_map)) {
        return false;
    }
    // if
    $sorted = array();
    foreach ($id_name_map as $object) {
        $option_attributes = $value == $object['id'] ? array('selected' => true) : null;
        $sorted[strtolower($object['type'])][] = option_tag($object['name'], $object['id'], $option_attributes);
    }
    // foreach
    if (is_foreachable($sorted)) {
        foreach ($sorted as $sorted_key => $sorted_values) {
            $options[] = option_group_tag($sorted_key, $sorted_values);
        }
        // foreach
    }
    // if
    return select_box($options, $params);
}
/**
 * Render select project helper
 * 
 * Parametars:
 * 
 *  - value - Id of selected project
 *  - user - Limit only to projects that can be viewed by User
 *  - optional
 * 
 * @param void
 * @return null
 */
function smarty_function_select_project($params, &$smarty)
{
    $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
    $show_all = array_var($params, 'show_all', false) && $user->isProjectManager();
    $value = array_var($params, 'value', null, true);
    $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
    $exclude = (array) array_var($params, 'exclude', array(), true);
    $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);
}
Example #5
0
/**
 * Select a single project file
 *
 * @param string $name Control name
 * @param Project $project
 * @param integer $selected ID of selected file
 * @param array $exclude_files Array of IDs of files that need to be excluded (already attached to object etc)
 * @param array $attributes
 * @return string
 */
function select_project_file($name, $project = null, $selected = null, $exclude_files = null, $attributes = null)
{
    if (is_null($project)) {
        $project = active_project();
    }
    // if
    if (!$project instanceof Project) {
        throw new InvalidInstanceError('$project', $project, 'Project');
    }
    // if
    $all_options = array(option_tag(lang('none'), 0));
    // array of options
    $folders = $project->getFolders();
    if (is_array($folders)) {
        foreach ($folders as $folder) {
            $files = $folder->getFiles();
            if (is_array($files)) {
                $options = array();
                foreach ($files as $file) {
                    if (is_array($exclude_files) && in_array($file->getId(), $exclude_files)) {
                        continue;
                    }
                    $option_attrbutes = $file->getId() == $selected ? array('selected' => true) : null;
                    $options[] = option_tag($file->getFilename(), $file->getId(), $option_attrbutes);
                }
                // if
                if (count($options)) {
                    $all_options[] = option_tag('', 0);
                    // separator
                    $all_options[] = option_group_tag($folder->getName(), $options);
                }
                // if
            }
            // if
        }
        // foreach
    }
    // if
    $orphaned_files = $project->getOrphanedFiles();
    if (is_array($orphaned_files)) {
        $all_options[] = option_tag('', 0);
        // separator
        foreach ($orphaned_files as $file) {
            if (is_array($exclude_files) && in_array($file->getId(), $exclude_files)) {
                continue;
            }
            $option_attrbutes = $file->getId() == $selected ? array('selected' => true) : null;
            $all_options[] = option_tag($file->getFilename(), $file->getId(), $option_attrbutes);
        }
        // foreach
    }
    // if
    return select_box($name, $all_options, $attributes);
}
/**
 * Render select user control
 * 
 * Supported paramteres:
 * 
 * - all HTML attributes
 * - value - ID of selected user
 * - project - Instance of selected project, if NULL all users will be listed
 * - optional - Is this value optional
 * - optional_caption - Value used as text for optional select item
 * - users - id-s of preloaded users
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_user($params, &$smarty)
{
    $preloaded_ids = array_var($params, 'users', null);
    if ($preloaded_ids) {
        $users = Users::findForSelectByIds($preloaded_ids);
    } else {
        $users = Users::findForSelect(array_var($params, 'company'), array_var($params, 'project'));
    }
    // if
    if (array_key_exists('company', $params)) {
        unset($params['company']);
    }
    // if
    if (array_key_exists('project', $params)) {
        unset($params['project']);
    }
    // if
    $value = array_var($params, 'value', null, true);
    $optional = array_var($params, 'optional', false, true);
    $options = array();
    if ($optional) {
        $options[] = option_tag(lang(array_var($params, 'optional_caption', '-- Select user --')), '');
    }
    // if
    if (is_foreachable($users)) {
        foreach ($users as $company_name => $company_users) {
            $company_options = array();
            foreach ($company_users as $user) {
                $option_attributes = $user['id'] == $value ? array('selected' => true) : null;
                $company_options[] = option_tag($user['display_name'], $user['id'], $option_attributes);
            }
            // foreach
            $options[] = option_group_tag($company_name, $company_options);
        }
        // if
    }
    // if
    return select_box($options, $params);
}
/**
 * Render select project user box
 *
 * @param string $name Name of the widget
 * @param Project $project
 * @param integer $selected ID of selected user
 * @param array $exclude_users Array of IDs of users that need to be excluded (e.g. already subscribers)
 * @param array $attributes Additional attributes
 * @return string
 */
function select_project_user($name, $project = null, $selected = null, $exclude_users = null, $attributes = null)
{
    if (is_null($project)) {
        $project = active_project();
    }
    // if
    if (!$project instanceof Project) {
        throw new InvalidInstanceError('$project', $project, 'Project');
    }
    // if
    if (is_array($attributes)) {
        if (!isset($attributes['class'])) {
            $attributes['class'] = 'select_project_user';
        }
        // if
    } else {
        $attributes = array('class' => 'select_project_user');
    }
    // if
    $grouped_project_users = $project->getUsers(true);
    $all_options = array(option_tag(lang('none'), 0));
    if (is_array($grouped_project_users)) {
        foreach ($grouped_project_users as $company_id => $users) {
            $company = Companies::findById($company_id);
            if (!$company instanceof Company) {
                continue;
            }
            // if
            $options = array();
            if (is_array($users)) {
                foreach ($users as $user) {
                    if (is_array($exclude_users) && in_array($user->getId(), $exclude_users)) {
                        continue;
                    }
                    // if
                    $option_attributes = $user->getId() == $selected ? array('selected' => 'selected') : null;
                    $display_name = $user->getDisplayName() . ($user->getId() == logged_user()->getId() ? ' (' . lang('you') . ')' : '');
                    $options[] = option_tag($display_name, $user->getId(), $option_attributes);
                }
                // foreach
                if (count($options)) {
                    $all_options[] = option_group_tag($company->getName(), $options);
                }
                // if
            }
        }
        // foreach
    }
    // if
    return select_box($name, $all_options, $attributes);
}
/**
 * Render select milestone control
 * 
 * Params:
 * 
 * - project - Project instance that need to be used
 * - active_only - Return only active milestones, true by default
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_milestone($params, &$smarty)
{
    $project = array_var($params, 'project');
    if (!instance_of($project, 'Project')) {
        return new InvalidParamError('project', $project, '$project value is expected to be an instance of Project class', true);
    }
    // if
    unset($params['project']);
    $active_only = false;
    if (isset($params['active_only'])) {
        $active_only = (bool) $params['active_only'];
        unset($params['active_only']);
    }
    // if
    $value = null;
    if (isset($params['value'])) {
        $value = $params['value'];
        unset($params['value']);
    }
    // if
    $optional = true;
    if (isset($params['optional'])) {
        $optional = (bool) $params['optional'];
        unset($params['optional']);
    }
    // if
    $options = array();
    if ($optional) {
        $options[] = option_tag(lang('-- None --'), '');
        $options[] = option_tag('', '');
    }
    // if
    $logged_user = $smarty->get_template_vars('logged_user');
    $milestones = $active_only ? Milestones::findActiveByProject($project, STATE_VISIBLE, $logged_user->getVisibility()) : Milestones::findByProject($project, $logged_user);
    //BOF:mod 20130126
    $entries_exist = false;
    //EOF:mod 20130126
    if (is_foreachable($milestones)) {
        $completed_options = array();
        foreach ($milestones as $milestone) {
            if ($milestone->isCompleted()) {
                $option_attributes = $milestone->getId() == $value ? array('selected' => true) : null;
                $completed_options[] = option_tag($milestone->getName(), $milestone->getId(), $option_attributes);
            } else {
                //BOF:mod 20130126
                $entries_exist = true;
                //EOF:mod 20130126
                $option_attributes = $milestone->getId() == $value ? array('selected' => true) : null;
                $options[] = option_tag($milestone->getName(), $milestone->getId(), $option_attributes);
            }
            // if
        }
        // foreach
        if (is_foreachable($completed_options)) {
            $options[] = option_tag('', '');
            $options[] = option_group_tag(lang('Completed'), $completed_options);
        }
        // if
    }
    // if
    //BOF:mod 20130126
    if (!$entries_exist) {
        unset($params['class']);
    }
    //EOF:mod 20130126
    return select_box($options, $params);
}
/**
 * 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);
}