00001 <?php
00002
00012 class SDRField extends ModelBase {
00013
00014 const TYPE_INT = 'int';
00015 const TYPE_TEXT = 'text';
00016 const TYPE_DATE = 'date';
00017
00019 public static $ATTRIBUTES = array(
00020 'id' => array(
00021 'nullable' => false,
00022 'commit' => SModel2::COMMIT_NEVER,
00023 'type' => 'int'
00024 ),
00025 'name' => array(
00026 'nullable' => false,
00027 'type' => 'string'
00028 ),
00029 'description' => array(
00030 'nullable' => false,
00031 'type' => 'string'
00032 ),
00033 'control' => array(
00034 'type' => 'string'
00035 ),
00036 'type' => array(
00037 'type' => 'string',
00038 'default' => 'text'
00039 ),
00040 'editable' => array(
00041 'type' => 'int',
00042 'default' => 1
00043 )
00044 );
00045
00054 public function __construct($constraints = null) {
00055 parent::__construct();
00056
00057 $this->registerAttributes(self::$ATTRIBUTES);
00058 $this->registerTableName('SDRField');
00059 $this->registerPrimaryKey('id', true);
00060
00061 if($constraints !== null)
00062 $this->populate($constraints);
00063 }
00064
00076 public static function retrieve($constraints = array(), $checkOnly = false) {
00077 $list = self::getList($constraints, array(1), array(), $checkOnly);
00078 if(!$list || count($list) == 0)
00079 return $checkOnly ? false : null;
00080 else if($checkOnly)
00081 return $list > 0;
00082 else
00083 return $list[0];
00084 }
00085
00094 public static function exists($constraints = array()) {
00095 return self::retrieve($constraints, true);
00096 }
00097
00111 public static function getList($constraints = array(), $limit = array(), $order = array(), $count = false) {
00112 return parent::getListBase(self::getDBI(), $constraints, $limit, $order, $count, 'SDRField', true);
00113 }
00114
00124 public static function getAttributesStatic() {
00125 return self::$ATTRIBUTES;
00126 }
00127
00133 public static function getTableNameStatic() {
00134 return 'SDRField';
00135 }
00136
00137
00154 public function listSDRVersionFieldValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00155 return parent::getListBase(self::getDBI(),
00156 array_merge($constraints, array('fieldId' => $this->id)),
00157 $limit, $order, $count, 'SDRVersionFieldValue', true);
00158 }
00159
00178 public function listSDRVersionFieldValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00179 return parent::getListBaseMulti(self::getDBI(),
00180 array_merge($constraints, array('Base.fieldId' => $this->id)),
00181 $limit, $order, $count, 'SDRVersionFieldValue', true, $depth);
00182 }
00183
00199 public function addSDRVersionFieldValue($version, $value) {
00200 $obj = new SDRVersionFieldValue();
00201
00202 if(is_object($version))
00203 $obj->version = $version;
00204 else
00205 $obj->versionId = $version;
00206 $obj->fieldId = $this->id;
00207 if(is_object($value))
00208 $obj->value = $value;
00209 else
00210 $obj->valueId = $value;
00211
00212 if(!$obj->commit())
00213 return null;
00214
00215 return $obj;
00216 }
00217
00234 public function listSDRFieldValues($constraints = array(), $limit = array(), $order = array(), $count = false) {
00235 return parent::getListBase(self::getDBI(),
00236 array_merge($constraints, array('fieldId' => $this->id)),
00237 $limit, $order, $count, 'SDRFieldValue', true);
00238 }
00239
00258 public function listSDRFieldValuesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00259 return parent::getListBaseMulti(self::getDBI(),
00260 array_merge($constraints, array('Base.fieldId' => $this->id)),
00261 $limit, $order, $count, 'SDRFieldValue', true, $depth);
00262 }
00263
00278 public function addSDRFieldValue($value) {
00279 $obj = new SDRFieldValue();
00280
00281 $obj->fieldId = $this->id;
00282 if(is_object($value))
00283 $obj->value = $value;
00284 else
00285 $obj->valueId = $value;
00286
00287 if(!$obj->commit())
00288 return null;
00289
00290 return $obj;
00291 }
00292
00309 public function listSDRSearchWordCounts($constraints = array(), $limit = array(), $order = array(), $count = false) {
00310 return parent::getListBase(self::getDBI(),
00311 array_merge($constraints, array('fieldId' => $this->id)),
00312 $limit, $order, $count, 'SDRSearchWordCount', true);
00313 }
00314
00333 public function listSDRSearchWordCountsMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00334 return parent::getListBaseMulti(self::getDBI(),
00335 array_merge($constraints, array('Base.fieldId' => $this->id)),
00336 $limit, $order, $count, 'SDRSearchWordCount', true, $depth);
00337 }
00338
00355 public function addSDRSearchWordCount($searchWord, $cserd, $count) {
00356 $obj = new SDRSearchWordCount();
00357
00358 if(is_object($searchWord))
00359 $obj->searchWord = $searchWord;
00360 else
00361 $obj->searchWordId = $searchWord;
00362 if(is_object($cserd))
00363 $obj->cserd = $cserd;
00364 else
00365 $obj->cserdId = $cserd;
00366 $obj->fieldId = $this->id;
00367 $obj->count = $count;
00368
00369 if(!$obj->commit())
00370 return null;
00371
00372 return $obj;
00373 }
00374
00391 public function listSDRProjectFields($constraints = array(), $limit = array(), $order = array(), $count = false) {
00392 return parent::getListBase(self::getDBI(),
00393 array_merge($constraints, array('fieldId' => $this->id)),
00394 $limit, $order, $count, 'SDRProjectField', true);
00395 }
00396
00415 public function listSDRProjectFieldsMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00416 return parent::getListBaseMulti(self::getDBI(),
00417 array_merge($constraints, array('Base.fieldId' => $this->id)),
00418 $limit, $order, $count, 'SDRProjectField', true, $depth);
00419 }
00420
00437 public function addSDRProjectField($project, $indexOrder, $required = '') {
00438 $obj = new SDRProjectField();
00439
00440 if(is_object($project))
00441 $obj->project = $project;
00442 else
00443 $obj->projectId = $project;
00444 $obj->fieldId = $this->id;
00445 $obj->indexOrder = $indexOrder;
00446 if($required != '')
00447 $obj->required = $required;
00448
00449 if(!$obj->commit())
00450 return null;
00451
00452 return $obj;
00453 }
00454
00461 public static function findOrAddField($name) {
00462
00463 $field = self::retrieve( array('name'=>$name) );
00464
00465
00466 if ($field == null) {
00467 $field = new SDRField();
00468 $field->name = $name;
00469 if ( !$field->commit() ) {
00470 self::setStaticError('Could not commit new field for ' . $name);
00471 return false;
00472 }
00473 }
00474
00475 return $field;
00476 }
00477
00482 public static function getNamesAndIds() {
00483 $DBI = self::getDBI();
00484 $result = $DBI->select(self::getTableNameStatic(), array('id', 'name'));
00485 if (!$result)
00486 return false;
00487 $return = array();
00488 foreach ($result as $r) {
00489 $return[$r['name']] = $r['id'];
00490 }
00491 return $return;
00492 }
00493 }
00494
00495 ?>