Beispiel #1
0
/**
 * Returns a control to select multiple users or groups
 *
 * @param string $name
 * 		Name for the control
 * @param array $workspaces
 * 		Array of workspaces to choose from. If null the workspaces from the WorkspacePanel will be loaded.
 * @param array $selected
 * 		Array of workspaces selected by default
 * @return string
 * 		HTML for the control
 */
function select_users_or_groups($name = "", $users = null, $selected = null, $id = null)
{
    require_javascript('og/UserGroupPicker.js');
    if (!isset($id)) {
        $id = gen_id();
    }
    $selectedCSV = "";
    if (is_array($selected)) {
        foreach ($selected as $s) {
            if ($s instanceof Project) {
                if ($selectedCSV != "") {
                    $selectedCSV .= ",";
                }
                $selectedCSV .= $s->getId();
            }
        }
    }
    $json = array();
    if (logged_user()->isMemberOfOwnerCompany()) {
        $companies = Companies::findAll(array('order' => 'name ASC'));
    } else {
        $companies = array(owner_company(), logged_user()->getCompany());
    }
    foreach ($companies as $company) {
        $company_users = $company->getUsers();
        if (count($company_users) > 0) {
            $json[] = array('p' => 'users', 't' => 'company', 'id' => 'c' . $company->getId(), 'n' => $company->getName());
            foreach ($company_users as $u) {
                $json[] = array('p' => 'c' . $company->getId(), 't' => 'user', 'g' => $u->isGuest() ? 1 : 0, 'id' => $u->getId(), 'n' => $u->getDisplayName());
            }
        }
    }
    $groups = Groups::findAll(array('order' => 'name ASC'));
    foreach ($groups as $group) {
        $json[] = array('p' => 'groups', 't' => 'group', 'id' => $group->getId(), 'n' => $group->getName());
    }
    $jsonUsers = json_encode($json);
    $output = "<div id=\"{$id}-user-picker\"></div>\n\t\t\t<input id=\"{$id}-field\" type=\"hidden\" value=\"{$selectedCSV}\" name=\"{$name}\"></input>\n\t\t<script>\n\t\tvar userPicker = new og.UserPicker({\n\t\t\trenderTo: '{$id}-user-picker',\n\t\t\tfield: '{$id}-field',\n\t\t\tid: '{$id}',\n\t\t\tusers: {$jsonUsers},\n\t\t\theight: 320,\n\t\t\twidth: 210\n\t\t});\n\t\t</script>\n\t";
    return $output;
}
 /**
  * Returns all groups a user belongs to
  *
  * @param $user_id
  * @return unknown
  */
 static function getGroupsByUser($user_id)
 {
     return Groups::findAll(array('conditions' => array('`id` IN (SELECT `group_id` FROM `' . TABLE_PREFIX . 'group_users` WHERE `user_id` = ?)', $user_id)));
 }
 /**
  * Return all registered groups
  *
  * @param void
  * @return array
  */
 static function getAll()
 {
     return Groups::findAll();
     // findAll
 }