Example #1
0
 public function deleteInstancesByOrder(CustomerOrder $order)
 {
     // remove other ExpressCheckout instances for this order
     $f = new ARDeleteFilter();
     $f->setCondition(new EqualsCond(new ARFieldHandle('ExpressCheckout', 'orderID'), $order->getID()));
     ActiveRecordModel::deleteRecordSet('ExpressCheckout', $f);
 }
Example #2
0
 public static function addNewRolesNames($roleNames, $deleteOther = false)
 {
     // unset meta- roles
     if ($i = array_search('login', $roleNames)) {
         unset($roleNames[$i]);
     }
     if (!is_array($roleNames) || empty($roleNames)) {
         return;
     }
     $filter = new ARSelectFilter();
     $deleteFilter = new ARDeleteFilter();
     $condition = new EqualsCond(new ARFieldHandle(__CLASS__, "name"), $roleNames[0]);
     $deleteCondition = new NotEqualsCond(new ARFieldHandle(__CLASS__, "name"), $roleNames[0]);
     foreach ($roleNames as $roleName) {
         $condition->addOR(new EqualsCond(new ARFieldHandle(__CLASS__, "name"), $roleName));
         $deleteCondition->addAnd(new NotEqualsCond(new ARFieldHandle(__CLASS__, "name"), $roleName));
     }
     $filter->setCondition($condition);
     $deleteFilter->setCondition($deleteCondition);
     if ($deleteOther) {
         self::deleteRecordSet(__CLASS__, $deleteFilter);
     }
     // Find new roles
     $invertedRoleNames = array_flip($roleNames);
     foreach (self::getRecordSet($filter) as $role) {
         if (isset($invertedRoleNames[$role->name->get()])) {
             unset($invertedRoleNames[$role->name->get()]);
         }
     }
     // Add new roles to database
     foreach ($invertedRoleNames as $role => $value) {
         if (!empty($role)) {
             $newRole = Role::getNewInstance($role);
             $newRole->save();
         }
     }
 }
Example #3
0
 /**
 *	@todo Rewrite this to use ARUpdateFilter or simply an SQL query to update all values
 
 		As in the original bug report:
 
 			The update query for merging value 799 into 808 would look something like this:
 
 			UPDATE SpecificationItem
 			LEFT JOIN SpecificationItem AS SecondItem ON SpecificationItem.productID=SecondItem.productID AND SecondItem.specFieldValueID=808
 			SET SpecificationItem.specFieldValueID=808
 			WHERE SpecificationItem.specFieldValueID=799 AND SecondItem.specFieldValueID IS NULL
 
 			- it would only update products that do not have value 808 already set (otherwise the query would error because of duplicate SpecificationItem records).
 
 			The remaining records with value 799 will be automatically removed (cascade) when SpecFieldValue 799 is deleted.
 */
 private function mergeFields()
 {
     if (empty($this->mergedFields)) {
         return true;
     }
     $itemClass = call_user_func(array($this->getFieldClass(), 'getSelectValueClass'));
     $valueColumn = call_user_func(array($itemClass, 'getValueIDColumnName'));
     $specificationItemSchema = self::getSchemaInstance($itemClass);
     $foreignKeys = $specificationItemSchema->getForeignKeyList();
     $specFieldReferenceFieldName = '';
     foreach ($foreignKeys as $foreignKey) {
         if ($foreignKey->getForeignClassName() == get_class($this)) {
             $specFieldReferenceFieldName = $foreignKey->getName();
             break;
         }
     }
     $thisSchema = self::getSchemaInstance(get_class($this));
     $primaryKeyList = $thisSchema->getPrimaryKeyList();
     $promaryKey = array_shift($primaryKeyList);
     $mergedFieldsIDs = array();
     foreach ($this->mergedFields as $mergedField) {
         $mergedFieldsIDs[] = $mergedField->getID();
     }
     $inAllItemsExceptThisCondition = new INCond(new ARFieldHandle(get_class($this), $promaryKey->getName()), $mergedFieldsIDs);
     $mergedFieldsIDs[] = $this->getID();
     $inAllItemsCondition = new INCond(new ARFieldHandle($itemClass, $specFieldReferenceFieldName), $mergedFieldsIDs);
     // Create filters
     $mergedSpecificationItemsFilter = new ARSelectFilter();
     $mergedSpecificationItemsFilter->setCondition($inAllItemsCondition);
     // Using IGNORE I'm ignoring duplicate primary keys. Those rows that violate the uniqueness of the primary key are simply not saved
     // Then later I just delete these records and the merge is complete.
     $sql = 'UPDATE IGNORE ' . $itemClass . ' SET ' . $valueColumn . ' = ' . $this->getID() . ' ' . $mergedSpecificationItemsFilter->createString();
     self::getLogger()->logQuery($sql);
     self::executeUpdate($sql);
     $mergedSpecFieldValuesDeleteFilter = new ARDeleteFilter();
     $mergedSpecFieldValuesDeleteFilter->setCondition($inAllItemsExceptThisCondition);
     ActiveRecord::deleteRecordSet(get_class($this), $mergedSpecFieldValuesDeleteFilter);
 }
