Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
data_source.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/interfaces/bridge/bridge.inc';
19 
31 class Data_Source extends Asset implements Bridge
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _createAdditional(Array &$link)
60  {
61  if (!parent::_createAdditional($link)) return FALSE;
62 
63  // set an initial web path
64  $initial_path = strtolower($this->attr('name'));
65  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
66  $valid_paths = make_valid_web_paths(Array($initial_path));
67  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($link['asset'], $valid_paths, $this->id, TRUE);
68  return $this->saveWebPaths($good_paths);
69 
70  }//end _createAdditional()
71 
72 
83  protected function _getName($short_name=FALSE, $contextid=NULL)
84  {
85  // No context specified, using the current context
86  if ($contextid === NULL) {
87  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
88  }//end if
89 
90  // Obtain the attribute value for Name from the specified Context
91  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
92  if (empty($values) === TRUE) {
93  return parent::_getName($short_name, $contextid);
94  } else {
95  return $values[$this->id];
96  }
97 
98  }//end _getName()
99 
100 
108  public function _getAllowedLinks()
109  {
110  return Array();
111 
112  }//end _getAllowedLinks()
113 
114 
121  public function getFieldNamesFromRecordSet()
122  {
123  return Array();
124 
125  }//end getFieldNamesFromRecordSet()
126 
127 
136  public function getResultSet($result_id='')
137  {
138  return $this->getCachedResultSet($result_id);
139 
140  }//end getResultSet()
141 
142 
151  public function getCachedResultSet($cache_key='')
152  {
153  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
154  $result = $cm->loadFromCache($this->id, $this->type(), $cache_key, FALSE);
155 
156  // empty string means that the result was not serialised
157  // which means cache manager does not have an entry for this cache_key
158  if ($result === FALSE) return FALSE;
159 
160  $result = unserialize($result);
161  return $result;
162 
163  }//end getCachedResultSet()
164 
165 
176  public function setResultSet(Array $result, $result_id='')
177  {
178  $this->cacheResultSet($result, $result_id);
179 
180  }//end setResultSet()
181 
182 
192  public function cacheResultSet(Array $result, $cache_key='')
193  {
194  if (!empty($cache_key)) {
195  $this->setAttrValue('cache_key', $cache_key);
196  }
197 
198  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
199  $r = $cm->saveToCache($this->id, $this->type(), $cache_key, serialize($result), FALSE);
200 
201  }//end cacheResultSet()
202 
203 
224  public function getPermission($assetid, $permission, $granted=NULL, $and_greater=TRUE, $expand_groups=FALSE, $all_info=FALSE)
225  {
226  return Array();
227 
228  }//end getPermission()
229 
230 
242  public function setPermission($assetid, $data_source_record_setid, $permission, $granted)
243  {
244  return FALSE;
245 
246  }//end setPermission()
247 
248 
259  public function deletePermission($assetid, $data_source_record_setid, $permission)
260  {
261  return FALSE;
262 
263  }//end deletePermission()
264 
265 
280  public function getAssetInfo(Array $assetids, $type_code=Array(), $strict_type_code=TRUE, $field='')
281  {
282  $info = Array();
283  foreach ($assetids as $assetid) {
284  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
285  $keywords = $asset->getAvailableKeywords();
286  // if a sort field is specified then return only that field value else return all of the fields
287  if (!empty($field)) {
288  if ($field === 'type_code') {
289  $info[$assetid] = $asset->type();
290  } else if (isset($asset->{$field})) {
291  $info[$assetid] = $asset->{$field};
292  } else {
293  trigger_error('Invalid sort field for the asset type Data Source Record Set', E_USER_ERROR);
294  }
295  } else {
296  $info[$assetid] = Array(
297  'type_code' => 'data_source_record_set',
298  'version' => $asset->version,
299  'name' => $asset->name,
300  'short_name' => $asset->short_name,
301  'status' => $asset->status,
302  'languages' => '',
303  'charset' => '',
304  'force_secure' => '0',
305  'created' => $asset->getKeywordReplacement('asset_created'),
306  'created_userid' => $asset->created_userid,
307  'updated' => $asset->getKeywordReplacement('asset_updated'),
308  'updated_userid' => $asset->updated_userid,
309  'published' => $asset->getKeywordReplacement('asset_published'),
310  'published_userid' => $asset->published_userid,
311  'status_changed' => $asset->getKeywordReplacement('asset_status_changed'),
312  'status_changed_userid' => $asset->status_changed_userid,
313  );
314  }//end if field not empty
315  }//end foreach of assetids
316  return $info;
317 
318  }//end getAssetInfo()
319 
320 
332  public function assetExists($assetids)
333  {
334  return FALSE;
335 
336  }//end assetExists()
337 
338 
360  public function getLink($assetid, $link_type=NULL, $type_code='', $strict_type_code=TRUE, $value=NULL, $side_of_link='major', $exclusive=NULL)
361  {
362  return Array();
363 
364  }//end getLink()
365 
366 
378  public function getLinkById($linkid, $assetid=0, $side_of_link='major')
379  {
380  return Array();
381 
382  }//end getLinkById()
383 
384 
406  public function countLinks($assetid, $side_of_link='major', $link_types=0, $type_code='', $strict_type_code=TRUE, $ignore_linkid=0)
407  {
408  return 0;
409 
410  }//end countLinks()
411 
412 
433  public function getLinkByAsset($assetid, $other_assetid, $link_types=NULL, $value=NULL, $side_of_link='major', $force_array=FALSE, $dependant=NULL, $exclusive=NULL)
434  {
435  return Array();
436 
437  }//end getLinkByAsset()
438 
439 
449  public function getAllChildLinks($assetid, $link_type=0)
450  {
451  return Array();
452 
453  }//end getAllChildLinks()
454 
455 
463  function getBinaryDataAttributes()
464  {
465  return Array();
466 
467  }//end getBinaryDataAttributes()
468 
469 
484  public function updateLink($linkid, $link_type=NULL, $value=NULL, $sort_order=NULL)
485  {
486  return FALSE;
487 
488  }//end updateLink()
489 
490 
500  public function deleteAssetLink($linkid, $moving=FALSE)
501  {
502  return FALSE;
503 
504  }//end deleteAssetLink()
505 
506 
523  public function createAssetLink(Asset $major, Asset $minor, $link_type, $value='', $sort_order=NULL, $dependant='0', $exclusive='0', $moving=FALSE)
524  {
525  return 0;
526 
527  }//end createAssetLink()
528 
529 
551  public function getLinks($assetid, $link_types, $type_code='', $strict_type_code=TRUE, $side_of_link='major', $sort_by=NULL)
552  {
553  if (!($link_types & SQ_LINK_TYPE_1)) {
554  return Array();
555  }
556 
557  if (!is_array($type_code)) {
558  if (empty($type_code)) {
559  $type_code = Array();
560  } else {
561  $type_code = Array($type_code);
562  }
563  }
564  $type_code_key = implode('-', $type_code);
565  $links = Array();
566 
567  if ($side_of_link == 'major') {
568  if ($assetid == $this->id) {
569  $result = $this->filterResultSet($this->getResultSet());
570  if (empty($result)) {
571  return Array();
572  } else {
573  $identity = 0;
574  $index = 0;
575  foreach ($result as $record) {
576  $links[] = Array(
577  'linkid' => 0,
578  'majorid' => $this->id,
579  'minorid' => $this->id.':'.$identity,
580  'minor_type_code' => 'data_source_record_set',
581  'value' => '',
582  'link_type' => SQ_LINK_TYPE_1,
583  'is_dependant' => FALSE,
584  'is_exclusive' => FALSE,
585  'sort_order' => $index,
586  'locked' => 0,
587  );
588  $index++;
589  $identity++;
590  }
591  }
592  } else {
593  $links = Array();
594  }
595  }
596 
597  return $links;
598 
599  }//end getLinks()
600 
601 
622  public function getChildren($assetid, $type_code='', $strict_type_code=TRUE, $dependant=NULL, $sort_by=NULL)
623  {
624  // now that we have an array in the format we wanted (sorted or not)
625  // we can go through and construct our children array
626  $children = Array();
627  $links = Array();
628  if ($assetid == $this->id) {
629  $links = $this->getLinks($this->id, SQ_LINK_TYPE_1, 'data_source_record_set', TRUE, 'major', NULL);
630  }
631  if (!empty($links)) {
632  foreach ($links as $link) {
633  $children[$link['minorid']][0]['type_code'] = $link['minor_type_code'];
634  }
635  }
636  return $children;
637 
638  }//end getChildren()
639 
640 
656  public function getAsset($assetid, $type_code='', $mute_errors=FALSE, $return_null=FALSE)
657  {
658  $shadowid = explode(':', $assetid);
659  $shadowid = array_pop($shadowid);
660  $asset = NULL;
661  if (is_null($shadowid)) return $asset;
662  $result = $this->filterResultSet($this->getResultSet());
663 
664  if (!empty($result)) {
665  if (isset($result[$shadowid])) {
666  $record = $result[$shadowid];
667  $record['shadow_asset_name'] = $this->attr('shadow_name');
668  $record['shadow_asset_id'] = $shadowid;
669  $GLOBALS['SQ_SYSTEM']->am->includeAsset('data_source_record_set');
670  $asset = new Data_Source_Record_Set($this->id, $record);
671  }
672  }
673 
674  if(is_null($asset)) {
675  return $return_null ? NULL : $this->_getDummyAsset($shadowid);
676  } else {
677  return $asset;
678  }
679 
680  }//end getAsset()
681 
682 
691  protected function _getDummyAsset($shadowid){
692 
693  $record['shadow_asset_name'] = 'Unknown Data Source Record';
694  $record['shadow_asset_id'] = $shadowid;
695  $GLOBALS['SQ_SYSTEM']->am->includeAsset('data_source_record_set');
696  $asset = new Data_Source_Record_Set($this->id, $record);
697  return $asset;
698 
699  }//end _getDummyAsset()
700 
701 
722  public function getParents($assetid, $type_code='', $strict_type_code=TRUE, $sort_by=NULL, $access=NULL)
723  {
724  // get its own (datasource's) parents first
725  $id_parts = explode(':', $assetid);
726  $parents = $GLOBALS['SQ_SYSTEM']->am->getParents($id_parts[0]);
727  // after getting its own parent, the data soure adds itself as a pareent into the array as the shadow asset is its child
728  $parents[$this->id] = $this->type();
729  return $parents;
730 
731  }//end getParents()
732 
733 
742  public function getAssetMapAssetInfo($assetid)
743  {
744  return Array();
745 
746  }//end getAssetMapAssetInfo()
747 
748 
759  public function getLineageFromURL($assetid, $protocol, $url)
760  {
761  return Array();
762 
763  }//end getLineageFromURL()
764 
765 
774  function filterResultSet($result)
775  {
776  if (!$this->attr('filter_enabled')) {
777  return $result;
778  }
779  $filters = $this->getDatasourceFilters();
780  if (empty($filters)) {
781  return $result;
782  }
783 
784  $grouping_logic = $this->attr('filter_logic');
785  $filter_mode = $this->attr('filter_mode');
786 
787  // Check the cache
788  $hash = md5($this->attr('cache_key').$grouping_logic.$filter_mode.serialize($filters));
789  if (isset($this->tmp[$hash])) {
790  return $this->tmp[$hash];
791  }
792  $cached_result = $this->getCachedResultSet($hash);
793  if ($cached_result !== FALSE) {
794  $this->tmp[$hash] = $cached_result;
795  return $cached_result;
796  }
797 
798  $filtered_result = Array();
799  foreach($result as $index => $recordset) {
800  $condition_matched = TRUE;
801  foreach($filters as $filter) {
802  $field_name = $filter['name'];
803  $field_value = $filter['value'];
804  $match_type = $filter['match_type'];
805  $match_case = $filter['match_case'];
806  if (!isset($recordset[$field_name])) {
807  continue;
808  }
809 
810  $record_value = trim($recordset[$field_name]);
811  $condition_matched = FALSE;
812  switch($match_type) {
813  case 'contains':
814  if ($match_case == 'ignore_case') {
815  $condition_matched = stripos($record_value, $field_value) !== FALSE;
816  } else {
817  $condition_matched = strpos($record_value, $field_value) !== FALSE;
818  }
819  break;
820 
821  case 'exact':
822  if ($match_case == 'ignore_case') {
823  $condition_matched = strtolower($record_value) == strtolower($field_value);
824  } else {
825  $condition_matched = $record_value == $field_value;
826  }
827  break;
828 
829  case 'regex':
830  $condition_matched = preg_match('/'.str_replace('/', '\/',$field_value).'/'.($match_case == 'ignore_case' ? 'i' : ''), $record_value);
831  break;
832  }//end switch
833 
834  // Allow empty string comparision
835  if (!$condition_matched && empty($record_value) && empty($field_value)) {
836  $condition_matched = TRUE;
837  }
838 
839  if ($grouping_logic == 'OR' && $condition_matched) {
840  break;
841  } else if ($grouping_logic == 'AND' && !$condition_matched) {
842  break;
843  }
844 
845  }//end foreach
846 
847  if ($condition_matched && $filter_mode == 'include') {
848  $filtered_result[] = $recordset;
849  } else if (!$condition_matched && $filter_mode == 'exclude') {
850  $filtered_result[] = $recordset;
851  }
852 
853  }//end foreach
854 
855  // Cache the result
856  $this->cacheResultSet($filtered_result, $hash);
857  $this->tmp[$hash] = $filtered_result;
858 
859  return $filtered_result;
860 
861  }//end filterRecordSet()
862 
863 
864 
871  function getDatasourceFilters()
872  {
873  $filters = $this->attr('record_filters');
874  if (empty($filters)) {
875  return Array();
876  }
877 
878  // Keyword pattern for the dynamic keyword
879  $keyword_pattern = '('.'%%'.'([a-zA-Z_\-0-9\.]+)'.'%%'.')';
880 
881  foreach($filters as $index => $filter) {
882  if (empty($filter['name'])) {
883  continue;
884  }
885  // Replace the dynamic keyword in the filter's value
886  $filter_val = $filter['value'];
887  preg_match_all ('/'.$keyword_pattern.'/', $filter_val, $matches, PREG_PATTERN_ORDER);
888 
889  foreach($matches[2] as $keyword) {
890  $value = $this->getDynamicVariableValue($keyword);
891  $filter_val = str_replace('%%'.$keyword.'%%', $value, $filter_val);
892  }//end foreach
893  $filters[$index]['value'] = $filter_val;
894  }//end foreach
895 
896  return $filters;
897 
898  }//end getDatasourceFilters()
899 
900 
909  function getDynamicVariableValue($variable_name)
910  {
911  $dynamic_vars = $this->attr('dynamic_vars');
912  if (!isset($dynamic_vars[$variable_name])) return '';
913 
914  $parameter_map = $this->getAttribute('parameter_map');
915  $value = $parameter_map->getParameterValue($variable_name);
916 
917  if (empty($value)) {
918  $value = array_get_index($dynamic_vars, $variable_name, '');
919  }
920 
921  return $value;
922 
923  }//end getDynamicVariableValue()
924 
933  function delete($release_lock)
934  {
935  if (parent::delete($release_lock)) {
936  $this->_deleteStoredContents();
937  return TRUE;
938  }
939 
940  return FALSE;
941 
942  }//end delete()
943 
944 
951  function _deleteStoredContents()
952  {
953 
954  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
955  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
956 
957  $sql = 'DELETE FROM sq_shdw_ast_lnk WHERE minorid LIKE :minorid';
958 
959  $query = MatrixDAL::preparePdoQuery($sql);
960  MatrixDAL::bindValueToPdo($query, 'minorid', $this->id.':%');
961  $result = MatrixDAL::execPdoQuery($query);
962 
963  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
964  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
965 
966  return TRUE;
967 
968  }//end _deleteStoredContents()
969 
970 }//end class
971 
972 ?>