Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_add_assetid_remap.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 
49  public static function execute($settings, &$state)
50  {
51  if (empty($state['asset'])) {
52  // grab the asset if assetid is given, but not the asset.
53  if (empty($state['assetid'])) {
54  return FALSE;
55  } else {
56  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
57  }
58  }
59  if (is_null($state['asset'])) return FALSE;
60 
61  $am = $GLOBALS['SQ_SYSTEM']->am;
62 
63  $result = Array(
64  'assetid' => $state['asset']->id,
65  'remap_urls' => '',
66  );
67 
68  // No URL is assigned, nothing we can do here.
69  $urls = $state['asset']->getURLs();
70  if (empty($urls)) return $result;
71 
72  // No site, no URL, I know, it's the same as the above.
73  // Anyway I need the sites later
74  $sites = $am->getParents($state['asset']->id, 'site');
75  if (empty($sites)) return $result;
76 
77  // Get the list of tiny URLs we need to add
78  $tiny_urls = Array();
79  foreach (array_keys($sites) as $site_id) {
80 
81  $site = $am->getAsset($site_id);
82  $site_urls = $site->getSiteURLs();
83 
84  if (empty($site_urls)) {
85  // The site does not have URL yet
86  continue;
87  } else {
88  $tiny_urls = array_merge($tiny_urls, $site_urls);
89  }
90 
91  }
92 
93  // Map tiny URL
94  $mapped_tiny_urls = Array();
95  if (!empty($tiny_urls)) {
96  foreach ($tiny_urls as $tiny_url) {
97 
98  foreach ($urls as $url_info) {
99  if ( ($pos = strpos($url_info['url'], $tiny_url['url'].'/')) === 0 ) {
100  // a URL can have both HTTP and HTTPS options ticked
101  $protocols = Array();
102  if ($url_info['http'] == 1) {
103  $protocols[] = 'http://';
104  }
105 
106  if ($url_info['https'] == 1) {
107  $protocols[] = 'https://';
108  }
109 
110  for ($i=0; $i<count($protocols); $i++) {
111  $old_url = $protocols[$i].$tiny_url['url'].'/'.$state['asset']->id;
112  $mapped_tiny_urls[$old_url] = $protocols[$i].$url_info['url'];
113  }
114 
115  // If the asset has multiple webpaths, we just have to choose the first URL.
116  break;
117  }
118  }
119 
120  }
121  } else {
122  return $result;
123  }
124 
125  // Finally, insert Remap URLs
126  if (!empty($mapped_tiny_urls)) {
127 
128  // Remove the previous remapped URLs already exist
129  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
130  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
131 
132  $old_urls = array_keys($mapped_tiny_urls);
133 
134  try {
135  $bind_vars = Array (
136  'urls' => $old_urls,
137  );
138  MatrixDAL::executeQuery('core', 'deleteRemapUrls', $bind_vars);
139  } catch (Exception $e) {
140  throw new Exception('Unable to delete remap urls due to the following database error:'.$e->getMessage());
141  }//end try catch
142 
143  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
144  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
145 
146  $rm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('remap_manager');
147  foreach ($mapped_tiny_urls as $old_url => $new_url) {
148  $remap_result = $rm->addRemapURL($old_url, $new_url);
149  switch ($remap_result) {
150  case 0:
151  trigger_error('CORE0271', E_USER_WARNING);
152  break;
153  case -1:
154  trigger_error('CORE0272', E_USER_WARNING, $old_url);
155  break;
156  }
157  }
158  }//end if
159 
160  $result['remap_urls'] = $mapped_tiny_urls;
161  return $result;
162 
163  }//end execute()
164 
165 
176  public static function getInterface($settings, $prefix, $write_access=FALSE)
177  {
178  ob_start();
179  echo translate('trigger_action_add_remap_message');
180  return ob_get_clean();
181 
182  }//end getInterface()
183 
184 
194  public static function getLocks($settings, &$state)
195  {
196  return Array($state['assetid'] => Array('lookups'));
197 
198  }//end getLocks()
199 
200 
201 }//end class
202 
203 ?>