00001 <?php
00002
00012 class SDRAgent extends ModelBase {
00013
00014 const REALM_CSERD = 'cserd';
00015 const REALM_LDAP = 'ldap';
00016 const REALM_API = 'api';
00017
00019 public static $ATTRIBUTES = array(
00020 'id' => array(
00021 'nullable' => false,
00022 'commit' => SModel2::COMMIT_NEVER,
00023 'type' => 'int'
00024 ),
00025 'userName' => array(
00026 'nullable' => false,
00027 'type' => 'string'
00028 ),
00029 'firstName' => array(
00030 'type' => 'string',
00031 'default' => null
00032 ),
00033 'lastName' => array(
00034 'type' => 'string',
00035 'default' => null
00036 ),
00037 'realm' => array(
00038 'type' => 'string'
00039 ),
00040 'cserdUserId' => array(
00041 'type' => 'int',
00042 'default' => null
00043 )
00044 );
00045
00054 public function __construct($constraints = null) {
00055 parent::__construct();
00056
00057 $this->registerAttributes(self::$ATTRIBUTES);
00058 $this->registerTableName('SDRAgent');
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, 'SDRAgent', true);
00113 }
00114
00124 public static function getAttributesStatic() {
00125 return self::$ATTRIBUTES;
00126 }
00127
00133 public static function getTableNameStatic() {
00134 return 'SDRAgent';
00135 }
00136
00137
00154 public function listSDRVersions($constraints = array(), $limit = array(), $order = array(), $count = false) {
00155 return parent::getListBase(self::getDBI(),
00156 array_merge($constraints, array('modifiedAgentId' => $this->id)),
00157 $limit, $order, $count, 'SDRVersion', true);
00158 }
00159
00178 public function listSDRVersionsMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00179 return parent::getListBaseMulti(self::getDBI(),
00180 array_merge($constraints, array('Base.modifiedAgentId' => $this->id)),
00181 $limit, $order, $count, 'SDRVersion', true, $depth);
00182 }
00183
00203 public function addSDRVersion($parent = null, $cserd, $state = '', $comment = '', $created = '', $modified = '0000-00-00 00:00:00') {
00204 $obj = new SDRVersion();
00205
00206 if($parent != '')
00207 $obj->parent = $parent;
00208 if(is_object($cserd))
00209 $obj->cserd = $cserd;
00210 else
00211 $obj->cserdId = $cserd;
00212 if($state != '')
00213 $obj->state = $state;
00214 if($comment != '')
00215 $obj->comment = $comment;
00216 $obj->created = $created;
00217 $obj->createdAgentId = $this->id;
00218 $obj->modified = $modified;
00219 $obj->modifiedAgentId = $this->id;
00220
00221 if(!$obj->commit())
00222 return null;
00223
00224 return $obj;
00225 }
00226
00240 public static function agentFromSUser($user, $realm) {
00241
00242 $list = self::getList( array('userName' => $user->getUserName(),
00243 'realm' => $realm));
00244 if ($list === false)
00245 return false;
00246 if (count($list) > 0) {
00247 return $list[0];
00248 }
00249
00250
00251 $agent = new SDRAgent;
00252 $agent->userName = $user->getUserName();
00253 $agent->firstName = $user->getFirstName();
00254 $agent->lastName = $user->getLastName();
00255 $agent->realm = $realm;
00256 if (!$agent->commit())
00257 return false;
00258 return $agent;
00259 }
00260
00274 public static function agentFromS2User($user, $realm) {
00275
00276 $list = self::getList( array('userName' => $user->userName,
00277 'realm' => $realm));
00278 if ($list === false)
00279 return false;
00280 if (count($list) > 0) {
00281 return $list[0];
00282 }
00283
00284
00285 $agent = new SDRAgent;
00286 $agent->userName = $user->userName;
00287 $agent->firstName = $user->firstName;
00288 $agent->lastName = $user->lastName;
00289 $agent->realm = $realm;
00290 if (!$agent->commit())
00291 return false;
00292 return $agent;
00293 }
00294
00304 public static function agentFromCSERDUser($user) {
00305 $isSModel2 = $user instanceof SModel2;
00306 if ($isSModel2)
00307 $agent = self::agentFromS2User($user, self::REALM_CSERD);
00308 else
00309 $agent = self::agentFromSUser($user, self::REALM_CSERD);
00310
00311 if ($agent === false)
00312 return false;
00313 if ($isSModel2)
00314 $agent->cserdUserId = $user->userId;
00315 else
00316 $agent->cserdUserId = $user->getId();
00317
00318 if (!$agent->commit())
00319 return false;
00320 return $agent;
00321 }
00322
00331 public function getDisplayName() {
00332 if ($this->get('firstName') != '') {
00333 return $this->get('firstName') . ' ' . $this->get('lastName') . ' (' . $this->realm
00334 . ')';
00335 } else {
00336 return $this->get('userName') . ' (' . $this->realm . ')';
00337 }
00338 }
00339
00340 }
00341
00342 ?>