}
     }
     unset($pclf, $pc_obj);
     if ($current_user_failed == TRUE) {
         $pcf->Validator->isTrue('user', FALSE, TTi18n::gettext('You can not unassign yourself from a permission group, assign yourself to a new group instead'));
     }
 }
 if ($pcf->isValid()) {
     $pcf_id = $pcf->Save(FALSE);
     Debug::Text('aPermission Control ID: ' . $pcf_id, __FILE__, __LINE__, __METHOD__, 10);
     if ($pcf_id === TRUE) {
         $pcf_id = $data['id'];
     }
     if (DEMO_MODE == FALSE) {
         if (isset($data['user_ids'])) {
             $pcf->setUser($data['user_ids']);
         } else {
             $pcf->setUser(array());
         }
         //Don't Delete all previous permissions, do that in the Permission class.
         if (isset($data['permissions']) and is_array($data['permissions']) and count($data['permissions']) > 0) {
             $pcf->setPermission($data['permissions'], $old_data['permissions']);
         }
     }
     if ($pcf->isValid()) {
         $pcf->Save(TRUE);
         if (DEMO_MODE == FALSE) {
             if ($action == 'apply_preset') {
                 Debug::Text('Attempting to apply preset...', __FILE__, __LINE__, __METHOD__, 10);
                 if (!isset($data['preset_flags'])) {
                     $data['preset_flags'] = array();
 function postInstall()
 {
     global $cache;
     Debug::text('postInstall: ' . $this->getVersion(), __FILE__, __LINE__, __METHOD__, 9);
     Debug::text('l: ' . $this->getVersion(), __FILE__, __LINE__, __METHOD__, 9);
     /*
     	Take permission groups we put into memory from preInstall and create them now,
     	  after schema has been updated.
     */
     if (isset($this->permission_groups) and is_array($this->permission_groups)) {
         //Create permission groups and assign proper employees to each.
         //Debug::Arr($this->permission_groups, 'All Permission Groups: ', __FILE__, __LINE__, __METHOD__,9);
         foreach ($this->permission_groups as $company_id => $permission_group_data) {
             //Get all active users for this company, so we can assign them
             //to the default permission group.
             $ulf = new UserListFactory();
             $ulf->getByCompanyId($company_id);
             $all_user_ids = array_keys((array) $ulf->getArrayByListFactory($ulf, FALSE, TRUE));
             $assigned_user_ids = array();
             foreach ($permission_group_data as $group_name => $permission_data) {
                 Debug::text('zGroup Name: ' . $group_name, __FILE__, __LINE__, __METHOD__, 10);
                 $pcf = new PermissionControlFactory();
                 $pcf->StartTransaction();
                 $pcf->setCompany($company_id);
                 $pcf->setName(ucfirst($group_name));
                 $pcf->setDescription('Automatically Created By Installer');
                 if ($pcf->isValid()) {
                     $pcf_id = $pcf->Save(FALSE);
                     if (strtolower($group_name) == 'default') {
                         //Assign all unassigned users to this permission group.
                         $tmp_user_ids = array_merge((array) $this->permission_group_users[$company_id][$group_name], array_diff($all_user_ids, $assigned_user_ids));
                         //Debug::Arr($all_user_ids, 'Default Group All User IDs:', __FILE__, __LINE__, __METHOD__, 10);
                         //Debug::Arr($assigned_user_ids, 'Default Group All User IDs:', __FILE__, __LINE__, __METHOD__, 10);
                         //Debug::Arr($tmp_user_ids, 'Default Group User IDs:', __FILE__, __LINE__, __METHOD__, 10);
                         $pcf->setUser($tmp_user_ids);
                         unset($tmp_user_ids);
                     } else {
                         if (isset($this->permission_group_users[$company_id][$group_name]) and is_array($this->permission_group_users[$company_id][$group_name])) {
                             $pcf->setUser($this->permission_group_users[$company_id][$group_name]);
                             $assigned_user_ids = array_merge($assigned_user_ids, $this->permission_group_users[$company_id][$group_name]);
                         }
                     }
                     if (is_array($permission_data)) {
                         $pcf->setPermission($permission_data);
                     }
                 }
                 //$pcf->FailTransaction();
                 $pcf->CommitTransaction();
             }
             unset($all_user_ids, $assigned_user_ids);
         }
     }
     return TRUE;
 }
        if ($ulf->getRecordCount() == 1) {
            echo "Found user, apply administrator permissions...\n";
            ob_flush();
            $u_obj = $ulf->getCurrent();
            //Create new Permission Group just for this purpose.
            $pf = new PermissionFactory();
            $pf->StartTransaction();
            $preset_flags = array();
            $preset_options = $pf->getOptions('preset');
            $pcf = new PermissionControlFactory();
            $pcf->setCompany($u_obj->getCompany());
            $pcf->setName('Administrator Fix (' . rand(1, 1000) . ')');
            $pcf->setDescription('Created By set_admin_permissions.php');
            if ($pcf->isValid()) {
                $pcf_id = $pcf->Save(FALSE);
                $pcf->setUser(array($u_obj->getId()));
                $pcf->Save();
                if ($pf->applyPreset($pcf_id, 40, $preset_flags) == TRUE) {
                    echo "Success!\n";
                }
            }
            //$pf->FailTransaction();
            $pf->CommitTransaction();
        } elseif ($ulf->getRecordCount() > 2) {
            echo "Found more then one user with the same user name, not updating permissions!\n";
        } else {
            echo "User name not found!\n";
        }
    }
}
echo "WARNING: Clear TimeTrex cache after running this.\n";