예제 #1
0
 public function setUp()
 {
     parent::setUp();
     ActiveRecordModel::executeUpdate('DELETE FROM ClonedStore');
     $this->usd = ActiveRecordModel::getNewInstance('Currency');
     $this->usd->setID('ZZZ');
     $this->usd->save(ActiveRecord::PERFORM_INSERT);
     @unlink(ClonedStoreUpdater::getTimestampFile());
     @unlink(ClonedStoreUpdater::getIDFile());
     // create stores
     for ($k = 0; $k <= 0; $k++) {
         $this->stores[$k] = ClonedStore::getNewInstance();
         $this->stores[$k]->domain->set($k);
         $this->stores[$k]->save();
         echo $this->stores[$k]->lastImport->get();
     }
     // create categories
     $root = Category::getRootNode();
     for ($k = 0; $k <= 5; $k++) {
         $this->categories[] = $this->createCategory($root, $k);
     }
     $this->categories['1.1'] = $this->createCategory($this->categories[1], '1.1');
     $this->categories['1.2'] = $this->createCategory($this->categories[1], '1.2');
     $this->categories['1.2.1'] = $this->createCategory($this->categories['1.2'], '1.2.1');
     $this->categories['1.2.2'] = $this->createCategory($this->categories['1.2'], '1.2.2');
     usleep(1500000);
 }
예제 #2
0
 public function add()
 {
     $clonedStore = ClonedStore::getNewInstance();
     $clonedStore->domain->set($this->translate('newstore.com'));
     $clonedStore->save();
     return new JSONResponse($clonedStore->toArray());
 }
