Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
sharepoint_saved_search.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/data_source/data_source/data_source.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 require_once SQ_PACKAGES_PATH.'/sharepoint/lib/sharepoint_common.inc';
22 
35 {
36 
37 
44  function __construct($assetid=0)
45  {
46  parent::__construct($assetid);
47 
48  $this->_ser_attrs = TRUE;
49  $this->search_clauses = NULL;
50 
51  }//end constructor
52 
53 
64  protected function _createAdditional(Array &$link, $args=Array())
65  {
66  if (!parent::_createAdditional($link)) return FALSE;
67  $this->setAttrValue('shadow_name', '%ds__ows_FileLeafRef%');
68 
69  return TRUE;
70 
71  }//end _createAdditional()
72 
73 
87  public function _getAllowedLinks()
88  {
89  // any link is allowed
90  $allowed_link['asset']['card'] = 'M';
91  $allowed_link['asset']['exclusive'] = FALSE;
92 
93  $links[SQ_LINK_TYPE_1] = $allowed_link;
94  $links[SQ_LINK_TYPE_2] = $allowed_link;
95  $links[SQ_LINK_TYPE_3] = $allowed_link;
96  $links[SQ_LINK_NOTICE] = $allowed_link;
97 
98  return $links;
99 
100  }//end _getAllowedLinks()
101 
102 
109  public function &getResultSet()
110  {
111  $hash = $this->getHashKey();
112 
113  $data_list = $this->attr('data_list');
114  if (!empty($data_list) && $data_list != 'None') {
115  $result = $this->getCachedResults($hash);
116  if (empty($result)) {
117  try {
118  $connection = $this->getConnection();
119 
120  $all_lists = Array();
121  $lists_fields = Array();
122  $list_names = Array();
123 
124  $valid = Sharepoint_Common::test_sharepoint_connection($connection);
125  //$connection['url'] = Sharepoint_Common::getWSDLLocation('lists', $connection['url']);
126  if ($valid) {
127  $all_lists = Sharepoint_Common::getListCollections($connection);
128  $sp_lists = Sharepoint_Common::getListNames($connection, $all_lists);
129  $list_names += $sp_lists;
130  }//end if
131 
132  $search_crits = $this->attr('search_options');
133  $search_logic = $this->attr('search_logic');
134  $row_limit = $this->attr('row_limit');
135  $search_crit = isset($search_crits[$data_list]) ? $search_crits[$data_list] : Array();
136  $download_file = $this->attr('download_file');
137  $bridge_info = $download_file ? Array('data_path' => $this->data_path, 'url' => $this->getURL()) : Array();
138 
139  $result = Sharepoint_Common::getListItems($connection, $data_list, $search_crit, $search_logic, $row_limit, $download_file, $bridge_info);
140  if (!empty($result)) sort($result);
141  $this->cacheResult($result, $hash);
142 
143  } catch (SoapFault $sf) {
144  $detail = '';
145  if (isset($sf->detail)) $detail = $sf->detail;
146  trigger_error($sf->getMessage().'.'.$detail, E_USER_WARNING);
147  }//end try catch
148  }//end else
149  }//end if
150 
151  return $result;
152 
153  }//end getResultSet()
154 
155 
164  public function getCachedResults($hash_key)
165  {
166  $result = $this->attr('cached_result');
167  if (isset($result[$hash_key])) {
168  return $result[$hash_key];
169  }//end if
170 
171  return Array();
172 
173  }//end cacheResult()
174 
175 
185  public function cacheResult($result, $hash_key)
186  {
187  $cached_result = $this->attr('cached_result');
188  $cached_result[$hash_key] = $result;
189  $GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'attributes');
190  if ($this->setAttrValue('cached_result', $cached_result) && $this->saveAttributes()) {
191  return TRUE;
192  }//end if
193  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'attributes');
194 
195  return FALSE;
196 
197  }//end cacheResult()
198 
199 
206  public function updateCachedResult($forced=FALSE)
207  {
208  $hash = $this->getHashKey();
209 
210  $result = $this->attr('cached_result');
211  if (isset($result[$hash])) {
212  unset($result[$hash]);
213  }//end if
214  $this->setAttrValue('cached_result', $result);
215  $new_result = $this->getResultSet();
216  if (!empty($new_result)) {
217  return TRUE;
218  } else {
219  return FALSE;
220  }//end else
221 
222  }//end updateCachedResult()
223 
224 
231  function getHashKey() {
232 
233  // Hash Key was used before when this asset uses the Cache Manager to manage its cache
234  // Since attribute is used now, it's hard to go back and clear the cache for different configuration
235  // so lets just use one cache set instead of multiple sets like before
236  /*
237  $hash = md5($unhashed_key);*/
238  $hash = md5('resultSet');
239 
240  return $hash;
241 
242  }//end getHashKey()
243 
244 
251  function getConnection()
252  {
253  $connector_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'sharepoint_connector', FALSE, 'sharepoint_connector');
254  if (!empty($connector_link)) {
255  $connector = $GLOBALS['SQ_SYSTEM']->am->getAsset($connector_link['minorid']);
256  } else {
257  $connector = $this;
258  }//end else
259 
260  $connection = Array();
261  $connection['url'] = $connector->attr('url');
262  $connection['authentication'] = Array (
263  'login' => $connector->attr('username'),
264  'password' => $connector->attr('password'),
265  'trace' => '1',
266  'exception' => '1',
267  );
268 
269  return $connection;
270 
271  }//end getConnection()
272 
273 
282  public function getDynamicVariableValue($variable_name)
283  {
284  $dynamic_vars = $this->attr('dynamic_vars');
285  if (!isset($dynamic_vars[$variable_name])) return '';
286 
287  $parameter_map = $this->getAttribute('parameter_map');
288  $value = $parameter_map->getParameterValue($variable_name);
289 
290  if (empty($value)) {
291  $value = array_get_index($dynamic_vars, $variable_name, '');
292  }//end if
293 
294  return $value;
295 
296  }//end getDynamicVariableValue()
297 
298 
305  public function printFrontend()
306  {
307  // We are only doing synching in this asset, there's no need to check for update
308  if (isset($_GET['synch_now']) && !empty($_GET['synch_now'])) {
309  if ($this->adminAccess()) {
310  if ($this->updateCachedResult()) {
311  echo '1';
312  } else {
313  echo '0';
314  }//end if
315  }//end if
316  }//end else if
317 
318  // Get the sharepoint document from local server
319  if (isset($_GET['file_uri']) && !empty($_GET['file_uri'])) {
320  if (!$this->getAttachment($_GET['file_uri'])) {
321  trigger_error('Cannot find the given file '.$_GET['file_uri'], E_USER_WARNING);
322  }
323  }
324 
325  }//end printFrontend()
326 
327 
341  public function setAttrValue($name, $value)
342  {
343  if ($name == 'data_list' || $name == 'row_limit' || $name == 'search_logic' || $name == 'download_file') {
344  $res_val = parent::setAttrValue($name, $value);
345  $this->updateCachedResult(TRUE);
346  return $res_val;
347  } else {
348  return parent::setAttrValue($name, $value);
349  }//end if
350  }//end setAttrValue()
351 
352 
367  public function getAsset($assetid, $type_code='', $mute_errors=FALSE)
368  {
369  $asset = parent::getAsset($assetid, $type_code='', $mute_errors=FALSE);
370  if (isset($_REQUEST['a']) && isset($_REQUEST['a']) == $assetid) {
371  // Now we are looking for shadow assets to download
372  if (!is_null($asset)) {
373  if (isset($asset->vars['Uri']) && !empty($asset->vars['Uri'])) {
374  if ($this->getAttachedFile(($asset->vars['Uri']))) {
375  exit();
376  }
377  }//end if
378  }//end if
379  }//end if
380 
381  return $asset;
382  }//end getAsset()
383 
384 
393  public function getAttachment($uri)
394  {
395  require_once SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
396 
397  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('SQ_REPLACE_MYSOURCE_LEVEL_KEYWORDS', FALSE);
398 
399  $file_path = $this->data_path.'/'.$uri;
400  if (!file_exists($file_path)) {
401  return FALSE;
402  }//end else
403 
404  $file_info = pathinfo($file_path);
405  if (empty($file_info)) {
406  return FALSE;
407  }//end if
408 
409  $ext = strtolower($file_info['extension']);
410  $type = (empty($standards_lists_mime_types[$ext])) ? 'text/plain' : $standards_lists_mime_types[$ext];
411 
412  // Fix for IE caching
413  header("Pragma: public");
414  header("Expires: 0");
415  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
416  header("Cache-Control: private",false);
417 
418  // We'll be outputting a file
419  header('Content-Type: '.$type);
420 
421  // Set the name of the file
422  header('Content-Disposition: attachment; filename="'.$file_info['basename'].'";');
423  header('Content-Length: '.filesize($file_path));
424 
425  // Get the source file
426  readfile($file_path);
427 
428  return TRUE;
429  @ob_flush();
430 
431  }//end getAttachment()
432 
433 
434 
435 }//end class
436 ?>