Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_search_replace.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
18 
35 {
36 
37 
44  function __construct($assetid=0)
45  {
46  parent::__construct($assetid);
47 
48  }//end constructor
49 
50 
60  static function paintTool(&$o, $type_code)
61  {
62  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
63 
64  // user input of search and replace strings
65  $o->openSection(translate('sch_search_terms'));
66  $o->openField(translate('sch_search_string'));
67  text_box($type_code.'_search_string', '', 40);
68  $o->closeField();
69 
70  $o->openField(translate('sch_tool_global_sr_replace_string'));
71  text_box($type_code.'_replace_string', '', 40);
72  $o->closeField();
73  $o->openField('');
74  check_box($type_code.'_match_case');
75  label(translate('sch_tool_global_sr_match_case'), $type_code.'_match_case');
76  $o->closeField();
77  $o->closeSection();
78 
79  // allow restrictions based on asset type and asset status
80  $o->openSection(translate('sch_search_restrictions'));
81  $o->openField(translate('asset_types'));
82  asset_type_chooser($type_code.'_search_types', TRUE, Array());
83  $o->closeField();
84 
85  $o->openField(translate('asset_status'));
86  $statuses = get_constant_values('SQ_STATUS');
87  $options = Array();
88  foreach ($statuses as $constant => $value) {
89  $options[$value] = get_status_description($value);
90  }
91  combo_box($type_code.'_statuses', $options, TRUE, '', 10);
92  $o->closeField();
93  $o->closeSection();
94 
95  // limit the search to certain parts (or all) of the system
96  $o->openSection(translate('sch_search_location'));
97  $o->openField(translate('root_node'));
98  asset_finder($type_code.'_root_assetid', '');
99  echo '&nbsp;&nbsp;&nbsp;<b>'.strtoupper(translate('or')).'</b>';
100  $o->closeField();
101 
102  $o->openField(translate('sch_system_wide'));
103  check_box($type_code.'_root_folder');
104  label(translate('sch_search_entire_system'), $type_code.'_root_folder');
105  $o->closeField();
106 
107  $o->closeSection();
108 
109  }//end paintTool()
110 
111 
124  static function processTool(&$o, $type_code)
125  {
126  // ensure we have a search string
127  if ($_REQUEST[$type_code.'_search_string'] == '') {
128  trigger_localised_error('SCH0002', E_USER_NOTICE);
129  return FALSE;
130  }
131 
132  if (!isset($_REQUEST[$type_code.'_search_types'])) {
133  trigger_localised_error('SCH0003', E_USER_NOTICE);
134  return FALSE;
135  }
136 
137  if (!isset($_REQUEST[$type_code.'_statuses'])) {
138  trigger_localised_error('SCH0004', E_USER_NOTICE);
139  return FALSE;
140  }
141 
142  if (empty($_REQUEST[$type_code.'_root_assetid']['assetid']) && !isset($_REQUEST[$type_code.'_root_folder'])) {
143  trigger_localised_error('SCH0005', E_USER_NOTICE);
144  return FALSE;
145  }
146 
147  $sm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('search_manager');
148 
149  if (strlen($_REQUEST[$type_code.'_search_string']) < $sm->attr('min_word_length')) {
150  trigger_localised_error('SCH0026', E_USER_NOTICE, $_REQUEST[$type_code.'_search_string'], $sm->attr('min_word_length'));
151  return FALSE;
152  }
153 
154  // create search types array
155  $search_types = Array();
156  foreach ($_REQUEST[$type_code.'_search_types'] as $type) {
157  if (!empty($type)) $search_types[$type] = 1;
158  }
159 
160  // work out our root node position
161  if (isset($_REQUEST[$type_code.'_root_folder'])) {
162  $root_assetid = Array();
163  } else {
164  $root_assetid = Array($_REQUEST[$type_code.'_root_assetid']['assetid']);
165  }
166 
167  $words = strtolower($_REQUEST[$type_code.'_search_string']);
168  $words = explode(' ', $words);
169 
170  $search_details = Array(
171  'roots' => $root_assetid,
172  'asset_types' => $search_types,
173  'statuses' => $_REQUEST[$type_code.'_statuses'],
174  'fields' => Array(
175  'search_word' => Array(
176  'words' => $words,
177  'word_logic' => 'AND',
178  'data_sources' => Array(
179  Array('type' => 'include_all'),
180  ),
181  ),
182  ),
183  'field_logic' => 'AND',
184  );
185 
186  $search_results = $sm->processSearch($search_details, TRUE);
187 
188  // no matches were found, trigger error and paint getInterface
189  if (!$search_results) {
190  trigger_localised_error('SCH0006', E_USER_NOTICE, $_REQUEST[$type_code.'_search_string']);
191  return FALSE;
192  }
193 
194  $formatted_results = Tool_Search_Replace::getFormattedResults($search_results, $_REQUEST[$type_code.'_search_string']);
195 
196  // after formatting the results, there were no real replaceable matches
197  if (empty($formatted_results)) {
198  trigger_localised_error('SCH0006', E_USER_NOTICE, $_REQUEST[$type_code.'_search_string']);
199  return FALSE;
200  }
201 
202  $vars = Array(
203  'root_assetid' => $root_assetid,
204  'search_data' => $formatted_results,
205  'search_string' => $_REQUEST[$type_code.'_search_string'],
206  'replace_string' => $_REQUEST[$type_code.'_replace_string'],
207  'match_case' => isset($_REQUEST[$type_code.'_match_case']),
208  'contextid' => $GLOBALS['SQ_SYSTEM']->getContextId(),
209  );
210 
211  // redirect data to the HIPO
212  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
213  $hh->queueHipo('hipo_job_tool_search_replace', $vars, '', SQ_PACKAGES_PATH.'/search/hipo_jobs');
214  $url = $hh->runQueuedJobs();
215  if (!empty($url)) $o->setRedirect($url);
216  return TRUE;
217 
218  }//end processTool()
219 
220 
233  function getFormattedResults($results, $search_string)
234  {
235  $search_string = preg_replace('/([\.\\\!\+\*\?\[\]\^\$\(\)\=\!<>\|\:\/])/i', '\\\${1}',$search_string);
236 
237  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
238 
239  $formatted_results = Array();
240 
241  // transform the array into a better working format
242  foreach ($results as $result) {
243  $searched_results[$result['assetid']][] = $result['source'];
244  }
245 
246  // populate the results array as required
247  // note we dont include fields that dont have matches
248  foreach ($searched_results as $assetid => $content) {
249  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
250 
251  foreach ($content as $content_type) {
252  switch ($content_type['source']) {
253 
254  // asset contents containing search string
255  case preg_match('/contents/', $content_type) > 0:
256  $asset_contents = $GLOBALS['SQ_SYSTEM']->am->getEditableContents($assetid);
257  if ($asset_contents === FALSE) break;
258  foreach ($asset_contents as $contents_id => $contents_value) {
259  if (preg_match('/'.htmlentities($search_string).'/i', $contents_value) || preg_match('/'.($search_string).'/i', $contents_value)) {
260  $formatted_results[$assetid]['contents'][$contents_id] = $contents_value;
261  }
262  }
263  break;
264 
265  // attributes containing search string
266  // attributes come in 2 formats, __attribute__ and attr:attrname$
267  case preg_match('/__(.+)__/', $content_type, $matches) > 0:
268  case preg_match('/attr:(.+)$/', $content_type, $matches) > 0:
269  if (isset($asset->vars[$matches[1]])) {
270  $attribute = $asset->attr($matches[1]);
271 
272  if (preg_match("/$search_string/i", $attribute)) {
273  $formatted_results[$assetid]['attributes'][$matches[1]] = $attribute;
274  }
275  }
276  break;
277 
278  // metadata containing search string
279  case preg_match('/metadata/', $content_type) > 0:
280  // we've already set metadata for this asset
281  if (isset($formatted_results[$assetid]['metadata'])) {
282  break;
283  }
284 
285  $metadata = $mm->getMetadata($assetid);
286 
287  foreach ($metadata as $fieldid => $data) {
288  if (preg_match("/$search_string/i", $data[0]['value'])) {
289  $formatted_results[$assetid]['metadata'][$fieldid] = $data[0];
290  }
291  }
292 
293  // if no metadata was set, then we were looking for the schema
294  if (!isset($formatted_results[$assetid]['metadata'])) {
295  $schemas = $mm->getSchemas($assetid, TRUE);
296 
297  foreach ($schemas as $schemaid) {
298  $schema = $GLOBALS['SQ_SYSTEM']->am->getAsset($schemaid);
299  $schema_data = $mm->getSchemaDefaultValues($schema->id);
300 
301  foreach ($schema_data as $fieldid => $data) {
302  if (preg_match("/$search_string/i", $data['value'])) {
303  $formatted_results[$schema->id]['schema'][$fieldid] = $data;
304  }
305  }
306 
307  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($schema);
308  }
309  }
310  break;
311 
312  default:
313  break;
314 
315  }//end switch content type
316  }//end foreach $assetid
317 
318  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
319 
320  }//end foreach $results
321 
322  return $formatted_results;
323 
324  }//end getFormattedResults()
325 
326 
327 }//end class
328 
329 
330 ?>