Example #4
0
 public static function removeByZone(DeliveryZone $zone)
 {
     $filter = new ARDeleteFilter();
     $filter->setCondition(new EqualsCond(new ARFieldHandle(__CLASS__, 'deliveryZoneID'), $zone->getID()));
     return ActiveRecord::deleteRecordSet(__CLASS__, $filter);
 }
 public function save()
 {
     ActiveRecordModel::beginTransaction();
     $parent = Product::getInstanceByID($this->request->get('id'), true);
     $items = json_decode($this->request->get('items'), true);
     $types = json_decode($this->request->get('types'), true);
     $variations = json_decode($this->request->get('variations'), true);
     $existingTypes = $existingVariations = $existingItems = array();
     $currency = $this->application->getDefaultCurrencyCode();
     // deleted types
     foreach ($types as $id) {
         if (is_numeric($id)) {
             $existingTypes[] = $id;
         }
     }
     $parent->deleteRelatedRecordSet('ProductVariationType', new ARDeleteFilter(new NotINCond(new ARFieldHandle('ProductVariationType', 'ID'), $existingTypes)));
     // deleted variations
     foreach ($variations as $type => $typeVars) {
         foreach ($typeVars as $id) {
             if (is_numeric($id)) {
                 $existingVariations[] = $id;
             }
         }
     }
     $f = new ARDeleteFilter(new INCond(new ARFieldHandle('ProductVariation', 'typeID'), $existingTypes));
     $f->mergeCondition(new NotINCond(new ARFieldHandle('ProductVariation', 'ID'), $existingVariations));
     ActiveRecordModel::deleteRecordSet('ProductVariation', $f);
     // deleted items
     foreach ($items as $id) {
         if (is_numeric($id)) {
             $existingItems[] = $id;
         }
     }
     $parent->deleteRelatedRecordSet('Product', new ARDeleteFilter(new NotINCond(new ARFieldHandle('Product', 'ID'), $existingItems)));
     // load existing records
     foreach (array('Types' => 'ProductVariationType', 'Variations' => 'ProductVariation', 'Items' => 'Product') as $arr => $class) {
         $var = 'existing' . $arr;
         $array = ${$var};
         if ($array) {
             ActiveRecordModel::getRecordSet($class, new ARSelectFilter(new INCond(new ARFieldHandle($class, 'ID'), $array)));
         }
     }
     $idMap = array();
     // save types
     $request = $this->request->toArray();
     foreach ($types as $index => $id) {
         if (!is_numeric($id)) {
             $type = ProductVariationType::getNewInstance($parent);
             $idMap[$id] = $type;
         } else {
             $type = ActiveRecordModel::getInstanceByID('ProductVariationType', $id);
         }
         $type->setValueByLang('name', null, $request['variationType'][$index]);
         $type->position->set($index);
         if (!empty($request['typeLang_' . $id])) {
             foreach ($request['typeLang_' . $id] as $field => $value) {
                 list($field, $lang) = explode('_', $field, 2);
                 $type->setValueByLang($field, $lang, $value);
             }
         }
         $type->save();
     }
     // save variations
     $tree = array();
     $typeIndex = -1;
     foreach ($variations as $typeID => $typeVars) {
         $type = is_numeric($typeID) ? ActiveRecordModel::getInstanceByID('ProductVariationType', $typeID) : $idMap[$typeID];
         $typeIndex++;
         foreach ($typeVars as $index => $id) {
             if (!is_numeric($id)) {
                 $variation = ProductVariation::getNewInstance($type);
                 $idMap[$id] = $variation;
             } else {
                 $variation = ActiveRecordModel::getInstanceByID('ProductVariation', $id);
             }
             $variation->position->set($index);
             $variation->setValueByLang('name', null, $request['variation'][$id]);
             if (!empty($request['variationLang_' . $id])) {
                 foreach ($request['variationLang_' . $id] as $field => $value) {
                     list($field, $lang) = explode('_', $field, 2);
                     $variation->setValueByLang($field, $lang, $value);
                 }
             }
             $variation->save();
             $tree[$typeIndex][] = $variation;
         }
     }
     $images = array();
     // save items
     foreach ($items as $index => $id) {
         if (!is_numeric($id)) {
             $item = $parent->createChildProduct();
             $idMap[$id] = $item;
         } else {
             $item = ActiveRecordModel::getInstanceByID('Product', $id);
         }
         $item->isEnabled->set(!empty($request['isEnabled'][$id]));
         if (!$request['sku'][$index]) {
             $request['sku'][$index] = $item->sku->get();
         }
         foreach (array('sku', 'stockCount', 'shippingWeight') as $field) {
             if ($item->{$field}->get() || $request[$field][$index]) {
                 $item->{$field}->set($request[$field][$index]);
             }
         }
         $item->setChildSetting('weight', $request['shippingWeightType'][$index]);
         $item->setChildSetting('price', $request['priceType'][$index]);
         if (!strlen($request['priceType'][$index])) {
             $request['price'][$index] = '';
         }
         $item->setPrice($currency, $request['price'][$index]);
         $item->save();
         // assign variations
         $currentVariationValues = $currentVariations = array();
         foreach ($item->getRelatedRecordSet('ProductVariationValue') as $variationValue) {
             $currentVariations[$variationValue->variation->get()->getID()] = $variationValue->variation->get();
             $currentVariationValues[$variationValue->variation->get()->getID()] = $variationValue;
         }
         foreach ($this->getItemVariations($tree, $index) as $variation) {
             if (!isset($currentVariations[$variation->getID()])) {
                 ProductVariationValue::getNewInstance($item, $variation)->save();
             }
             unset($currentVariations[$variation->getID()]);
         }
         foreach ($currentVariations as $deletedVariation) {
             $currentVariationValues[$deletedVariation->getID()]->delete();
         }
         // set image
         if ($_FILES['image']['tmp_name'][$index]) {
             if ($item->defaultImage->get()) {
                 $item->defaultImage->get()->load();
                 $image = $item->defaultImage->get();
             } else {
                 $image = ProductImage::getNewInstance($item);
             }
             $image->save();
             $image->setFile($_FILES['image']['tmp_name'][$index]);
             $image->save();
             $images[$item->getID()] = $image->toArray();
             unset($images[$item->getID()]['Product']);
         }
     }
     ActiveRecordModel::commit();
     // pass ID's for newly created records
     $ids = array();
     foreach ($idMap as $id => $instance) {
         $ids[$id] = $instance->getID();
     }
     $response = new ActionResponse('ids', $ids);
     $response->set('parent', $parent->getID());
     $response->set('images', $images);
     $response->set('variationCount', $parent->getRelatedRecordCount('Product', new ARSelectFilter(new EqualsCond(new ARFieldHandle('Product', 'isEnabled'), true))));
     return $response;
 }
