/**
  * Sets up "is_selected" in false, and also "is_active" in false in all
  * modules, except the passed in ones. It sets "is_selected" in true in all
  * passed modules,that exist in the database. It adds the modules, that don't
  * exist in the table. So that it should be specified in the inputted data
  * which data should be added as "active", and which of them shouldn't.
  *
  * Parameters have a complicated structure, because words Selected and Active
  * for modules depend: if a module is not Selected, it can't be Active.
  * And also because the status Selected/non-Selected is usually changed for
  * several modules at once. Administrator choses, which modules should be
  * Selected, which of them are non-Selected and then presses the button:
  * save the changes.
  */
 function setSelectedModules($modules, $modulesType, $b_settings_table_is_empty = false)
 {
     global $application;
     //The list of modules,which should become selected.
     $selected_modules_ids = array();
     foreach ($modules as $module_id => $module_settings) {
         $selected_modules_ids[] = "'" . $module_id . "'";
     }
     $tables = Checkout::getTables();
     $columns = $tables['checkout_pm_sm_settings']['columns'];
     $YES = "1";
     $NO = "2";
     //Sets up "is_selected" in false, and also "is_active" in false in all modules, except the passed in ones.
     if (count($selected_modules_ids) != 0) {
         $query = new DB_Update('checkout_pm_sm_settings');
         $query->addUpdateExpression($columns['status_selected_value_id'], $NO);
         $query->addUpdateExpression($columns['status_active_value_id'], $NO);
         $query->WhereField($columns['module_id'], DB_NIN, "(" . implode(",", $selected_modules_ids) . ")");
         $query->WhereAnd();
         $query->WhereValue($columns['module_group'], DB_EQ, $modulesType);
         $application->db->getDB_Result($query);
     }
     //Set "is_selected" in true, and also sort_order in all passed modules that exist in the database.
     //It adds the modules, that don't exist in the table. So that it should be specified in the inputted data
     //which data should be added as "active", and which of them shouldn't.
     //First get the list of modules, entered to the database:
     //_print($selected_modules_ids);die("full stop");
     if ($b_settings_table_is_empty === true) {
         $modules_already_in_db = array();
     } else {
         $modules_already_in_db = $this->getSelectedModules($modulesType);
     }
     foreach ($modules as $module_id => $module_settings) {
         if (!array_key_exists($module_id, $modules_already_in_db)) {
             //The module doesn't exist in the database.
             //Add module settings to the database.
             //        Checkout::getPaymentModuleInfo()       ,
             //                                   ,           isActive()
             //                      ,                               checkout_pm_sm_settings.
             $mm_info = Checkout::getInstalledModulesListData($modulesType, NULL, true);
             $m_name = "";
             foreach ($mm_info as $key => $info) {
                 if ($info->UUID == $module_settings['module_id']) {
                     $m_name = $info->name;
                     break;
                 }
             }
             $db_insert = new DB_Insert('checkout_pm_sm_settings');
             $db_insert->addInsertValue($module_settings['module_id'], $columns['module_id']);
             $db_insert->addInsertValue($m_name, $columns['module_class_name']);
             $db_insert->addInsertValue($module_settings['module_group'], $columns['module_group']);
             $db_insert->addInsertValue($NO, $columns['status_active_value_id']);
             $db_insert->addInsertValue($module_settings['b_is_selected'] === true ? $YES : $NO, $columns['status_selected_value_id']);
             $db_insert->addInsertValue($module_settings['sort_order'], $columns['sort_order']);
             $application->db->PrepareSQL($db_insert);
             //_print($modules);_print($module_settings);_print($application->db->QueryString);die;
             $application->db->DB_Exec();
             Checkout::update_pm_sm_currency_settings($module_settings['module_id'], modApiStaticFunc($m_name, "getInitialCurrencySettings"));
         } else {
             //The module exists in the database. Update sort_order and is_selected.
             $query = new DB_Update('checkout_pm_sm_settings');
             $query->addUpdateExpression($columns['status_selected_value_id'], $YES);
             $query->addUpdateExpression($columns['sort_order'], $module_settings['sort_order']);
             $query->WhereValue($columns['module_id'], DB_EQ, $module_id);
             $application->db->getDB_Result($query);
         }
     }
 }
 function linkTempEmails($key)
 {
     global $application;
     $tables = Subscriptions::getTables();
     $table = 'subscription_temp';
     $columns =& $tables[$table]['columns'];
     $atable = 'email_address';
     $acolumns =& $tables[$atable]['columns'];
     $query = new DB_Update($table);
     $query->addUpdateTable($atable);
     $query->addUpdateExpression($columns['email_id'], $acolumns['email_id']);
     $query->addUpdateValue($columns['state'], SUBSCRIPTION_TEMP_EXISTS);
     $query->WhereValue($columns['action_key'], DB_EQ, $key);
     $query->WhereAND();
     $query->WhereField($columns['email'], DB_EQ, $acolumns['email']);
     $application->db->getDB_Result($query);
     $query = new DB_Update($table);
     $query->addUpdateValue($columns['state'], SUBSCRIPTION_TEMP_DONT_EXISTS);
     $query->WhereValue($columns['action_key'], DB_EQ, $key);
     $query->WhereAND();
     $query->WhereValue($columns['state'], DB_EQ, SUBSCRIPTION_TEMP_UNKNOWN);
     $application->db->getDB_Result($query);
 }
 /**
  *                  : Active /   -active.
  *                -active,                                           .
  */
 function updateRowsFromQuantityDiscount($active_rates)
 {
     global $application;
     //                ,                             ,
     //                  View.                    ,               View.
     $YES = 1;
     //Active
     $NO = 2;
     //Disabled
     $not_active_rates_ids = array();
     $active_rates_ids = array();
     foreach ($active_rates as $rate_id => $status) {
         if ($status == $YES) {
             $active_rates_ids[] = $rate_id;
         } else {
             if ($status == $NO) {
                 $not_active_rates_ids[] = $rate_id;
             } else {
                 //: report error.
                 exit(1);
             }
         }
     }
     $tables = $this->getTables();
     $columns = $tables['quantity_discounts_rates_table']['columns'];
     $query = new DB_Update('quantity_discounts_rates_table');
     $query->addUpdateExpression($columns['b_active'], $YES);
     $query->WhereField($columns['id'], DB_IN, "('" . implode("','", $active_rates_ids) . "')");
     $application->db->getDB_Result($query);
     $query = new DB_Update('quantity_discounts_rates_table');
     $query->addUpdateExpression($columns['b_active'], $NO);
     $query->WhereField($columns['id'], DB_IN, "('" . implode("','", $not_active_rates_ids) . "')");
     $application->db->getDB_Result($query);
 }
 /**
  * Moves the category.
  *
  * @author Alexander Girin
  * @param integer $newParentCatId the new category id,
  *        to which the category is moved
  * @param integer $cid the moved category id
  * @return
  */
 function moveCategory($newParentCatId, $cid)
 {
     global $application;
     $NewParentCatInfo = $this->fetchCategoryInfo($newParentCatId);
     $CatInfo = $this->fetchCategoryInfo($cid);
     $catobj = new CCategoryInfo($cid);
     $CurrentParentCatInfo = $catobj->getCategoryTagValue('parentid');
     if ($NewParentCatInfo == FALSE || $CatInfo == FALSE) {
         return;
     }
     $tables = $this->getTables();
     $c = $tables['categories']['columns'];
     # categories is a database table
     $deltaLevel = -($CatInfo['level'] - 1) + $NewParentCatInfo['level'];
     if ($NewParentCatInfo['left'] < $CatInfo['left'] && $NewParentCatInfo['right'] > $CatInfo['right'] && $NewParentCatInfo['level'] < $CatInfo['level'] - 1) {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['right'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '+' . (($NewParentCatInfo['right'] - $CatInfo['right'] - $CatInfo['level'] + $NewParentCatInfo['level']) / 2 * 2 + $CatInfo['level'] - $NewParentCatInfo['level'] - 1), $c['right'])));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['left'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '+' . (($NewParentCatInfo['right'] - $CatInfo['right'] - $CatInfo['level'] + $NewParentCatInfo['level']) / 2 * 2 + $CatInfo['level'] - $NewParentCatInfo['level'] - 1), $c['left'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . ($NewParentCatInfo['left'] + 1) . "'" . ' ' . DB_AND . " '" . ($NewParentCatInfo['right'] - 1) . "'");
         $application->db->getDB_Result($query);
     } elseif ($NewParentCatInfo['left'] < $CatInfo['left']) {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $NewParentCatInfo['right'] . ' ' . DB_AND . ' ' . ($CatInfo['left'] - 1), $c['left'] . '+' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '-' . ($CatInfo['left'] - $NewParentCatInfo['right']), $c['left'])));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $NewParentCatInfo['right'] . ' ' . DB_AND . ' ' . $CatInfo['left'], $c['right'] . '+' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '-' . ($CatInfo['left'] - $NewParentCatInfo['right']), $c['right'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . $NewParentCatInfo['left'] . "'" . ' ' . DB_AND . " '" . $CatInfo['right'] . "'");
         $query->WhereOR();
         $query->WhereField($c['right'], DB_BETWEEN, "'" . $NewParentCatInfo['left'] . "'" . ' ' . DB_AND . " '" . $CatInfo['right'] . "'");
         $application->db->getDB_Result($query);
     } else {
         $query = new DB_Update('categories');
         $query->addUpdateExpression($c['level'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['level'] . sprintf('%+d', $deltaLevel), $c['level']));
         $query->addUpdateExpression($c['left'], $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['right'] . ' ' . DB_AND . ' ' . $NewParentCatInfo['right'], $c['left'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['left'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['left'] . '+' . ($NewParentCatInfo['right'] - $CatInfo['right'] - 1), $c['left'])));
         $query->addUpdateExpression($c['right'], $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . ($CatInfo['right'] + 1) . ' ' . DB_AND . ' ' . ($NewParentCatInfo['right'] - 1), $c['right'] . '-' . ($CatInfo['right'] - $CatInfo['left'] + 1), $query->fIf($c['right'] . ' ' . DB_BETWEEN . ' ' . $CatInfo['left'] . ' ' . DB_AND . ' ' . $CatInfo['right'], $c['right'] . '+' . ($NewParentCatInfo['right'] - $CatInfo['right'] - 1), $c['right'])));
         $query->WhereField($c['left'], DB_BETWEEN, "'" . $CatInfo['left'] . "'" . ' ' . DB_AND . " '" . $NewParentCatInfo['right'] . "'");
         $query->WhereOR();
         $query->WhereField($c['right'], DB_BETWEEN, "'" . $CatInfo['left'] . "'" . ' ' . DB_AND . " '" . $NewParentCatInfo['right'] . "'");
         $application->db->getDB_Result($query);
     }
     modApiFunc('EventsManager', 'throwEvent', 'CategoryMoved', array('OLD_PARENT_CATEGORY_ID' => $CurrentParentCatInfo, 'NEW_PARENT_CATEGORY_ID' => $newParentCatId, 'CATEGORY_ID_LIST' => $cid));
 }