Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_remove_remaps.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 
30 {
31 
32 
51  public static function execute($settings, &$state)
52  {
53  // check required settings
54  if (!isset($settings['removefromto']) && !isset($settings['removetofrom'])) return FALSE;
55  $remove_from_to = ($settings['removefromto'] == '1') ? TRUE : FALSE;
56  $remove_to_from = ($settings['removetofrom'] == '1') ? TRUE : FALSE;
57 
58  // Grab the asset if not already grabbed
59  if (empty($state['asset'])) {
60  // grab the asset if assetid is given but not the asset.
61  if (empty($state['assetid'])) {
62  return FALSE;
63  } else {
64  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
65  }
66  }
67  if (is_null($state['asset'])) return FALSE;
68 
69  // Acquire the lock
70  $lock_acquired = ($GLOBALS['SQ_SYSTEM']->am->acquireLock($state['asset']->id, 'lookups') == 1);
71 
72  // Grab the URLS for this asset
73  $urls = $state['asset']->getURLs();
74 
75  try {
76  $results = MatrixDAL::executeAssoc('core', 'getRemapUrls');
77  } catch (DALException $e) {
78  throw new Exception('Unable to delete remaps due to database error: '.$e->getMessage());
79  }
80 
81  // Don't process those Never Delete remaps
82  $never_delete_urls = Array();
83  foreach ($results as $index => $result) {
84  if($result['never_delete']) {
85  $never_delete_urls[$result[$remove_from_to ? 'remap_url' : 'url']] = 1;
86  }
87  }
88 
89  foreach ($urls as $url) {
90  $address = Array();
91  if ($url['http'] == '1' && !isset($never_delete_urls['http://'.$url['url']])) {
92  $address[] = 'http://'.$url['url'];
93  }
94  if ($url['https'] == '1' && !isset($never_delete_urls['https://'.$url['url']])) {
95  $address[] = 'https://'.$url['url'];
96  }
97 
98  if (!empty($address)) {
99 
100  // Process removing remaps (from-to) ie. remapping to this address (remap_url is this address)
101  if ($remove_from_to) {
102  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
103  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
104  try {
105  $bind_vars = Array('urls' => $address);
106  MatrixDAL::executeQuery('remap_manager', 'deleteRemapsByRemap', $bind_vars);
107  } catch (DALException $e) {
108  throw new Exception('Unable to delete remaps due to database error: '.$e->getMessage());
109  }
110  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
111  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
112  }
113 
114  // Process removing remaps (to-from) ie. remapping from this address (url is this address)
115  if ($remove_to_from) {
116  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
117  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
118  try {
119  $bind_vars = Array('urls' => $address);
120  MatrixDAL::executeQuery('remap_manager', 'deleteRemapsByURL', $bind_vars);
121  } catch (DALException $e) {
122  throw new Exception('Unable to delete remaps due to database error: '.$e->getMessage());
123  }
124  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
125  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
126  }
127  }//end if
128  }//end foreach
129 
130  // Release the lock if acquired(!?!)
131  if ($lock_acquired) {
132  $GLOBALS['SQ_SYSTEM']->am->releaseLock($state['asset']->id, 'lookups');
133  }
134 
135  }//end execute()
136 
137 
148  public static function getInterface($settings, $prefix, $write_access=FALSE)
149  {
150  $remove_from_to = array_get_index($settings, 'removefromto', 1);
151  $remove_to_from = array_get_index($settings, 'removetofrom', 0);
152 
153  ob_start();
154  echo translate('trigger_schema_remove_remaps_message');
155  echo '<br />';
156  if ($write_access) {
157  check_box($prefix.'[removefromto]', '1', $remove_from_to, '');
158  label(translate('trigger_schema_remove_from_to_remaps_message'));
159  echo '<br />';
160  check_box($prefix.'[removetofrom]', '1', $remove_to_from, '');
161  label(translate('trigger_schema_remove_to_from_remaps_message'));
162  } else {
163  echo '<img src="'.sq_web_path('lib').'/web/images/'.($remove_from_to ? 'tick' : 'cross').'.gif" alt="'.($remove_from_to ? translate('yes') : translate('no')).'" /> ';
164  echo translate('trigger_schema_remove_from_to_remaps_message');
165  echo '<br />';
166  echo '<img src="'.sq_web_path('lib').'/web/images/'.($remove_to_from ? 'tick' : 'cross').'.gif" alt="'.($remove_to_from ? translate('yes') : translate('no')).'" /> ';
167  echo translate('trigger_schema_remove_to_from_remaps_message');
168  }
169 
170  $output = ob_get_contents();
171  ob_end_clean();
172 
173  return $output;
174 
175  }//end getInterface()
176 
177 
189  public static function processInterface(&$settings, $request_data)
190  {
191  $settings['removefromto'] = array_get_index($request_data, 'removefromto', FALSE);
192  $settings['removetofrom'] = array_get_index($request_data, 'removetofrom', FALSE);
193  return FALSE;
194 
195  }//end processInterface()
196 
197 
207  public static function getLocks($settings, &$state)
208  {
209  return Array($state['assetid'] => Array('lookups'));
210 
211  }//end getLocks()
212 
213 
214 }//end class
215 
216 ?>