00001 <?php
00002
00012 class SDRVersion extends ModelBase {
00013
00014 const STATE_LIVE = 'live';
00015 const STATE_APPROVED = 'live';
00016 const STATE_DEV = 'dev';
00017 const STATE_PENDING = 'pending';
00018 const STATE_HISTORY = 'history';
00019 const STATE_REJECTED = 'rejected';
00020
00021
00023 public static $ATTRIBUTES = array(
00024 'id' => array(
00025 'nullable' => false,
00026 'commit' => SModel2::COMMIT_NEVER,
00027 'type' => 'int'
00028 ),
00029 'parent' => array(
00030 'type' => 'int',
00031 'default' => null
00032 ),
00033 'cserdId' => array(
00034 'nullable' => false,
00035 'commit' => SModel2::COMMIT_FIRST,
00036 'type' => 'int',
00037 ),
00038 'state' => array(
00039 'type' => 'string',
00040 'default' => 'pending'
00041 ),
00042 'comment' => array(
00043 'type' => 'string'
00044 ),
00045 'created' => array(
00046 'nullable' => false,
00047 'commit' => SModel2::COMMIT_CHANGED,
00048 'type' => 'time',
00049 'default' => ''
00050 ),
00051 'createdAgentId' => array(
00052 'nullable' => false,
00053 'commit' => SModel2::COMMIT_FIRST,
00054 'type' => 'int',
00055 'foreign' => array('table' => 'SDRAgent', 'on' => 'id', 'object' => 'createdAgent')
00056 ),
00057 'modified' => array(
00058 'nullable' => false,
00059 'type' => 'time',
00060 'default' => '0000-00-00 00:00:00'
00061 ),
00062 'modifiedAgentId' => array(
00063 'nullable' => false,
00064 'commit' => SModel2::COMMIT_FIRST,
00065 'type' => 'int',
00066 'foreign' => array('table' => 'SDRAgent', 'on' => 'id', 'object' => 'modifiedAgent')
00067 ),
00068 'createdAgent' => array(
00069 'commit' => SModel2::COMMIT_FOREIGN,
00070 'type' => 'SDRAgent'
00071 ),
00072 'modifiedAgent' => array(
00073 'commit' => SModel2::COMMIT_FOREIGN,
00074 'type' => 'SDRAgent'
00075 )
00076 );
00077
00087 public function __construct($constraints = null, $multi = false) {
00088 parent::__construct();
00089
00090 $this->registerAttributes(self::$ATTRIBUTES);
00091 $this->registerTableName('SDRVersion');
00092 $this->registerPrimaryKey('id', true);
00093
00094 if($constraints !== null)
00095 $this->populate($constraints, $multi);
00096 }
00097
00107 public static function getAttributesStatic() {
00108 return self::$ATTRIBUTES;
00109 }
00110
00116 public static function getTableNameStatic() {
00117 return 'SDRVersion';
00118 }
00119
00131 public static function retrieve($constraints = array(), $checkOnly = false) {
00132 $list = self::getList($constraints, array(1), array(), $checkOnly);
00133 if(!$list || count($list) == 0)
00134 return $checkOnly ? false : null;
00135 else if($checkOnly)
00136 return $list > 0;
00137 else
00138 return $list[0];
00139 }
00140
00149 public static function exists($constraints = array()) {
00150 return self::retrieve($constraints, true);
00151 }
00152
00165 public static function retrieveMulti($constraints = array(), $checkOnly = false, $depth = 1) {
00166 $list = self::getListMulti($constraints, array(1), array(), $checkOnly, $depth);
00167 if(!$list || count($list) == 0)
00168 return $checkOnly ? false : null;
00169 else if($checkOnly)
00170 return $list > 0;
00171 else
00172 return $list[0];
00173 }
00174
00183 public static function existsMulti($constraints = array()) {
00184 return self::retrieveMulti($constraints, true);
00185 }
00186
00200 public static function getList($constraints = array(), $limit = array(), $order = array(), $count = false) {
00201 return parent::getListBase(self::getDBI(), $constraints, $limit, $order, $count, 'SDRVersion', true);
00202 }
00203
00219 public static function getListMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00220 return parent::getListBaseMulti(self::getDBI(), $constraints, $limit, $order, $count,
00221 'SDRVersion', true, $depth);
00222 }
00223
00224
00233 public function getParentCserd() {
00234 return SDRResource::retrieve(array('cserdId' => $this->cserdId));
00235 }
00236
00246 public function getParentCserdMulti($depth = 1) {
00247 return SDRResource::retrieveMulti(array('Base.cserdId' => $this->cserdId), false, $depth);
00248 }
00249
00258 public function getParentCreatedAgent() {
00259 return SDRAgent::retrieve(array('id' => $this->createdAgentId));
00260 }
00261
00271 public function getParentCreatedAgentMulti($depth = 1) {
00272 return SDRAgent::retrieveMulti(array('Base.id' => $this->createdAgentId), false, $depth);
00273 }
00274
00283 public function getParentModifiedAgent() {
00284 return SDRAgent::retrieve(array('id' => $this->modifiedAgentId));
00285 }
00286
00296 public function getParentModifiedAgentMulti($depth = 1) {
00297 return SDRAgent::retrieveMulti(array('Base.id' => $this->modifiedAgentId), false, $depth);
00298 }
00299
00316 public function listSDRVersionFieldValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00317 return parent::getListBase(self::getDBI(),
00318 array_merge($constraints, array('versionId' => $this->id)),
00319 $limit, $order, $count, 'SDRVersionFieldValue', true);
00320 }
00321
00340 public function listSDRVersionFieldValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00341 return parent::getListBaseMulti(self::getDBI(),
00342 array_merge($constraints, array('Base.versionId' => $this->id)),
00343 $limit, $order, $count, 'SDRVersionFieldValue', true, $depth);
00344 }
00345
00361 public function addSDRVersionFieldValue($field, $value) {
00362 $obj = new SDRVersionFieldValue();
00363
00364 $obj->versionId = $this->id;
00365 if(is_object($field))
00366 $obj->field = $field;
00367 else
00368 $obj->fieldId = $field;
00369 if(is_object($value))
00370 $obj->value = $value;
00371 else
00372 $obj->valueId = $value;
00373
00374 if(!$obj->commit())
00375 return null;
00376
00377 return $obj;
00378 }
00379
00396 public function listSDRVersionCaches($constraints = array(), $limit = array(), $order = array(), $count = false) {
00397 return parent::getListBase(self::getDBI(),
00398 array_merge($constraints, array('versionId' => $this->id)),
00399 $limit, $order, $count, 'SDRVersionCache', true);
00400 }
00401
00420 public function listSDRVersionCachesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00421 return parent::getListBaseMulti(self::getDBI(),
00422 array_merge($constraints, array('Base.versionId' => $this->id)),
00423 $limit, $order, $count, 'SDRVersionCache', true, $depth);
00424 }
00425
00445 public function addSDRVersionCache($cserd, $Title = '', $Description = '', $MetadataXMLCache = '', $created = '0000-00-00 00:00:00', $modified = '0000-00-00 00:00:00') {
00446 $obj = new SDRVersionCache();
00447
00448 if(is_object($cserd))
00449 $obj->cserd = $cserd;
00450 else
00451 $obj->cserdId = $cserd;
00452 $obj->versionId = $this->id;
00453 if($Title != '')
00454 $obj->Title = $Title;
00455 if($Description != '')
00456 $obj->Description = $Description;
00457 if($MetadataXMLCache != '')
00458 $obj->MetadataXMLCache = $MetadataXMLCache;
00459 $obj->created = $created;
00460 $obj->modified = $modified;
00461
00462 if(!$obj->commit())
00463 return null;
00464
00465 return $obj;
00466 }
00467
00477 public static function createVersion($cserdId, $createdAgentId, $modifiedAgentId, $comment) {
00478 $ver = new SDRVersion();
00479 $ver->cserdId = $cserdId;
00480 $ver->createdAgentId = $createdAgentId;
00481 $ver->modifiedAgentId = $modifiedAgentId;
00482 $ver->comment = $comment;
00483 $ver->created = date("Y-m-d H:i:s");
00484 if(!$ver->commit()) {
00485 self::setStaticError('Error committing a new version.');
00486 return false;
00487 }
00488 return $ver;
00489 }
00490
00496 public function getMetadata() {
00497 $versionFieldValueList = SDRVersionFieldValue::getList(array('versionId' => $this->get('id')));
00498 $fields = array();
00499 $values = array();
00500 $metadata = new SDRMetadataObj();
00501 foreach($versionFieldValueList as $versionFieldValue) {
00502
00503 if (!isset($fields[$versionFieldValue->fieldId])) {
00504 $field = new SDRField(array('id' => $versionFieldValue->fieldId));
00505 $fields[$versionFieldValue->fieldId] = $field;
00506 }
00507
00508 if (!isset($values[$versionFieldValue->valueId])){
00509 $value = new SDRValue(array('id' => $versionFieldValue->valueId));
00510 $values[$versionFieldValue->valueId] = $value;
00511 }
00512 $metadata->addTag($fields[$versionFieldValue->fieldId]->name, $values[$versionFieldValue->valueId]->getValue());
00513 }
00514 return $metadata;
00515 }
00516 }
00517
00518 ?>