00001 <?php
00008 class SDRServiceRequest extends SObject {
00009
00010 const DEFAULT_NUM_PER_PAGE = 10;
00011
00012 const PRESET_FIND_QUICK = 1;
00013 const PRESET_FIND_ADVANCED = 2;
00014 const PRESET_FIND_LIST = 3;
00015 const PRESET_METADATA_BY_CSERD_ID = 20;
00016 const PRESET_ALL_DATA_BY_CSERD_ID = 21;
00017 const PRESET_METADATA_AND_REVIEWS_BY_CSERD_ID = 22;
00018 const PRESET_METADATA_BY_FIELDS = 25;
00019 const PRESET_TSD_BY_FIELDS = 26;
00020 const PRESET_VOCAB = 30;
00021
00022 protected $method = null;
00023 protected $type = null;
00024 protected $filterProject = array();
00025 protected $filterProjectType = '';
00026 protected $startingResult = null;
00027 protected $resultsPerPage = self::DEFAULT_NUM_PER_PAGE;
00028 protected $XMLData = null;
00029 protected $key = null;
00030 protected $constraints = array();
00031 protected $dataSections = array();
00032 protected $state = 'live';
00033
00044 public function __construct($method = SDRContract::METHOD_FIND,
00045 $type = SDRContract::REQUEST_TYPE_DEFAULT) {
00046 parent::__construct();
00047 $this->method = $method;
00048 $this->type = $type;
00049 $this->searchBase = SPath::getFullPath('SDRService') . '/';
00050
00051
00052 if (SConfig::getDefault('common.server') == 'newdev') {
00053 $this->state = 'dev';
00054 } else if (SConfig::getDefault('common.server') == 'www') {
00055 $this->state = 'live';
00056 }
00057
00058
00059
00060 }
00061
00071 public function buildQueryURL() {
00072 $sr = $this->startingResult;
00073 $numPerPage = $this->resultsPerPage;
00074 $state = $this->state;
00075 $filter = $this->constraints;
00076 if ($sr < 0) $sr = 0;
00077
00078 $search = $this->searchBase;
00079 $search .= "$this->method/?";
00080
00081 switch ($this->method) {
00082
00083 case SDRContract::METHOD_FIND:
00084 $search .= "type=$this->type";
00085
00086 if ($this->type != SDRContract::REQUEST_TYPE_VOCAB) {
00087 if (count($this->filterProject))
00088 $search .= "&filterProject=" . implode(',', array_keys($this->filterProject));
00089 if ($this->filterProjectType != '')
00090 $search .= '&filterProjectType=' . $this->filterProjectType;
00091 }
00092
00093 if ($sr != 0) $search .= "&sr=$sr";
00094 if ($state != '') $search .= "&state=$state";
00095 if ($numPerPage != self::DEFAULT_NUM_PER_PAGE) $search .= "&rp=$numPerPage";
00096 if (array_key_exists('orderBy', $this->constraints)) $search .= "&orderBy=".$this->constraints['orderBy'];
00097
00098 switch ($this->type) {
00099
00100 case SDRContract::REQUEST_TYPE_LIST:
00101 return ($search . $this->getDataSectionsForURL());
00102
00103
00104 case SDRContract::REQUEST_TYPE_ADVANCED:
00105 if (is_array($filter)) {
00106 foreach($filter as $key=>$value) {
00107 if ($value != '')
00108 $search .= '&' . $key .'=' . $value;
00109 else $search .= '&' . $key;
00110 }
00111 }
00112 $ds = ($this->getDataSectionsForURL());
00113 return ($search . $ds);
00114
00115
00116 case SDRContract::REQUEST_TYPE_QUICK:
00117 return ($search . "&ss=$filter[ss]" . $this->getDataSectionsForURL());
00118
00119
00120 case SDRContract::REQUEST_TYPE_VOCAB:
00121 return ($search . "&field=" . $filter['field']);
00122 }
00123
00124 case SDRContract::METHOD_SUBMIT:
00125 return $search;
00126 case SDRContract::METHOD_APPROVE:
00127 return $search;
00128
00129 default:
00130 $this->setError('Unknown method: "' . $this->method . '"');
00131 return false;
00132 }
00133 }
00134
00145 public function addConstraints($constraints) {
00146 foreach($constraints as $key => $value) {
00147 if (array_key_exists($key, $this->constraints))
00148 $this->setWarning('Overwriting existing constraint for key ' . $key);
00149 $this->constraints[$key] = $value;
00150 }
00151 }
00152
00159 public function getConstraints() {
00160 return $this->constraints;
00161 }
00162
00171 public function getConstraint($key) {
00172 if (!isset($this->constraints[$key])) return '';
00173 return $this->constraints[$key];
00174 }
00175
00186 public function usePreset($preset, $params = array()) {
00187 switch($preset) {
00188 case self::PRESET_FIND_QUICK:
00189 if(!isset($params['ss'])) {
00190 $this->setError('Requires a search string ss');
00191 return false;
00192 }
00193 $this->setMethod(SDRContract::METHOD_FIND);
00194 $this->setType(SDRContract::REQUEST_TYPE_QUICK);
00195 $this->addConstraints(array('ss' => $params['ss']));
00196 if (isset($params['sr']))
00197 $this->setStartingResult($params['sr']);
00198 return true;
00199 case self::PRESET_FIND_ADVANCED:
00200 $this->setMethod(SDRContract::METHOD_FIND);
00201 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00202 if (isset($params['ss']))
00203 $this->addConstraints(array('ss'=>$params['ss']));
00204 if (isset($params['sr']))
00205 $this->setStartingResult($params['sr']);
00206 return true;
00207 case self::PRESET_FIND_LIST:
00208 $this->setMethod(SDRContract::METHOD_FIND);
00209 $this->setType(SDRContract::REQUEST_TYPE_LIST);
00210 return true;
00211 case self::PRESET_ALL_DATA_BY_CSERD_ID:
00212 if(!isset($params['cserdId'])) {
00213 $this->setError('Requires a cserdId');
00214 return false;
00215 }
00216 $this->setMethod(SDRContract::METHOD_FIND);
00217 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00218 $this->addConstraints(array('cserdId' => $params['cserdId']));
00219 $this->addDataSection(array(SDRContract::DATA_METADATA,
00220 SDRContract::DATA_REVIEWS,
00221 SDRContract::DATA_ALIGNMENTS,
00222 SDRContract::DATA_RELATIONSHIPS));
00223 return true;
00224 case self::PRESET_METADATA_BY_CSERD_ID:
00225 if(!isset($params['cserdId'])) {
00226 $this->setError('Requires a cserdId');
00227 return false;
00228 }
00229 $this->setMethod(SDRContract::METHOD_FIND);
00230 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00231 $this->addConstraints(array('cserdId' => $params['cserdId']));
00232 $this->addDataSection(array(SDRContract::DATA_METADATA));
00233 return true;
00234 case self::PRESET_METADATA_BY_FIELDS:
00235 $this->setMethod(SDRContract::METHOD_FIND);
00236 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00237 $this->addConstraints($params);
00238 $this->addDataSection(array(SDRContract::DATA_METADATA));
00239 return true;
00240 case self::PRESET_TSD_BY_FIELDS:
00241 $this->setMethod(SDRContract::METHOD_FIND);
00242 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00243 $this->addConstraints($params);
00244 $this->addDataSection(array(SDRContract::DATA_METADATA,
00245 SDRContract::DATA_ALIGNMENTS,
00246 SDRContract::DATA_RELATIONSHIPS));
00247 return true;
00248 case self::PRESET_METADATA_AND_REVIEWS_BY_CSERD_ID:
00249 if(!isset($params['cserdId'])) {
00250 $this->setError('Requires a cserdId');
00251 return false;
00252 }
00253 $this->setMethod(SDRContract::METHOD_FIND);
00254 $this->setType(SDRContract::REQUEST_TYPE_ADVANCED);
00255 $this->addConstraints(array('cserdId' => $params['cserdId']));
00256 $this->addDataSection(array(SDRContract::DATA_METADATA,
00257 SDRContract::DATA_REVIEWS));
00258 return true;
00259 case self::PRESET_VOCAB:
00260 if(!isset($params['field'])) {
00261 $this->setError('Requires a vocab field');
00262 return false;
00263 }
00264 $this->setMethod(SDRContract::METHOD_FIND);
00265 $this->setType(SDRContract::REQUEST_TYPE_VOCAB);
00266 $this->addConstraints(array('field' => $params['field']));
00267 return true;
00268 default:
00269 $this->setError('Unknown preset ' . $preset);
00270 return false;
00271 }
00272 }
00273
00281 public function isAdvanced() {
00282 return $this->type == SDRContract::REQUEST_TYPE_ADVANCED;
00283 }
00284
00293 public function setMethod($method) {
00294 $this->method = $method;
00295 }
00296
00304 public function getMethod() {
00305 return $this->method;
00306 }
00307
00316 public function setState($state) {
00317 $this->state = $state;
00318 }
00319
00327 public function getState() {
00328 return $this->state;
00329 }
00330
00339 public function setType($type) {
00340 $this->type = $type;
00341 }
00342
00350 public function getType() {
00351 return $this->type;
00352 }
00353
00363 public function addProjectFilter($proj) {
00364 $this->filterProject[$proj] = true;
00365 }
00366
00374 public function removeProjectFilter($proj) {
00375 if (isset($this->filterProject[$proj]))
00376 unset($this->filterProject[$proj]);
00377 }
00378
00386 public function setProjectFilter($proj) {
00387 $this->filterProject = array($proj => true);
00388 }
00389
00395 public function clearProjectFilters() {
00396 $this->filterProject = array();
00397 }
00398
00404 public function addCSERDFilter() {
00405 $this->addProjectFilter(SDRContract::PROJECT_CSERD);
00406 }
00407
00413 public function clearCSERDFilter() {
00414 $this->removeProjectFilter(SDRContract::PROJECT_CSERD);
00415 }
00416
00427 public function setFilterProjectType($fpt) {
00428 $this->filterProjectType = $fpt;
00429 }
00430
00436 public function getFilterProjectType() {
00437 return $this->filterProjectType;
00438 }
00439
00440
00449 public function setStartingResult($sr) {
00450 $this->startingResult = $sr;
00451 }
00452
00461 public function setNumResultsPerPage($numPerPage) {
00462 $this->resultsPerPage = $numPerPage;
00463 }
00464
00472 public function getNumResultsPerPage() {
00473 return $this->resultsPerPage;
00474 }
00475
00484 public function setKey($key) {
00485 $this->key = $key;
00486 }
00487
00495 public function getKey() {
00496 return $this->key;
00497 }
00498
00507 public function setXMLData($data) {
00508 $this->XMLData = $data;
00509 }
00510
00518 public function getXMLData() {
00519 return $this->XMLData;
00520 }
00521
00530 public function addDataSection($section) {
00531 if (is_array($section)) {
00532 foreach($section as $s) {
00533 $this->dataSections []= $s;
00534 }
00535 } else {
00536 $this->dataSections []= $section;
00537 }
00538 }
00539
00547 public function getDataSectionsForURL() {
00548 return '&' . implode('&', $this->dataSections);
00549 }
00550
00558 public function toString() {
00559 switch ($this->method) {
00560 case SDRContract::METHOD_FIND:
00561 switch($this->type) {
00562 case SDRContract::REQUEST_TYPE_QUICK:
00563 if ($this->constraints['ss'] == '') return 'No Search Terms';
00564 return '<b>Quick Search</b> for <b>' . $this->constraintsToString() . '</b>';
00565 case SDRContract::REQUEST_TYPE_ADVANCED:
00566 return 'Advanced Search:'.$this->constraintsToString();
00567 case SDRContract::REQUEST_TYPE_LIST:
00568 return 'Catalog Listing';
00569 default:
00570 return '';
00571 }
00572 default:
00573 return '';
00574 }
00575 }
00576
00577
00585 public function constraintsToString() {
00586 switch ($this->method) {
00587 case SDRContract::METHOD_FIND:
00588 switch($this->type) {
00589 case SDRContract::REQUEST_TYPE_QUICK:
00590 if ($this->constraints['ss'] == '') return 'No Search Terms';
00591 return 'Searched for “' . htmlspecialchars(strip_tags($this->constraints['ss'])) . '”</b>';
00592 case SDRContract::REQUEST_TYPE_ADVANCED:
00593 $constraintString = '';
00594 if (array_key_exists('ss', $this->constraints) && $this->constraints['ss'] != '') {
00595 $constraintString .= ", Search String = “" . $this->constraints['ss']. "”";
00596 }
00597 foreach ($this->constraints as $field=>$value) {
00598 if ($value != '' && $field != 'orderBy' && $field != 'ss')
00599 $constraintString .= ", $field = $value";
00600 }
00601 return substr($constraintString, 1);
00602 case SDRContract::REQUEST_TYPE_LIST:
00603 return '';
00604 default:
00605 return '';
00606 }
00607 default:
00608 return '';
00609 }
00610 }
00611
00612
00613
00621 public function typeToString() {
00622 switch ($this->method) {
00623 case SDRContract::METHOD_FIND:
00624 switch($this->type) {
00625 case SDRContract::REQUEST_TYPE_QUICK:
00626 return 'Quick Search';
00627 case SDRContract::REQUEST_TYPE_ADVANCED:
00628 return 'Advanced Search';
00629 case SDRContract::REQUEST_TYPE_LIST:
00630 return 'Catalog Listing';
00631 default:
00632 return '';
00633 }
00634 default:
00635 return '';
00636 }
00637 }
00638 }
00639
00640 ?>