Example #1
0
 /** Save context & attributes to database */
 protected function save()
 {
     if (($id =& $this->id) !== null) {
         throw new SystemException('Cannot save existent context!');
     }
     // save to database
     $id = self::EMPTY_CONTEXT_ID;
     if ($attributes = $this->attributes) {
         $snapshot = self::getSnapshot($attributes);
         $query = array('limit' => 1, 'select' => array('ID'), 'filter' => array('=SNAPSHOT' => $snapshot));
         if ($row = Internals\ContextTable::getList($query)->fetch()) {
             $id = (int) $row['ID'];
         } else {
             try {
                 $result = Internals\ContextTable::add(array('SNAPSHOT' => $snapshot));
                 if ($result->isSuccess()) {
                     $id = $result->getId();
                     foreach ($attributes as $attribute) {
                         // TODO resetContext if not success and return null!!!
                         $result = Internals\ContextAttributeTable::add(array('CONTEXT_ID' => $id, 'NAME' => $attribute['NAME'], 'VALUE' => $attribute['VALUE']));
                     }
                 } else {
                     throw new DB\SqlQueryException();
                 }
             } catch (DB\SqlQueryException $e) {
                 if ($row = Internals\ContextTable::getList($query)->fetch()) {
                     $id = (int) $row['ID'];
                 }
             }
         }
     }
 }
Example #2
0
 /** Save context & attributes to database */
 protected function save()
 {
     if (($id =& $this->id) !== null) {
         throw new SystemException('Cannot save existent context!');
     }
     $id = self::EMPTY_CONTEXT_ID;
     if ($attributes = $this->attributes) {
         // leave only one attribute in group
         static $groupedTypes;
         if (!$groupedTypes) {
             $groupedTypes = AttributeManager::getGroupedTypes();
             unset($groupedTypes[null]);
         }
         foreach ($groupedTypes as $types) {
             $intersection = array_intersect_key($types, $attributes);
             if (count($intersection) > 1) {
                 array_shift($intersection);
                 foreach ($intersection as $name => $type) {
                     unset($attributes[$name]);
                 }
             }
         }
         // save to database
         $snapshot = self::getSnapshot($attributes);
         $query = array('limit' => 1, 'select' => array('ID'), 'filter' => array('=SNAPSHOT' => $snapshot));
         if ($row = ContextTable::getList($query)->fetch()) {
             $id = (int) $row['ID'];
         } elseif (Option::get('conversion', 'CONTEXT_TABLE') != 'locked') {
             try {
                 $result = ContextTable::add(array('SNAPSHOT' => $snapshot));
                 if ($result->isSuccess()) {
                     $id = $result->getId();
                     foreach ($attributes as $name => $value) {
                         // TODO resetContext if not success and return null!!!
                         $result = ContextAttributeTable::add(array('CONTEXT_ID' => $id, 'NAME' => $name, 'VALUE' => (string) $value));
                     }
                 } else {
                     throw new DB\SqlQueryException();
                 }
             } catch (DB\SqlQueryException $e) {
                 if ($row = ContextTable::getList($query)->fetch()) {
                     $id = (int) $row['ID'];
                 }
             }
         }
     }
 }
Example #3
0
 private static function setAttributeFilter(Query $query, $field, $name, $value = null)
 {
     $query->registerRuntimeField(null, new ReferenceField($field, Internals\ContextAttributeTable::getEntity(), array('=this.CONTEXT_ID' => 'ref.CONTEXT_ID'), array('join_type' => 'INNER')));
     $query->addFilter("={$field}.NAME", $name);
     if ($value !== null) {
         $query->addFilter("={$field}.VALUE", $value);
     }
 }