/**
  * A static method for processing preference values from a UI form, and
  * updating the preference values in the database.
  *
  * @static
  * @param array $aElementNames An array of HTML form element names, which
  *                             are also the preference value names.
  * @param array $aCheckboxes   An array of the above HTML form element
  *                             names which are checkboxes, as these will not
  *                             be set in the form POST if unchecked, and
  *                             so need to be treated differently.
  * @return boolean True on success, false otherwise.
  */
 function processPreferencesFromForm($aElementNames, $aCheckboxes)
 {
     phpAds_registerGlobalUnslashed('token');
     if (!phpAds_SessionValidateToken($GLOBALS['token'])) {
         return false;
     }
     // Get all of the preference types that exist
     $aPreferenceTypes = array();
     $doPreferences = OA_Dal::factoryDO('preferences');
     $doPreferences->find();
     if ($doPreferences->getRowCount() < 1) {
         return false;
     }
     while ($doPreferences->fetch()) {
         $aPreference = $doPreferences->toArray();
         $aPreferenceTypes[$aPreference['preference_name']] = array('preference_id' => $aPreference['preference_id'], 'account_type' => $aPreference['account_type']);
     }
     // Are there any preference types in the system?
     if (empty($aPreferenceTypes)) {
         return false;
     }
     // Get the type of the current accout
     $currentAccountType = OA_Permission::getAccountType();
     // Get the current account's ID
     $currentAccountId = OA_Permission::getAccountId();
     // Get the parent account preferences
     $aParentPreferences = OA_Preferences::loadPreferences(false, true, true);
     // Prepare the preference values that should be saved or deleted
     $aSavePreferences = array();
     $aDeletePreferences = array();
     foreach ($aElementNames as $preferenceName) {
         // Ensure that the current account has permission to process
         // the preference type
         $access = OA_Preferences::hasAccess($currentAccountType, $aPreferenceTypes[$preferenceName]['account_type']);
         if ($access == false) {
             // Don't process this value
             continue;
         }
         // Register the HTML element value
         phpAds_registerGlobalUnslashed($preferenceName);
         // Is the HTML element value a checkbox, and unset?
         if (isset($aCheckboxes[$preferenceName]) && !isset($GLOBALS[$preferenceName])) {
             // Set the value of the element to the false string ""
             $GLOBALS[$preferenceName] = '';
         } else {
             if (isset($aCheckboxes[$preferenceName]) && $GLOBALS[$preferenceName]) {
                 // Set the value of the element to the true string "1"
                 $GLOBALS[$preferenceName] = '1';
             }
         }
         // Was the HTML element value set?
         if (isset($GLOBALS[$preferenceName])) {
             // Is the preference value different from the parent value?
             if (!isset($aParentPreferences[$preferenceName]) || $GLOBALS[$preferenceName] != $aParentPreferences[$preferenceName]) {
                 // The preference value is different from the parent, so it
                 // needs to be stored
                 $aSavePreferences[$preferenceName] = $GLOBALS[$preferenceName];
             } else {
                 if ($currentAccountType != OA_ACCOUNT_ADMIN) {
                     // The preference value is not different from the parent, so
                     // it should be deleted if not the admin account (in case it
                     // exists for the account, and so would not inherit correctly
                     // if the admin account changes preferences)
                     $aDeletePreferences[$preferenceName] = $GLOBALS[$preferenceName];
                 }
             }
         }
     }
     // Save the required preferences
     foreach ($aSavePreferences as $preferenceName => $preferenceValue) {
         $doAccount_preference_assoc = OA_Dal::factoryDO('account_preference_assoc');
         $doAccount_preference_assoc->account_id = $currentAccountId;
         $doAccount_preference_assoc->preference_id = $aPreferenceTypes[$preferenceName]['preference_id'];
         $doAccount_preference_assoc->find();
         if ($doAccount_preference_assoc->getRowCount() != 1) {
             // Insert the preference
             $doAccount_preference_assoc->value = $preferenceValue;
             $result = $doAccount_preference_assoc->insert();
             if ($result === false) {
                 return false;
             }
         } else {
             // Update the preference
             $doAccount_preference_assoc->fetch();
             $doAccount_preference_assoc->value = $preferenceValue;
             $result = $doAccount_preference_assoc->update();
             if ($result === false) {
                 return false;
             }
         }
     }
     // Delete the required preferences
     foreach ($aDeletePreferences as $preferenceName => $preferenceValue) {
         $doAccount_preference_assoc = OA_Dal::factoryDO('account_preference_assoc');
         $doAccount_preference_assoc->account_id = $currentAccountId;
         $doAccount_preference_assoc->preference_id = $aPreferenceTypes[$preferenceName]['preference_id'];
         $doAccount_preference_assoc->find();
         if ($doAccount_preference_assoc->getRowCount() == 1) {
             // Delete the preference
             $result = $doAccount_preference_assoc->delete();
             if ($result === false) {
                 return false;
             }
         }
     }
     return true;
 }
示例#2
0
 /**
  * A private method to set the required options for column-based output
  * of option items.
  *
  * @access private
  * @param array $aItem The column option to display.
  * @param array $aValue An array of the column values.
  */
 function _showStatsColumns($aItem, $aValue)
 {
     // Get all of the preference types that exist
     $aPreferenceTypes = array();
     $doPreferences = OA_Dal::factoryDO('preferences');
     $doPreferences->find();
     if ($doPreferences->getRowCount() >= 1) {
         while ($doPreferences->fetch()) {
             $aPreference = $doPreferences->toArray();
             $aPreferenceTypes[$aPreference['preference_name']] = array('preference_id' => $aPreference['preference_id'], 'account_type' => $aPreference['account_type']);
         }
     }
     // Get the type of the current accout
     $currentAccountType = OA_Permission::getAccountType();
     global $tabindex;
     $aItem['tabindex'] = $tabindex++;
     foreach ($aItem['rows'] as $key => $aRow) {
         if (isset($aValue[$aRow['name']]['base'])) {
             $aItem['rows'][$key]['value'] = $aValue[$aRow['name']]['base'];
         }
         if (isset($aValue[$aRow['name']]['label'])) {
             $aItem['rows'][$key]['label_value'] = $aValue[$aRow['name']]['label'];
         }
         if (isset($aValue[$aRow['name']]['rank'])) {
             $aItem['rows'][$key]['rank_value'] = $aValue[$aRow['name']]['rank'];
         }
         // Has the current account got access to edit this preference?
         $access = OA_Preferences::hasAccess($currentAccountType, $aPreferenceTypes[$aRow['name']]['account_type']);
         if ($access == false) {
             $aItem['rows'][$key]['disabled'] = true;
         }
     }
     $this->aOption[] = array('statscolumns.html' => $aItem);
     // Update the global tab index for the number of stats column rows added
     $rows = count($aItem['rows']);
     $tabindex += $rows * 3;
     // Not an exact increment of the tab index, but close enough!
 }