Example #6
0
 /**
  * Removes a persisted object (deletes from a database) by an uniqu identifier (record ID)
  *
  * @param string $className
  * @param mixed $recordID
  *
  * @todo remove / replace self::enumerateID method call
  */
 public static function deleteByID($className, $recordID)
 {
     $filter = new ARDeleteFilter();
     $schema = self::getSchemaInstance($className);
     $PKList = $schema->getPrimaryKeyList();
     $PKValueCond = null;
     if (!is_array($recordID)) {
         $PKFieldName = $PKList[key($PKList)]->getName();
         $PKValueCond = new EqualsCond(new ARFieldHandle($className, $PKFieldName), $recordID);
     } else {
         foreach ($PKList as $PK) {
             $cond = new EqualsCond(new ARFieldHandle($className, $PK->getName()), $recordID[$PK->getName()]);
             if (empty($PKValueCond)) {
                 $PKValueCond = $cond;
             } else {
                 $PKValueCond->addAND($cond);
             }
         }
     }
     $hash = self::getRecordHash($recordID);
     if (isset(self::$recordPool[$className][$hash])) {
         $record = self::$recordPool[$className][$hash];
         $record->markAsNotLoaded();
         $record->markAsDeleted();
     }
     $filter->setCondition($PKValueCond);
     self::deleteRecordSet($className, $filter, false);
 }
