00001 <?php
00002
00012 class SDRValue extends ModelBase {
00014 public static $ATTRIBUTES = array(
00015 'id' => array(
00016 'nullable' => false,
00017 'commit' => SModel2::COMMIT_NEVER,
00018 'type' => 'int'
00019 ),
00020 'type' => array(
00021 'type' => 'string'
00022 )
00023 );
00024
00033 public function __construct($constraints = null) {
00034 parent::__construct();
00035
00036 $this->registerAttributes(self::$ATTRIBUTES);
00037 $this->registerTableName('SDRValue');
00038 $this->registerPrimaryKey('id', true);
00039
00040 if($constraints !== null)
00041 $this->populate($constraints);
00042 }
00043
00055 public static function retrieve($constraints = array(), $checkOnly = false) {
00056 $list = self::getList($constraints, array(1), array(), $checkOnly);
00057 if(!$list || count($list) == 0)
00058 return $checkOnly ? false : null;
00059 else if($checkOnly)
00060 return $list > 0;
00061 else
00062 return $list[0];
00063 }
00064
00073 public static function exists($constraints = array()) {
00074 return self::retrieve($constraints, true);
00075 }
00076
00090 public static function getList($constraints = array(), $limit = array(), $order = array(), $count = false) {
00091 return parent::getListBase(self::getDBI(), $constraints, $limit, $order, $count, 'SDRValue', true);
00092 }
00093
00103 public static function getAttributesStatic() {
00104 return self::$ATTRIBUTES;
00105 }
00106
00112 public static function getTableNameStatic() {
00113 return 'SDRValue';
00114 }
00115
00116
00133 public function listSDRVersionFieldValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00134 return parent::getListBase(self::getDBI(),
00135 array_merge($constraints, array('valueId' => $this->id)),
00136 $limit, $order, $count, 'SDRVersionFieldValue', true);
00137 }
00138
00157 public function listSDRVersionFieldValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00158 return parent::getListBaseMulti(self::getDBI(),
00159 array_merge($constraints, array('Base.valueId' => $this->id)),
00160 $limit, $order, $count, 'SDRVersionFieldValue', true, $depth);
00161 }
00162
00178 public function addSDRVersionFieldValue($version, $field) {
00179 $obj = new SDRVersionFieldValue();
00180
00181 if(is_object($version))
00182 $obj->version = $version;
00183 else
00184 $obj->versionId = $version;
00185 if(is_object($field))
00186 $obj->field = $field;
00187 else
00188 $obj->fieldId = $field;
00189 $obj->valueId = $this->id;
00190
00191 if(!$obj->commit())
00192 return null;
00193
00194 return $obj;
00195 }
00196
00213 public function listSDRDateValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00214 return parent::getListBase(self::getDBI(),
00215 array_merge($constraints, array('valueId' => $this->id)),
00216 $limit, $order, $count, 'SDRDateValue', true);
00217 }
00218
00237 public function listSDRDateValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00238 return parent::getListBaseMulti(self::getDBI(),
00239 array_merge($constraints, array('Base.valueId' => $this->id)),
00240 $limit, $order, $count, 'SDRDateValue', true, $depth);
00241 }
00242
00257 public function addSDRDateValue($entry = '') {
00258 $obj = new SDRDateValue();
00259
00260 $obj->valueId = $this->id;
00261 $obj->entry = $entry;
00262
00263 if(!$obj->commit())
00264 return null;
00265
00266 return $obj;
00267 }
00268
00285 public function listSDRFieldValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00286 return parent::getListBase(self::getDBI(),
00287 array_merge($constraints, array('valueId' => $this->id)),
00288 $limit, $order, $count, 'SDRFieldValue', true);
00289 }
00290
00309 public function listSDRFieldValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00310 return parent::getListBaseMulti(self::getDBI(),
00311 array_merge($constraints, array('Base.valueId' => $this->id)),
00312 $limit, $order, $count, 'SDRFieldValue', true, $depth);
00313 }
00314
00329 public function addSDRFieldValue($field) {
00330 $obj = new SDRFieldValue();
00331
00332 if(is_object($field))
00333 $obj->field = $field;
00334 else
00335 $obj->fieldId = $field;
00336 $obj->valueId = $this->id;
00337
00338 if(!$obj->commit())
00339 return null;
00340
00341 return $obj;
00342 }
00343
00360 public function listSDRTextValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00361 return parent::getListBase(self::getDBI(),
00362 array_merge($constraints, array('valueId' => $this->id)),
00363 $limit, $order, $count, 'SDRTextValue', true);
00364 }
00365
00384 public function listSDRTextValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00385 return parent::getListBaseMulti(self::getDBI(),
00386 array_merge($constraints, array('Base.valueId' => $this->id)),
00387 $limit, $order, $count, 'SDRTextValue', true, $depth);
00388 }
00389
00404 public function addSDRTextValue($entry) {
00405 $obj = new SDRTextValue();
00406
00407 $obj->valueId = $this->id;
00408 $obj->entry = $entry;
00409
00410 if(!$obj->commit())
00411 return null;
00412
00413 return $obj;
00414 }
00415
00432 public function listSDRIntValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00433 return parent::getListBase(self::getDBI(),
00434 array_merge($constraints, array('valueId' => $this->id)),
00435 $limit, $order, $count, 'SDRIntValue', true);
00436 }
00437
00456 public function listSDRIntValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00457 return parent::getListBaseMulti(self::getDBI(),
00458 array_merge($constraints, array('Base.valueId' => $this->id)),
00459 $limit, $order, $count, 'SDRIntValue', true, $depth);
00460 }
00461
00476 public function addSDRIntValue($entry) {
00477 $obj = new SDRIntValue();
00478
00479 $obj->valueId = $this->id;
00480 $obj->entry = $entry;
00481
00482 if(!$obj->commit())
00483 return null;
00484
00485 return $obj;
00486 }
00487
00488
00489
00496 private function valueTableForType($type) {
00497 switch($type) {
00498 case SDRField::TYPE_INT:
00499 return 'SDRIntValue';
00500 case SDRField::TYPE_TEXT:
00501 return 'SDRTextValue';
00502 case SDRField::TYPE_DATE:
00503 return 'SDRDateValue';
00504 default:
00505 self::setStaticError('Unknown type for SDRValue: ' . $type);
00506 return false;
00507 }
00508 }
00509
00510
00521 public static function findOrAddValue($entry, $field) {
00522 $commitSuccess = true;
00523 if ($field->type == SDRField::TYPE_INT) {
00524 $valueEntry = SDRTextValue::retrieve(array('LIKE BINARY'=>array('entry' => $entry)));
00525 if ($valueEntry == null) {
00526 $value = new SDRValue();
00527 $value->type = $field->type;
00528 $value->commit();
00529 $value->addSDRIntValue($entry);
00530 $commitSuccess = $value->commit();
00531 }
00532 else
00533 $value = $valueEntry->getParentValue();
00534 }
00535 else if ($field->type == SDRField::TYPE_TEXT) {
00536
00537 $valueEntry = SDRTextValue::retrieve(array('LIKE BINARY'=>array('entry' => $entry)));
00538 if ($valueEntry == null) {
00539 $value = new SDRValue();
00540 $value->type = $field->type;
00541 $value->commit();
00542 $value->addSDRTextValue($entry);
00543 $commitSuccess = $value->commit();
00544 }
00545 else
00546 $value = $valueEntry->getParentValue();
00547 }
00548 else if ($field->type == SDRField::TYPE_DATE) {
00549 $valueEntry = SDRTextValue::retrieve(array('LIKE BINARY'=>array('entry' => $entry)));
00550 if ($valueEntry == null) {
00551 $value = new SDRValue();
00552 $value->type = $field->type;
00553 $value->commit();
00554 $value->addSDRDateValue($entry);
00555 $commitSuccess = $value->commit();
00556 }
00557 else
00558 $value = $valueEntry->getParentValue();
00559 }
00560
00561 if ( !$commitSuccess ) {
00562 self::setStaticError("Could not commit new $type value for $entry");
00563 return false;
00564 }
00565
00566
00567 $fieldValue = SDRFieldValue::retrieve(array('fieldId'=>$field->id, 'valueId'=>$value->id));
00568 if ($fieldValue == null) {
00569 $fieldValue = new SDRFieldValue();
00570 $fieldValue->fieldId = $field->id;
00571 $fieldValue->valueId = $value->id;
00572 if (!$fieldValue->commit()) {
00573 self::setStaticError("Could not connect field $field->name to value $entry");
00574 return false;
00575 }
00576 }
00577 return($value);
00578 }
00579
00580 public function getValue() {
00581 $cacheList = SDRValueCache::getList(array('valueId'=>$this->id));
00582 return $cacheList[0]->value;
00583 }
00584
00585
00586 }
00587
00588 ?>