function createPermissionGroups($company_id)
 {
     Debug::text('Adding Preset Permission Groups: ' . $company_id, __FILE__, __LINE__, __METHOD__, 9);
     $pf = new PermissionFactory();
     $pf->StartTransaction();
     if (getTTProductEdition() == 20) {
         $preset_flags = array('invoice' => 0, 'job' => 1, 'document' => 0);
         /*
         			$preset_flags = array(
         								'job' => 1,
         								'invoice' => 1,
         								'document' => 1,
         								);
         */
     } else {
         $preset_flags = array();
     }
     $preset_options = $pf->getOptions('preset');
     foreach ($preset_options as $preset_id => $preset_name) {
         $pcf = new PermissionControlFactory();
         $pcf->setCompany($company_id);
         $pcf->setName($preset_name);
         $pcf->setDescription('');
         if ($pcf->isValid()) {
             $pcf_id = $pcf->Save(FALSE);
             $this->permission_presets[$preset_id] = $pcf_id;
             $pf->applyPreset($pcf_id, $preset_id, $preset_flags);
         }
     }
     //$pf->FailTransaction();
     $pf->CommitTransaction();
 }
 function postSave()
 {
     $this->removeCache($this->getId());
     $this->remoteSave();
     if ($this->getDeleted() == FALSE) {
         //Add base currency for this new company.
         if ($this->getEnableAddCurrency() == TRUE) {
             $clf = new CurrencyListFactory();
             $clf->getByCompanyId($this->getId());
             if ($clf->getRecordCount() == 0) {
                 Debug::text('Adding Default Currency', __FILE__, __LINE__, __METHOD__, 9);
                 $cf = new CurrencyFactory();
                 $country_to_currency_map_arr = $cf->getOptions('country_currency');
                 if (isset($country_to_currency_map_arr[$this->getCountry()])) {
                     $base_currency = $country_to_currency_map_arr[$this->getCountry()];
                     Debug::text('Found Base Currency For Country: ' . $this->getCountry() . ' Currency: ' . $base_currency, __FILE__, __LINE__, __METHOD__, 9);
                 } else {
                     Debug::text('DID NOT Find Base Currency For Country: ' . $this->getCountry() . ' Using default USD.', __FILE__, __LINE__, __METHOD__, 9);
                     $base_currency = 'USD';
                 }
                 $cf->setCompany($this->getId());
                 $cf->setStatus(10);
                 $cf->setName($base_currency);
                 $cf->setISOCode($base_currency);
                 $cf->setConversionRate('1.000000000');
                 $cf->setAutoUpdate(FALSE);
                 $cf->setBase(TRUE);
                 $cf->setDefault(TRUE);
                 if ($cf->isValid()) {
                     $cf->Save();
                 }
             }
         }
         if ($this->getEnableAddPermissionGroupPreset() == TRUE) {
             Debug::text('Adding Preset Permission Groups', __FILE__, __LINE__, __METHOD__, 9);
             $pf = new PermissionFactory();
             $pf->StartTransaction();
             if ($this->getProductEdition() == 20) {
                 $preset_flags = array('job' => 1, 'invoice' => 1, 'document' => 1);
             } else {
                 $preset_flags = array();
             }
             $preset_options = $pf->getOptions('preset');
             foreach ($preset_options as $preset_id => $preset_name) {
                 $pcf = new PermissionControlFactory();
                 $pcf->setCompany($this->getId());
                 $pcf->setName($preset_name);
                 $pcf->setDescription('');
                 if ($pcf->isValid()) {
                     $pcf_id = $pcf->Save(FALSE);
                     $pf->applyPreset($pcf_id, $preset_id, $preset_flags);
                 }
             }
             //$pf->FailTransaction();
             $pf->CommitTransaction();
         }
         if ($this->getEnableAddStation() == TRUE) {
             Debug::text('Adding Default Station', __FILE__, __LINE__, __METHOD__, 9);
             //Enable punching in from all stations
             $sf = new StationFactory();
             $sf->setCompany($this->getId());
             $sf->setStatus(20);
             $sf->setType(10);
             $sf->setSource('ANY');
             $sf->setStation('ANY');
             $sf->setDescription('All stations');
             $sf->setGroupSelectionType(10);
             $sf->setBranchSelectionType(10);
             $sf->setDepartmentSelectionType(10);
             if ($sf->isValid()) {
                 $sf->Save();
             }
         }
         if ($this->getEnableAddPayStubEntryAccountPreset() == TRUE) {
             Debug::text('Adding Pay Stub Entry Account Presets', __FILE__, __LINE__, __METHOD__, 9);
             PayStubEntryAccountFactory::addPresets($this->getId());
         }
         if ($this->getEnableAddCompanyDeductionPreset() == TRUE) {
             Debug::text('Adding Company Deduction Presets', __FILE__, __LINE__, __METHOD__, 9);
             CompanyDeductionFactory::addPresets($this->getId());
         }
         if ($this->getEnableAddRecurringHolidayPreset() == TRUE) {
             Debug::text('Adding Recurring Holiday Presets', __FILE__, __LINE__, __METHOD__, 9);
             RecurringHolidayFactory::addPresets($this->getId(), $this->getCountry());
         }
     }
     if ($this->getDeleted() == TRUE) {
         $ulf = new UserListFactory();
         $ulf->getByCompanyId($this->getID());
         if ($ulf->getRecordCount() > 0) {
             $ulf->StartTransaction();
             foreach ($ulf as $u_obj) {
                 Debug::text('Deleting User ID: ' . $u_obj->getId(), __FILE__, __LINE__, __METHOD__, 9);
                 $u_obj->setDeleted(TRUE);
                 if ($u_obj->isValid()) {
                     $u_obj->Save();
                 }
             }
             $ulf->CommitTransaction();
         }
     }
     return TRUE;
 }
/*
 * Get FORM variables
 */
extract(FormVariables::GetVariables(array('action', 'id', 'data', 'group_id', 'old_data', 'src_user_id')));
$pcf = new PermissionControlFactory();
$action = Misc::findSubmitButton();
switch ($action) {
    case 'submit':
    case 'apply_preset':
        //Debug::setVerbosity( 11 );
        Debug::Text('Submit!', __FILE__, __LINE__, __METHOD__, 10);
        $pf = new PermissionFactory();
        $pcf->StartTransaction();
        $pcf->setId($data['id']);
        $pcf->setCompany($current_company->getId());
        $pcf->setName($data['name']);
        $pcf->setDescription($data['description']);
        //Check to make sure the currently logged in user is NEVER in the unassigned
        //user list. This prevents an administrator from accidently un-assigning themselves
        //from a group and losing all permissions.
        if (in_array($current_user->getId(), (array) $src_user_id)) {
            //Check to see if current user is assigned to another permission group.
            $current_user_failed = FALSE;
            $pclf = new PermissionControlListFactory();
            $pclf->getByCompanyIdAndUserId($current_company->getId(), $current_user->getId());
            if ($pclf->getRecordCount() == 0) {
                $current_user_failed = TRUE;
            } else {
                foreach ($pclf as $pc_obj) {
                    if ($pc_obj->getId() == $data['id']) {
                        $current_user_failed = TRUE;
 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;
 }
 $user_name = $argv[$last_arg];
 //Get user_id from user_name
 $ulf = new UserListFactory();
 $ulf->getByUserName($user_name);
 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";