예제 #3
0
 public function executeTextReplaceInSnapshot(ClonedStore $store)
 {
     $replace = array();
     $f = select(eq(f('ClonedStoreRule.type'), ClonedStoreRule::TYPE_TEXT));
     $f->setOrder(f('ClonedStoreRule.ID'));
     foreach ($store->getRelatedRecordSet('ClonedStoreRule', $f) as $rule) {
         list($entity, $field) = explode('.', $rule->field->get());
         $ruleData = $rule->toArray();
         $ruleData['entity'] = $entity;
         $ruleData['field'] = $field;
         $replace[$entity][] = $ruleData;
     }
     echo '<span style="font-size: 2px;">';
     foreach ($replace as $table => $rules) {
         echo '<!--';
         var_dump($rules);
         echo '-->';
         flush();
         //exit;
         $this->flush($table);
         $this->flush($rules);
         if ('specField' == $table) {
             $table = 'SpecificationStringValue';
         }
         $extraFields = '';
         if (in_array($table, array('SpecificationStringValue', 'Product'))) {
             foreach (ActiveRecordModel::getDataBySQL('SELECT * FROM ' . $this->importDatabase . '.SpecField') as $field) {
                 $extraFields .= ',(SELECT SpecificationStringValue.value FROM ' . $this->importDatabase . '.SpecificationStringValue WHERE productID=Product.ID AND SpecificationStringValue.specFieldID=' . $field['ID'] . ') AS field_' . $field['handle'];
             }
         }
         foreach (array('name', 'longDescription', 'shortDescription') as $parentField) {
             $extraFields .= ', IF(Product.parentID IS NOT NULL, ParentProduct.' . $parentField . ', Product.' . $parentField . ') AS ' . $parentField;
         }
         if ('SpecificationStringValue' == $table) {
             $sql = 'SELECT Product.*, Product.ID AS productID, Product.parentID AS parentProductID, SpecField.ID AS specFieldID, (SELECT value FROM SpecificationStringValue WHERE SpecificationStringValue.productID=Product.ID AND SpecificationStringValue.specFieldID=SpecField.ID) AS value, (SELECT productID FROM SpecificationStringValue WHERE SpecificationStringValue.productID=Product.ID AND SpecificationStringValue.specFieldID=SpecField.ID) AS fieldValueExists' . $extraFields . ' FROM ' . $this->importDatabase . '.Product INNER JOIN ' . $this->importDatabase . '.SpecField LEFT JOIN ' . $this->importDatabase . '.Product AS ParentProduct ON (Product.parentID=ParentProduct.ID)';
             //$sql = 'SELECT *' . $extraFields . ' FROM ' . $this->importDatabase . '.SpecField LEFT JOIN ' . $this->importDatabase . '.' . $table . ' LEFT JOIN Product ON productID=Product.ID';
         } else {
             if ('Product' == $table) {
                 $sql = 'SELECT Product.*' . $extraFields . ' FROM ' . $this->importDatabase . '.' . $table . ' LEFT JOIN Product AS ParentProduct ON (Product.parentID=ParentProduct.ID)';
             } else {
                 $sql = 'SELECT * FROM ' . $this->importDatabase . '.' . $table;
             }
         }
         $offset = 0;
         $batch = 5000;
         $modified = array();
         do {
             $select = $sql . ' LIMIT ' . $batch * $offset . ',' . $batch;
             $offset++;
             $rows = ActiveRecordModel::getDataBySQL($select);
             foreach ($rows as $row) {
                 $original = $row;
                 $id = array();
                 $values = array();
                 foreach ($row as $key => $val) {
                     if (@unserialize($val)) {
                         $data = unserialize($val);
                         $value = array_shift($data);
                     } else {
                         $value = $val;
                     }
                     $values[$key] = $value;
                 }
                 if (!empty($modified[$values['ID']])) {
                     $values = array_merge($values, $modified[$values['ID']]);
                 }
                 $hasQuery = false;
                 foreach ($rules as $rule) {
                     $f = $rule['field'];
                     $identifier = $f;
                     //						var_dump($f);
                     if ($rule['query']) {
                         $productID = 'SpecificationStringValue' == $table ? $original['productID'] : $original['ID'];
                         $count = ActiveRecordModel::getDataBySQL('SELECT COUNT(*) AS cnt FROM ' . $this->importDatabase . '.Product WHERE ID=' . $productID . ' AND ' . $rule['query']);
                         $count = array_pop($count);
                         if (!$count['cnt']) {
                             continue;
                         }
                         $hasQuery = true;
                     }
                     if ('SpecificationStringValue' == $table) {
                         if ($row['specFieldID'] != $f) {
                             continue;
                         } else {
                             $f = 'value';
                         }
                         if ($rule['field']) {
                             try {
                                 $spFld = SpecField::getInstanceByID($rule['field'], true);
                                 if ($spFld) {
                                     $identifier = 'field_' . $spFld->handle->get();
                                 }
                             } catch (Exception $e) {
                                 $identifier = '';
                             }
                         }
                     }
                     if (@unserialize($row[$f])) {
                         $data = unserialize($row[$f]);
                         foreach ($data as $key => $value) {
                             $data[$key] = $this->replace($rule, $value, $values);
                             $values[$identifier] = $data[$key];
                             $modified[$values['ID']][$identifier] = $data[$key];
                         }
                         $row[$f] = serialize($data);
                     } else {
                         $row[$f] = $this->replace($rule, $row[$f], $values);
                         $values[$identifier] = $row[$f];
                         $modified[$values['ID']][$identifier] = $row[$f];
                         if ('SpecificationStringValue' == $table) {
                             $row[$f] = serialize(array('en' => $row[$f]));
                         }
                     }
                 }
                 $row = array_diff($row, $original);
                 if (!$row) {
                     continue;
                 }
                 foreach ($row as $field => $value) {
                     $value = "'" . addslashes($value) . "'";
                     if (substr($field, 0, 6) != 'field_') {
                         $row[$field] = $field . '=' . $value;
                     } else {
                         unset($row[$field]);
                     }
                 }
                 foreach ($original as $field => $value) {
                     if (strpos($field, 'ID') !== false && $value) {
                         $value = "'" . addslashes($value) . "'";
                         $id[] = $field . '=' . $value;
                     }
                 }
                 if ('SpecificationStringValue' == $table) {
                     $exists = $original['fieldValueExists'];
                     $row = array('value' => $row['value']);
                     $id = array('productID' => 'productID=' . $original['productID'], 'specFieldID' => 'specFieldID=' . $original['specFieldID']);
                     $row = array_merge($row, $id);
                     if ($exists) {
                         $query = 'UPDATE ' . $this->importDatabase . '.' . $table . ' SET ' . implode(', ', $row) . ' WHERE ' . implode(' AND ', $id);
                     } else {
                         $query = 'INSERT INTO ' . $this->importDatabase . '.' . $table . ' SET ' . implode(', ', $row);
                     }
                     if (17 == $original['specFieldID']) {
                         //echo '<pre>' . htmlspecialchars($query) . '</pre>' . '<Br /><Br />';
                     }
                     try {
                         ActiveRecordModel::executeUpdate($query);
                     } catch (Exception $e) {
                         echo '<div style="color:red;">' . htmlspecialchars($query) . '</div><Br /><Br />';
                     }
                 } else {
                     $query = 'UPDATE ' . $this->importDatabase . '.' . $table . ' SET ' . implode(', ', $row) . ' WHERE ' . implode(' AND ', $id);
                     ActiveRecordModel::executeUpdate($query);
                 }
                 echo '. ';
                 flush();
             }
         } while ($rows);
     }
     echo '</span>';
 }