/**
 * Add sidebars to project overview page
 *
 * @param array $sidebars
 * @param Project $project
 * @param User $user
 * @return null
 */
function system_handle_on_project_overview_sidebars(&$sidebars, &$project, &$user)
{
    // only project leader, system administrators and project manages can see last activity
    $can_see_last_activity = $user->isProjectLeader($project) || $user->isAdministrator() || $user->isProjectManager();
    $project_users = $project->getUsers();
    if (is_foreachable($project_users)) {
        $smarty =& Smarty::instance();
        require_once SYSTEM_MODULE_PATH . '/helpers/function.user_link.php';
        require_once SMARTY_PATH . '/plugins/modifier.ago.php';
        $output = '';
        $sorted_users = Users::groupByCompany($project_users);
        foreach ($sorted_users as $sorted_user) {
            $company = $sorted_user['company'];
            $users = $sorted_user['users'];
            if (is_foreachable($users)) {
                $output .= '<h3><a href="' . $company->getViewUrl() . '">' . clean($company->getName()) . '</a></h3>';
                $output .= '<ul class="company_users">';
                foreach ($users as $current_user) {
                    $last_seen = '';
                    if ($can_see_last_activity && $user->getId() != $current_user->getId()) {
                        $last_seen = smarty_modifier_ago($current_user->getLastActivityOn());
                    }
                    // if
                    $output .= '<li><span class="icon_holder"><img src="' . $current_user->getAvatarUrl() . '" /></span> ' . smarty_function_user_link(array('user' => $current_user), $smarty) . ' ' . $last_seen . '</li>';
                }
                // foreach
                $output .= '</ul>';
            }
            // if
        }
        // foreach
        $sidebars[] = array('label' => lang('People on This Project'), 'is_important' => false, 'id' => 'project_people', 'body' => $output);
    }
    // if
}
/**
 * Renders action string with time when action was taken
 * 
 * Parameteres:
 * 
 * - action - Action string, default is 'Posted'. It is used for lang retrival
 * - datetime - Datetime object when action was taken
 * - format - Format in with time is displayed. Possible values are ago, 
 *   datetime, date and time. Default is 'ago'
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_action_on($params, &$smarty)
{
    $action = clean(array_var($params, 'action', 'Posted'));
    $datetime = array_var($params, 'datetime');
    if (!instance_of($datetime, 'DateValue')) {
        return new InvalidParamError('datetime', $datetime, '$datetime is expected to be an instance of DateValue or DateTimeValue class', true);
    }
    // if
    $format = array_var($params, 'format', 'ago');
    if (!in_array($format, array('ago', 'date', 'datetime', 'time'))) {
        return new InvalidParamError('format', $format, 'Format is requred to be one of following four values: ago, date, datetime or time', true);
    }
    // if
    switch ($format) {
        case 'date':
            require_once SMARTY_DIR . '/plugins/modifier.date.php';
            $formatted_datetime = smarty_modifier_date($datetime);
            break;
        case 'time':
            require_once SMARTY_DIR . '/plugins/modifier.time.php';
            $formatted_datetime = smarty_modifier_time($datetime);
            break;
        case 'datetime':
            require_once SMARTY_DIR . '/plugins/modifier.datetime.php';
            $formatted_datetime = smarty_modifier_datetime($datetime);
            break;
        default:
            require_once SMARTY_DIR . '/plugins/modifier.ago.php';
            $formatted_datetime = smarty_modifier_ago($datetime);
    }
    // switch
    return lang($action) . ' ' . $formatted_datetime;
}
/**
 * Renders action string with time when action was taken and link to profile of 
 * user who acted
 * 
 * Parameteres:
 * 
 * - action - Action string, default is 'Posted'. It is used for lang retrival
 * - user - User who took the action. Can be registered user or anonymous user
 * - datetime - Datetime object when action was taken
 * - format - Format in with time is displayed. Possible values are ago, 
 *   datetime, date and time. Default is 'ago'
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_action_on_by($params, &$smarty)
{
    $action = clean(array_var($params, 'action', 'Posted'));
    $datetime = array_var($params, 'datetime');
    if (!instance_of($datetime, 'DateValue')) {
        return new InvalidParamError('datetime', $datetime, '$datetime is expected to be an instance of DateValue or DateTimeValue class', true);
    }
    // if
    $format = array_var($params, 'format', 'ago');
    if (!in_array($format, array('ago', 'date', 'datetime', 'time'))) {
        return new InvalidParamError('format', $format, 'Format is requred to be one of following four values: ago, date, datetime or time', true);
    }
    // if
    $offset = array_var($params, 'offset', null);
    switch ($format) {
        case 'date':
            require_once SMARTY_DIR . '/plugins/modifier.date.php';
            $formatted_datetime = smarty_modifier_date($datetime, $offset);
            break;
        case 'time':
            require_once SMARTY_DIR . '/plugins/modifier.time.php';
            $formatted_datetime = smarty_modifier_time($datetime, $offset);
            break;
        case 'datetime':
            require_once SMARTY_DIR . '/plugins/modifier.datetime.php';
            $formatted_datetime = smarty_modifier_datetime($datetime, $offset);
            break;
        default:
            require_once SMARTY_DIR . '/plugins/modifier.ago.php';
            $formatted_datetime = smarty_modifier_ago($datetime, $offset);
    }
    // switch
    $user = array_var($params, 'user');
    //BOF:mod 20110708 ticketid202
    /*
    //EOF:mod 20110708 ticketid202
    if(instance_of($user, 'User')) {
      return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by') . ' <a href="'. $user->getViewUrl() .'">' . clean($user->getDisplayName()) . '</a>';
    } elseif(instance_of($user, 'AnonymousUser')) {
      return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by') . ' <a href="mailto:'. $user->getEmail() .'">' . clean($user->getName()) . '</a>';
    } else {
      return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by unknown user');
    } // if
    //BOF:mod 20110708 ticketid202
    */
    $comment_link = '';
    $parent_obj = array_var($params, 'parent_obj');
    if ($parent_obj) {
        $type = $parent_obj->getType();
        if ($type == 'Comment') {
            $comment_link = '&nbsp;&nbsp;<a href="' . $parent_obj->getViewUrl() . '">View Comment</a>';
        }
    }
    //BOF:mod 20120913
    /*
    //EOF:mod 20120913
        if(instance_of($user, 'User')) {
          return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by') . ' <a href="'. $user->getViewUrl() .'">' . clean($user->getDisplayName()) . '</a>' . $comment_link;
        } elseif(instance_of($user, 'AnonymousUser')) {
          return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by') . ' <a href="mailto:'. $user->getEmail() .'">' . clean($user->getName()) . '</a>' . $comment_link;
        } else {
          return lang($action) . ' ' . $formatted_datetime . ' ' . lang('by unknown user') . $comment_link;
        } // if
    //BOF:mod 20120913
    */
    return lang($action) . ' ' . $formatted_datetime . ' ' . $comment_link;
    //EOF:mod 20120913
    //EOF:mod 20110708 ticketid202
}