public function display()
 {
     // permissions...
     if (!\Current_User::isDeity()) {
         \NQ::simple('intern', NotifyUI::ERROR, 'You cannot edit administrators.');
         return false;
     }
     // set up some stuff for the page template
     $tpl = array();
     // create the list of admins
     $adminList = Admin::getAdminPager();
     // get the list of departments
     $depts = Department::getDepartmentsAssoc();
     // make the form for adding a new admin
     $form = new \PHPWS_Form('add_admin');
     $form->addSelect('department_id', $depts);
     $form->setLabel('department_id', 'Department');
     $form->addText('username');
     $form->setLabel('username', 'Username');
     $form->addCheck('all');
     $form->setLabel('all', 'All Departments');
     $form->addSubmit('submit', 'Create Admin');
     $form->setAction('index.php?module=intern&action=edit_admins');
     $form->addHidden('add', 1);
     // TODO: Add Javascript autocomplete for usernames.
     javascript('jquery');
     javascript('jquery_ui');
     javascriptMod('intern', 'admin');
     $tpl['PAGER'] = $adminList;
     $form->mergeTemplate($tpl);
     return \PHPWS_Template::process($form->getTemplate(), 'intern', 'edit_admin.tpl');
 }
 /**
  * Grant user access to search and manage Department.
  */
 public static function add($username, $departmentId)
 {
     if (empty($username)) {
         return \NQ::simple('intern', NotifyUI::WARNING, 'No username entered.');
     }
     if ($departmentId == -1) {
         return \NQ::simple('intern', NotifyUI::WARNING, 'No department selected.');
     }
     // First check that the username passed in is a registered user.
     $db = new \PHPWS_DB('users');
     $db->addWhere('username', $username);
     $db->addColumn('id', $count = true);
     if (sizeof($db->select()) == 0) {
         // No user exists with that name.
         return \NQ::simple('intern', NotifyUI::ERROR, "No user exists with the name <i>{$username}</i>. Please choose a valid username.");
     }
     // Deity users automatically see every department. No need to add them to table.
     $db->reset();
     $db->addWhere('username', $username);
     $db->addWhere('deity', true);
     $db->addColumn('id', $count = true);
     if (sizeof($db->select()) >= 1) {
         // Is a deity.
         return \NQ::simple('intern', NotifyUI::WARNING, "<i>{$username}</i> can view all internships in all departments.");
     }
     $d = new Department($departmentId);
     // Check if user already has permission.
     if (self::allowed($username, $departmentId)) {
         // User permission has already been added.
         return \NQ::simple('intern', NotifyUI::WARNING, "<i>{$username}</i> can already view internships in <i>{$d->name}</i>.");
     }
     $ia = new Admin();
     $ia->username = $username;
     $ia->department_id = $departmentId;
     $ia->save();
     \NQ::simple('intern', NotifyUI::SUCCESS, "<i>{$username}</i> can now view internships for <i>{$d->name}</i>.");
 }