Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_add_url.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 
32 {
33 
34 
54  public static function execute($settings, &$state)
55  {
56  $run_hipo = FALSE;
57  // check required settings
58  if (!isset($settings['url'])) return FALSE;
59 
60  // url not specified
61  if ($settings['url'] == '') return FALSE;
62 
63  $settings['http'] = array_get_index($settings, 'http', 0);
64  $settings['https'] = array_get_index($settings, 'https', 0);
65  $settings['base_contextid'] = array_get_index($settings, 'base_contextid', 0);
66 
67  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
68  // If the current setting doesn't exist, then revert to the default
69  if (array_key_exists($settings['base_contextid'], $all_contexts) === FALSE) {
70  $settings['base_contextid'] = 0;
71  }
72 
73  // protocols not specified
74  if (!$settings['http'] && !$settings['https']) {
75  return FALSE;
76  }
77 
78  if (empty($state['asset'])) {
79  // grab the asset if assetid is given, but not the asset.
80  if (empty($state['assetid'])) {
81  return FALSE;
82  } else {
83  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
84  }
85  }
86  if (is_null($state['asset'])) return FALSE;
87 
88  // if asset is not a site asset?
89  if (!($state['asset'] instanceof Site)) {
90  trigger_error('Unable to add URL for a '.$state['asset']->type().' asset', E_USER_NOTICE);
91  return FALSE;
92  }
93 
94  // dont change anything if the asset already has this URL
95  $url_exists = FALSE;
96  $urls = $state['asset']->getSiteURLs();
97  foreach ($urls as $key => $value) {
98  if ($value['url'] == $settings['url']) {
99  if ($value['http'] == $settings['http'] && $value['https'] == $settings['https']) {
100  $url_exists = TRUE;
101  break;
102  }
103  }
104  }
105 
106  if (!$url_exists) {
107  // add a url
108 
109  $urls[0] = Array(
110  'url' => $settings['url'],
111  'http' => $settings['http'],
112  'https' => $settings['https'],
113  'base_contextid' => $settings['base_contextid'],
114  );
115 
116  // save a new url
117  $old_urls = $state['asset']->getSiteURLs();
118  switch ($state['asset']->saveWebURLs($urls)) {
119  case 0:
120  return FALSE;
121  break;
122  case 2:
123  $run_hipo = TRUE;
124  break;
125  }
126 
127 
128 
129  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
130  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
131  $db = MatrixDAL::getDb();
132 
133  foreach ($urls as $urlid => $url_data) {
134  // Skip the new URL
135  if (!isset($old_urls[$urlid]) || $old_urls[$urlid]==$url_data) {
136  continue;
137  }
138 
139  $run_hipo = TRUE;
140 
141  // update the root url in the asset url table to the new url
142  try {
143  $bind_vars = Array (
144  'url' => strip_url($urls[$urlid]['url']),
145  'urlid' => $urlid,
146  );
147  $result = MatrixDAL::executeQuery('site', 'updateUrl', $bind_vars);
148  } catch (Exception $e) {
149  throw new Exception('Unable to update URL: '.strip_url($urls[$urlid]['url']).' for urlid: '.$urlid.' due to database error: '.$e->getMessage());
150  }
151 
152  // update any urls that use this url in the lookup and lookup value tables
153  foreach (Array('sq_ast_lookup_value', 'sq_ast_lookup') as $tablename) {
154  // Skip if the url was not changed
155  if ($urls[$urlid]['url'] == $old_urls[$urlid]['url']) {
156  continue;
157  }
158  $sql = 'UPDATE
159  '.$tablename.'
160  SET
161  url = :url || SUBSTR(url, :substr_arg)
162  WHERE
163  (url LIKE :old_url_like OR url LIKE :old_url_like_slash)';
164 
165 
166  if ($tablename == 'sq_ast_lookup') {
167  $sql .= ' AND root_urlid = :urlid';
168  } else if ($tablename == 'sq_ast_lookup_value') {
169  $sql .= ' AND url IN (
170  SELECT
171  l.url
172  FROM
173  sq_ast_lookup l
174  INNER JOIN
175  sq_ast_lookup_value v ON ((l.url = v.url) OR (l.url || \'/\' = v.url))
176  WHERE
177  l.root_urlid = :urlid
178  )';
179  }
180  try {
181  $query = MatrixDAL::preparePdoQuery($sql);
182  MatrixDAL::bindValueToPdo($query, 'url', $urls[$urlid]['url']);
183  MatrixDAL::bindValueToPdo($query, 'old_url_like', $old_urls[$urlid]['url'].'%');
184  MatrixDAL::bindValueToPdo($query, 'old_url_like_slash', $old_urls[$urlid]['url'].'%/');
185  MatrixDAL::bindValueToPdo($query, 'url', $urls[$urlid]['url']);
186  MatrixDAL::bindValueToPdo($query, 'substr_arg', strlen($old_urls[$urlid]['url'])+1);
187  MatrixDAL::bindValueToPdo($query, 'urlid', $urlid);
188  $result = MatrixDAL::executePdoOne($query);
189  } catch (Exception $e) {
190  throw new Exception('Unable to update this old URL: '.$old_urls[$urlid]['url'].' with this new URL: '.$urls[$urlid]['url'].' due to database error: '.$e->getMessage());
191  }
192  }
193  }//end foreach
194 
195  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
196  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
197 
198  flush();
199 
200  }//end if
201 
202 
203  if ($run_hipo) {
204  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
205  $vars = Array('assetids' => Array($state['asset']->id));
206  $hh->freestyleHipo('hipo_job_update_lookups', $vars);
207  }//end if
208 
209  return Array(
210  'assetid' => $state['asset']->id,
211  'url' => $settings['url'],
212  'http' => $settings['http'],
213  'https' => $settings['https'],
214  'base_contextid' => $settings['base_contextid'],
215  );
216 
217  }//end execute()
218 
219 
230  public static function getInterface($settings, $prefix, $write_access=FALSE)
231  {
232  ob_start();
233 
234  $url = array_get_index($settings, 'url', '');
235  $http = array_get_index($settings, 'http', '');
236  $https = array_get_index($settings, 'https', '');
237  $base_contextid = array_get_index($settings, 'base_contextid', 0);
238 
239  echo translate('add_url').' ';
240  if ($write_access) {
241  text_box($prefix.'[url]', $url, 20);
242  echo '&nbsp;';
243  check_box($prefix.'[http]', '1', $http);
244  echo '&nbsp;HTTP&nbsp;';
245  check_box($prefix.'[https]', '1', $https);
246  echo '&nbsp;HTTPS&nbsp;';
247  } else {
248  echo '<b>'.$url.'</b>';
249  }
250 
251  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
252  if (count($all_contexts) > 1) {
253  // If the current setting doesn't exist, then revert to the default
254  if (array_key_exists($base_contextid, $all_contexts) === FALSE) {
255  $base_contextid = 0;
256  }
257 
258  $options = Array();
259  foreach ($all_contexts as $contextid => $context_data) {
260  $options[$contextid] = $context_data['name'];
261  }
262  combo_box($prefix.'[base_contextid]', $options, FALSE, $base_contextid);
263  }
264 
265  return ob_get_clean();
266 
267  }//end getInterface()
268 
269 
281  public static function processInterface(&$settings, $request_data)
282  {
283 
284  // process url
285  $url = array_get_index($request_data, 'url', '');
286  $url = trim($url);
287  if ($url == '') return translate('url_not_specified');
288  $settings['url'] = $url;
289 
290  // process protocol
291  $http = array_get_index($request_data, 'http', 0);
292  $https = array_get_index($request_data, 'https', 0);
293  $base_contextid = (int)array_get_index($request_data, 'base_contextid', 0);
294  if (!$http && !$https) {
295  return translate('protocol_not_specified');
296  }
297 
298  $settings['http'] = ($http == '1') ? '1' : '0';
299  $settings['https'] = ($https == '1') ? '1' : '0';
300 
301  return FALSE;
302 
303  }//end processInterface()
304 
305 
315  public static function getLocks($settings, &$state)
316  {
317  return Array($state['assetid'] => Array('lookups'));
318 
319  }//end getLocks()
320 
321 
322 }//end class
323 
324 ?>