Exemple #1
0
 /**
  * Handles database errors
  *
  * Also migrates old Shop Customers to the User accounts and adds
  * all new settings
  * @return  boolean     false     Always!
  * @throws  Cx\Lib\Update_DatabaseException
  */
 static function errorHandler()
 {
     // Customer
     $table_name_old = DBPREFIX . "module_shop_customers";
     // If the old Customer table is missing, the migration has completed
     // successfully already
     if (!\Cx\Lib\UpdateUtil::table_exist($table_name_old)) {
         return false;
     }
     // Ensure that the ShopSettings (including \Cx\Core\Setting) and Order tables
     // are ready first!
     //DBG::log("Customer::errorHandler(): Adding settings");
     ShopSettings::errorHandler();
     //        \Cx\Core\Country\Controller\Country::errorHandler(); // Called by Order::errorHandler();
     Order::errorHandler();
     Discount::errorHandler();
     \Cx\Core\Setting\Controller\Setting::init('Shop', 'config');
     $objUser = \FWUser::getFWUserObject()->objUser;
     // Create new User_Profile_Attributes
     $index_notes = \Cx\Core\Setting\Controller\Setting::getValue('user_profile_attribute_notes', 'Shop');
     if (!$index_notes) {
         //DBG::log("Customer::errorHandler(): Adding notes attribute...");
         //            $objProfileAttribute = new \User_Profile_Attribute();
         $objProfileAttribute = $objUser->objAttribute->getById(0);
         //DBG::log("Customer::errorHandler(): NEW notes attribute: ".var_export($objProfileAttribute, true));
         $objProfileAttribute->setNames(array(1 => 'Notizen', 2 => 'Notes', 3 => 'Notes', 4 => 'Notes', 5 => 'Notes', 6 => 'Notes'));
         $objProfileAttribute->setType('text');
         $objProfileAttribute->setMultiline(true);
         $objProfileAttribute->setParent(0);
         $objProfileAttribute->setProtection(array(1));
         //DBG::log("Customer::errorHandler(): Made notes attribute: ".var_export($objProfileAttribute, true));
         if (!$objProfileAttribute->store()) {
             throw new \Cx\Lib\Update_DatabaseException("Failed to create User_Profile_Attribute 'notes'");
         }
         //Re initialize shop setting
         \Cx\Core\Setting\Controller\Setting::init('Shop', 'config');
         //DBG::log("Customer::errorHandler(): Stored notes attribute, ID ".$objProfileAttribute->getId());
         if (!(\Cx\Core\Setting\Controller\Setting::set('user_profile_attribute_notes', $objProfileAttribute->getId()) && \Cx\Core\Setting\Controller\Setting::update('user_profile_attribute_notes'))) {
             throw new \Cx\Lib\Update_DatabaseException("Failed to update User_Profile_Attribute 'notes' setting");
         }
         //DBG::log("Customer::errorHandler(): Stored notes attribute ID setting");
     }
     $index_group = \Cx\Core\Setting\Controller\Setting::getValue('user_profile_attribute_customer_group_id', 'Shop');
     if (!$index_group) {
         //            $objProfileAttribute = new \User_Profile_Attribute();
         $objProfileAttribute = $objUser->objAttribute->getById(0);
         $objProfileAttribute->setNames(array(1 => 'Kundenrabattgruppe', 2 => 'Discount group', 3 => 'Kundenrabattgruppe', 4 => 'Kundenrabattgruppe', 5 => 'Kundenrabattgruppe', 6 => 'Kundenrabattgruppe'));
         $objProfileAttribute->setType('text');
         $objProfileAttribute->setParent(0);
         $objProfileAttribute->setProtection(array(1));
         if (!$objProfileAttribute->store()) {
             throw new \Cx\Lib\Update_DatabaseException("Failed to create User_Profile_Attribute 'notes'");
         }
         //Re initialize shop setting
         \Cx\Core\Setting\Controller\Setting::init('Shop', 'config');
         if (!(\Cx\Core\Setting\Controller\Setting::set('user_profile_attribute_customer_group_id', $objProfileAttribute->getId()) && \Cx\Core\Setting\Controller\Setting::update('user_profile_attribute_customer_group_id'))) {
             throw new \Cx\Lib\Update_DatabaseException("Failed to update User_Profile_Attribute 'customer_group_id' setting");
         }
     }
     // For the migration, a temporary flag is needed in the orders table
     // in order to prevent mixing up old and new customer_id values.
     $table_order_name = DBPREFIX . "module_shop_orders";
     if (!\Cx\Lib\UpdateUtil::column_exist($table_order_name, 'migrated')) {
         $query = "\n                ALTER TABLE `{$table_order_name}`\n                  ADD `migrated` TINYINT(1) unsigned NOT NULL default 0";
         \Cx\Lib\UpdateUtil::sql($query);
     }
     // Create missing UserGroups for customers and resellers
     $objGroup = null;
     $group_id_customer = \Cx\Core\Setting\Controller\Setting::getValue('usergroup_id_customer', 'Shop');
     if ($group_id_customer) {
         $objGroup = \FWUser::getFWUserObject()->objGroup->getGroup($group_id_customer);
     }
     if (!$objGroup || $objGroup->EOF) {
         $objGroup = \FWUser::getFWUserObject()->objGroup->getGroups(array('group_name' => 'Shop Endkunden'));
     }
     if (!$objGroup || $objGroup->EOF) {
         $objGroup = new \UserGroup();
         $objGroup->setActiveStatus(true);
         $objGroup->setDescription('Online Shop Endkunden');
         $objGroup->setName('Shop Endkunden');
         $objGroup->setType('frontend');
     }
     //DBG::log("Group: ".var_export($objGroup, true));
     if (!$objGroup) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to create UserGroup for customers");
     }
     //DBG::log("Customer::errorHandler(): Made customer usergroup: ".var_export($objGroup, true));
     if (!$objGroup->store() || !$objGroup->getId()) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to store UserGroup for customers");
     }
     //DBG::log("Customer::errorHandler(): Stored customer usergroup, ID ".$objGroup->getId());
     \Cx\Core\Setting\Controller\Setting::set('usergroup_id_customer', $objGroup->getId());
     if (!\Cx\Core\Setting\Controller\Setting::update('usergroup_id_customer')) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to store UserGroup ID for customers");
     }
     $group_id_customer = $objGroup->getId();
     $objGroup = null;
     $group_id_reseller = \Cx\Core\Setting\Controller\Setting::getValue('usergroup_id_reseller', 'Shop');
     if ($group_id_reseller) {
         $objGroup = \FWUser::getFWUserObject()->objGroup->getGroup($group_id_reseller);
     }
     if (!$objGroup || $objGroup->EOF) {
         $objGroup = \FWUser::getFWUserObject()->objGroup->getGroups(array('group_name' => 'Shop Wiederverkäufer'));
     }
     if (!$objGroup || $objGroup->EOF) {
         $objGroup = new \UserGroup();
         $objGroup->setActiveStatus(true);
         $objGroup->setDescription('Online Shop Wiederverkäufer');
         $objGroup->setName('Shop Wiederverkäufer');
         $objGroup->setType('frontend');
     }
     if (!$objGroup) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to create UserGroup for resellers");
     }
     //DBG::log("Customer::errorHandler(): Made reseller usergroup: ".var_export($objGroup, true));
     if (!$objGroup->store() || !$objGroup->getId()) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to store UserGroup for resellers");
     }
     \Cx\Core\Setting\Controller\Setting::set('usergroup_id_reseller', $objGroup->getId());
     if (!\Cx\Core\Setting\Controller\Setting::update('usergroup_id_reseller')) {
         throw new \Cx\Lib\Update_DatabaseException("Failed to store UserGroup ID for resellers");
     }
     $group_id_reseller = $objGroup->getId();
     $default_lang_id = \FWLanguage::getDefaultLangId();
     $query = "\n            SELECT `customer`.`customerid`,\n                   `customer`.`prefix`, `customer`.`firstname`,\n                   `customer`.`lastname`,\n                   `customer`.`company`, `customer`.`address`,\n                   `customer`.`city`, `customer`.`zip`,\n                   `customer`.`country_id`,\n                   `customer`.`phone`, `customer`.`fax`,\n                   `customer`.`email`,\n                   `customer`.`username`, `customer`.`password`,\n                   `customer`.`company_note`,\n                   `customer`.`is_reseller`,\n                   `customer`.`customer_status`, `customer`.`register_date`,\n                   `customer`.`group_id`\n              FROM `{$table_name_old}` AS `customer`\n             ORDER BY `customer`.`customerid` ASC";
     $objResult = \Cx\Lib\UpdateUtil::sql($query);
     while (!$objResult->EOF) {
         $old_customer_id = $objResult->fields['customerid'];
         if (empty($objResult->fields['email'])) {
             $objResult->fields['email'] = $objResult->fields['username'];
         }
         $email = $objResult->fields['email'];
         $objUser = \FWUser::getFWUserObject()->objUser->getUsers(array('email' => array(0 => $email)));
         // TODO: See whether a User with that username (but different e-mail address) exists!
         $objUser_name = \FWUser::getFWUserObject()->objUser->getUsers(array('username' => array(0 => $objResult->fields['username'])));
         if ($objUser && $objUser_name) {
             $objUser = $objUser_name;
         }
         $objCustomer = null;
         if ($objUser) {
             $objCustomer = self::getById($objUser->getId());
         }
         if (!$objCustomer) {
             $lang_id = Order::getLanguageIdByCustomerId($old_customer_id);
             $lang_id = \FWLanguage::getLangIdByIso639_1($lang_id);
             if (!$lang_id) {
                 $lang_id = $default_lang_id;
             }
             $objCustomer = new Customer();
             if (preg_match('/^(?:frau|mad|mme|signora|miss)/i', $objResult->fields['prefix'])) {
                 $objCustomer->gender('gender_female');
             } elseif (preg_match('/^(?:herr|mon|signore|mister|mr)/i', $objResult->fields['prefix'])) {
                 $objCustomer->gender('gender_male');
                 //                } else {
                 // Other "genders", like "family", "thing", or "it" won't be matched
                 // and are left on "gender_unknown".
                 //DBG::log("*** Prefix {$objResult->fields['prefix']}, UNKNOWN GENDER!");
             }
             //DBG::log("Prefix {$objResult->fields['prefix']}, made gender ".$objCustomer->gender());
             $objCustomer->company($objResult->fields['company']);
             $objCustomer->firstname($objResult->fields['firstname']);
             $objCustomer->lastname($objResult->fields['lastname']);
             $objCustomer->address($objResult->fields['address']);
             $objCustomer->city($objResult->fields['city']);
             $objCustomer->zip($objResult->fields['zip']);
             $objCustomer->country_id($objResult->fields['country_id']);
             $objCustomer->phone($objResult->fields['phone']);
             $objCustomer->fax($objResult->fields['fax']);
             $objCustomer->email($objResult->fields['email']);
             $objCustomer->companynote($objResult->fields['company_note']);
             $objCustomer->active($objResult->fields['customer_status']);
             // Handled by a UserGroup now, see below
             //$objCustomer->setResellerStatus($objResult->fields['is_reseller']);
             $objCustomer->register_date($objResult->fields['register_date']);
             $objCustomer->group_id($objResult->fields['group_id']);
             // NOTE: Mind that the User class has been modified to accept e-mail addresses
             // as usernames!
             $objCustomer->username($objResult->fields['username']);
             // Copy the md5 hash of the password!
             $objCustomer->password = $objResult->fields['password'];
             $objCustomer->setFrontendLanguage($lang_id);
         }
         if ($objResult->fields['is_reseller']) {
             $objCustomer->setGroups($objCustomer->getAssociatedGroupIds() + array($group_id_reseller));
             //DBG::log("Customer::errorHandler(): Added reseller: ".$objCustomer->id());
         } else {
             $objCustomer->setGroups($objCustomer->getAssociatedGroupIds() + array($group_id_customer));
             //DBG::log("Customer::errorHandler(): Added customer: ".$objCustomer->id());
         }
         if (!$objCustomer->store()) {
             //DBG::log(var_export($objCustomer, true));
             throw new \Cx\Lib\Update_DatabaseException("Failed to migrate existing Customer ID " . $old_customer_id . " to Users (Messages: " . join(', ', $objCustomer->error_msg) . ")");
         }
         // Update the Orders table with the new Customer ID.
         // Note that we use the ambiguous old customer ID that may
         // coincide with a new User ID, so to prevent inconsistencies,
         // migrated Orders are marked as such.
         $query = "\n                UPDATE `{$table_order_name}`\n                   SET `customer_id`=" . $objCustomer->id() . ",\n                       `migrated`=1\n                 WHERE `customer_id`={$old_customer_id}\n                   AND `migrated`=0";
         \Cx\Lib\UpdateUtil::sql($query);
         // Drop migrated
         $query = "\n                DELETE FROM `{$table_name_old}`\n                 WHERE `customerid`={$old_customer_id}";
         \Cx\Lib\UpdateUtil::sql($query);
         $objResult->MoveNext();
         if (!checkMemoryLimit() || !checkTimeoutLimit()) {
             return false;
         }
     }
     // Remove the flag, it's no longer needed.
     // (You could also verify that all records have been migrated by
     // querying them with "[...] WHERE `migrated`=0", which *MUST* result
     // in an empty recordset.  This is left as an exercise for the reader.)
     $query = "\n            ALTER TABLE `{$table_order_name}`\n             DROP `migrated`";
     \Cx\Lib\UpdateUtil::sql($query);
     \Cx\Lib\UpdateUtil::drop_table($table_name_old);
     //DBG::log("Updated Customer table and related stuff");
     // Always
     return false;
 }
 private function processDigitalAssetManagementExtension($objUser)
 {
     global $_ARRAYLANG, $_CONFIG;
     if (empty($_POST['access_user_add_dma_category'])) {
         return true;
     }
     // TODO: Never used
     //        $objFWUser = \FWUser::getFWUserObject();
     $objDownloadLib = new \Cx\Modules\Downloads\Controller\DownloadsLibrary();
     $arrDownloadSettings = $objDownloadLib->getSettings();
     $objUser->setGroups(array_merge($objUser->getAssociatedGroupIds(), array_map('trim', explode(',', $arrDownloadSettings['associate_user_to_groups']))));
     $objUser->store();
     $firstname = $objUser->getProfileAttribute('firstname');
     $lastname = $objUser->getProfileAttribute('lastname');
     $userName = !empty($firstname) || !empty($lastname) ? trim($firstname . ' ' . $lastname) : $objUser->getUsername();
     $objGroup = new \UserGroup();
     $objGroup->setName(sprintf($_ARRAYLANG['TXT_ACCESS_CUSTOMER_TITLE'], $userName));
     $objGroup->setDescription(sprintf($_ARRAYLANG['TXT_ACCESS_USER_ACCOUNT_GROUP_DESC'], $userName));
     $objGroup->setActiveStatus(true);
     $objGroup->setType('frontend');
     $objGroup->setUsers(array($objUser->getId()));
     $objGroup->setDynamicPermissionIds(array());
     $objGroup->setStaticPermissionIds(array());
     if (!$objGroup->store()) {
         self::$arrStatusMsg['error'] = array_merge(self::$arrStatusMsg['error'], $objGroup->getErrorMsg());
         return false;
     }
     $arrLanguageIds = array_keys(\FWLanguage::getLanguageArray());
     $arrNames = array();
     $arrDescription = array();
     foreach ($arrLanguageIds as $langId) {
         $arrNames[$langId] = sprintf($_ARRAYLANG['TXT_ACCESS_CUSTOMER_TITLE'], $userName);
         $arrDescription[$langId] = '';
     }
     $objCategory = new \Cx\Modules\Downloads\Controller\Category();
     $objCategory->setActiveStatus(true);
     $objCategory->setVisibility(false);
     $objCategory->setNames($arrNames);
     $objCategory->setDescriptions($arrDescription);
     $objCategory->setOwner($objUser->getId());
     $objCategory->setDeletableByOwner(false);
     $objCategory->setModifyAccessByOwner(false);
     $objCategory->setPermissions(array('read' => array('protected' => true, 'groups' => array($objGroup->getId())), 'add_subcategories' => array('protected' => true, 'groups' => array($objGroup->getId())), 'manage_subcategories' => array('protected' => true, 'groups' => array($objGroup->getId())), 'add_files' => array('protected' => true, 'groups' => array($objGroup->getId())), 'manage_files' => array('protected' => true, 'groups' => array($objGroup->getId()))));
     if (!$objCategory->store()) {
         self::$arrStatusMsg['error'] = array_merge(self::$arrStatusMsg['error'], $objCategory->getErrorMsg());
         return false;
     }
     $damCategoryUri = '?cmd=Downloads&act=categories&parent_id=' . $objCategory->getId();
     $damCategoryAnchor = '<a href="' . $damCategoryUri . '">' . htmlentities($objCategory->getName(LANG_ID), ENT_QUOTES, CONTREXX_CHARSET) . '</a>';
     self::$arrStatusMsg['ok'][] = sprintf($_ARRAYLANG['TXT_ACCESS_NEW_DAM_CATEGORY_CREATED_TXT'], contrexx_raw2xhtml($objUser->getUsername()), $damCategoryAnchor);
     return true;
 }