Example #7
0
 private function updateRoles()
 {
     @unlink($this->getRoleCacheFile());
     if (count($this->canceledRoles) > 0) {
         // Delete canceled associations
         $deleteFilter = new ARDeleteFilter();
         $condition = new EqualsCond(new ARFieldHandle('AccessControlAssociation', "userGroupID"), $this->getID());
         $roleConditions = new EqualsCond(new ARFieldHandle('AccessControlAssociation', "roleID"), reset($this->canceledRoles)->getID());
         foreach ($this->canceledRoles as $key => $role) {
             if ($role->isExistingRecord()) {
                 $roleConditions->addOR(new EqualsCond(new ARFieldHandle('AccessControlAssociation', "roleID"), $role->getID()));
             } else {
                 unset($this->canceledRoles[$key]);
             }
         }
         $condition->addAND($roleConditions);
         $deleteFilter->setCondition($condition);
         if (!empty($this->canceledRoles)) {
             AccessControlAssociation::deleteRecordSet('AccessControlAssociation', $deleteFilter);
         }
     }
     if (count($this->appliedRoles) > 0 && is_object(reset($this->appliedRoles))) {
         // adding new associations is a bit trickier
         // First, find all nodes that are already in DB
         // There is no point to apply them
         $appliedRolesFilter = new ARSelectFilter();
         $appliedIDs = array();
         $condition = new EqualsCond(new ARFieldHandle('AccessControlAssociation', "userGroupID"), $this->getID());
         $roleConditions = new EqualsCond(new ARFieldHandle('AccessControlAssociation', "roleID"), reset($this->appliedRoles)->getID());
         foreach ($this->appliedRoles as $key => $role) {
             if (is_object($role) && $role->isExistingRecord()) {
                 $roleConditions->addOR(new EqualsCond(new ARFieldHandle('AccessControlAssociation', "roleID"), $role->getID()));
             } else {
                 unset($this->appliedRoles[$key]);
             }
         }
         $condition->addAND($roleConditions);
         $appliedRolesFilter->setCondition($condition);
         // Unset already applied nodes
         foreach (AccessControlAssociation::getRecordSetByUserGroup($this, $appliedRolesFilter, self::LOAD_REFERENCES) as $assoc) {
             unset($this->appliedRoles[$assoc->role->get()->getID()]);
         }
         // Apply roles
         foreach ($this->appliedRoles as $role) {
             $assoc = AccessControlAssociation::getNewInstance($this, $role);
             $assoc->save();
         }
     }
 }