/**
 * Render select company box
 * 
 * Parameters:
 * 
 * - value - Value of selected company
 * - optional - Is value of this field optional or not
 * - exclude - Array of company ID-s that will be excluded
 * - can_create_new - Should this select box offer option to create a new 
 *   company from within the list
 * 
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_select_company($params, &$smarty)
{
    static $ids = array();
    $companies = Companies::getIdNameMap(array_var($params, 'companies'));
    $value = array_var($params, 'value', null, true);
    $id = array_var($params, 'id', null, true);
    if (empty($id)) {
        $counter = 1;
        do {
            $id = "select_company_dropdown_{$counter}";
            $counter++;
        } while (in_array($id, $ids));
    }
    // if
    $ids[] = $id;
    $params['id'] = $id;
    $optional = array_var($params, 'optional', false, true);
    $exclude = array_var($params, 'exclude', array(), true);
    if (!is_array($exclude)) {
        $exclude = array();
    }
    // if
    $can_create_new = array_var($params, 'can_create_new', true, true);
    if ($optional) {
        $options = array(option_tag(lang('-- None --'), ''), option_tag('', ''));
    } else {
        $options = array();
    }
    // if
    foreach ($companies as $company_id => $company_name) {
        if (in_array($company_id, $exclude)) {
            continue;
        }
        // if
        $option_attributes = array('class' => 'object_option');
        if ($value == $company_id) {
            $option_attributes['selected'] = true;
        }
        // if
        $options[] = option_tag($company_name, $company_id, $option_attributes);
    }
    // if
    if ($can_create_new) {
        $logged_user = get_logged_user();
        if (instance_of($logged_user, 'User') && Company::canAdd($logged_user)) {
            $params['add_object_url'] = assemble_url('people_companies_quick_add');
            $params['object_name'] = 'company';
            $params['add_object_message'] = lang('Please insert new company name');
            $options[] = option_tag('', '');
            $options[] = option_tag(lang('New Company...'), '', array('class' => 'new_object_option'));
        }
        // if
    }
    // if
    return select_box($options, $params) . '<script type="text/javascript">$("#' . $id . '").new_object_from_select();</script>';
}
Пример #2
0
 /**
  * Fetch user details from database for provided user id-s
  *
  * @param array $user_ids
  * @return array
  */
 function findUsersDetails($user_ids)
 {
     if (!is_foreachable($user_ids)) {
         return false;
     }
     // if
     $users_table = TABLE_PREFIX . 'users';
     $users = array();
     $users = db_execute_all("SELECT {$users_table}.id, {$users_table}.first_name, {$users_table}.email, {$users_table}.last_name, {$users_table}.company_id FROM {$users_table} WHERE {$users_table}.id IN (?)", $user_ids);
     if (!is_foreachable($users)) {
         return false;
     }
     // if
     $companies = Companies::getIdNameMap();
     // Create a result array and make sure that owner company is the first
     // element in it
     $result = array(first($companies) => array());
     foreach ($users as $user) {
         if ($user['first_name'] && $user['last_name']) {
             $user['display_name'] = $user['first_name'] . ' ' . $user['last_name'];
         } elseif ($user['first_name']) {
             $user['display_name'] = $user['first_name'];
         } elseif ($user['last_name']) {
             $user['display_name'] = $user['last_name'];
         } else {
             $user['display_name'] = $user['email'];
         }
         // if
         $company_name = array_var($companies, $user['company_id']);
         if ($company_name) {
             if (isset($result[$company_name])) {
                 $result[$company_name][] = $user;
             } else {
                 $result[$company_name] = array($user);
             }
             // if
         }
         // if
     }
     // if
     ksort($result);
     return $result;
 }