function setPermission($permission_arr, $old_permission_arr = array())
 {
     if ($this->getId() == FALSE) {
         return FALSE;
     }
     $pf = new PermissionFactory();
     //Don't Delete all previous permissions, do that in the Permission class.
     if (isset($permission_arr) and is_array($permission_arr) and count($permission_arr) > 0) {
         foreach ($permission_arr as $section => $permissions) {
             Debug::Text('  Section: ' . $section, __FILE__, __LINE__, __METHOD__, 10);
             foreach ($permissions as $name => $value) {
                 Debug::Text('     Name: ' . $name . ' - Value: ' . $value, __FILE__, __LINE__, __METHOD__, 10);
                 if ((!isset($old_permission_arr[$section][$name]) or isset($old_permission_arr[$section][$name]) and $value != $old_permission_arr[$section][$name]) and $pf->isIgnore($section, $name, $this->getCompanyObject()->getProductEdition()) == FALSE) {
                     if ($value == 0 or $value == 1) {
                         Debug::Text('    Modifying/Adding Permission: ' . $name . ' - Value: ' . $value, __FILE__, __LINE__, __METHOD__, 10);
                         $tmp_pf = new PermissionFactory();
                         $tmp_pf->setPermissionControl($this->getId());
                         $tmp_pf->setSection($section);
                         $tmp_pf->setName($name);
                         $tmp_pf->setValue((int) $value);
                         if ($tmp_pf->isValid()) {
                             $tmp_pf->save();
                         }
                     }
                 } else {
                     Debug::Text('     Permission didnt change... Skipping', __FILE__, __LINE__, __METHOD__, 10);
                 }
             }
         }
     }
     return TRUE;
 }
 function applyPreset($permission_control_id, $preset, $preset_flags)
 {
     $preset_permissions = $this->getPresetPermissions($preset, $preset_flags);
     if (!is_array($preset_permissions)) {
         return FALSE;
     }
     $this->setPermissionControl($permission_control_id);
     $pf = new PermissionFactory();
     $pf->StartTransaction();
     //Delete all previous permissions for this user.
     $this->deletePermissions($this->getCompany(), $permission_control_id);
     foreach ($preset_permissions as $section => $permissions) {
         foreach ($permissions as $name => $value) {
             Debug::Text('Setting Permission - Section: ' . $section . ' Name: ' . $name . ' Value: ' . (int) $value, __FILE__, __LINE__, __METHOD__, 10);
             $pf->setPermissionControl($permission_control_id);
             $pf->setSection($section);
             $pf->setName($name);
             $pf->setValue((int) $value);
             if ($pf->isValid()) {
                 $pf->save();
             }
         }
     }
     //Clear cache for all users assigned to this permission_control_id
     $pclf = new PermissionControlListFactory();
     $pclf->getById($permission_control_id);
     if ($pclf->getRecordCount() > 0) {
         $pc_obj = $pclf->getCurrent();
         if (is_array($pc_obj->getUser())) {
             foreach ($pc_obj->getUser() as $user_id) {
                 $pf->clearCache($user_id, $this->getCompany());
             }
         }
     }
     unset($pclf, $pc_obj, $user_id);
     //$pf->FailTransaction();
     $pf->CommitTransaction();
     return TRUE;
 }