17 require_once SQ_SYSTEM_ROOT.
'/core/assets/system/tool/tool.inc';
46 parent::__construct($assetid);
62 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
65 $o->openSection(translate(
'sch_search_terms'));
66 $o->openField(translate(
'sch_search_string'));
67 text_box($type_code.
'_search_string',
'', 40);
70 $o->openField(translate(
'sch_tool_global_sr_replace_string'));
71 text_box($type_code.
'_replace_string',
'', 40);
74 check_box($type_code.
'_match_case');
75 label(translate(
'sch_tool_global_sr_match_case'), $type_code.
'_match_case');
80 $o->openSection(translate(
'sch_search_restrictions'));
81 $o->openField(translate(
'asset_types'));
82 asset_type_chooser($type_code.
'_search_types', TRUE, Array());
85 $o->openField(translate(
'asset_status'));
86 $statuses = get_constant_values(
'SQ_STATUS');
88 foreach ($statuses as $constant => $value) {
89 $options[$value] = get_status_description($value);
91 combo_box($type_code.
'_statuses', $options, TRUE,
'', 10);
96 $o->openSection(translate(
'sch_search_location'));
97 $o->openField(translate(
'root_node'));
98 asset_finder($type_code.
'_root_assetid',
'');
99 echo
' <b>'.strtoupper(translate(
'or')).
'</b>';
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');
127 if ($_REQUEST[$type_code.
'_search_string'] ==
'') {
128 trigger_localised_error(
'SCH0002', E_USER_NOTICE);
132 if (!isset($_REQUEST[$type_code.
'_search_types'])) {
133 trigger_localised_error(
'SCH0003', E_USER_NOTICE);
137 if (!isset($_REQUEST[$type_code.
'_statuses'])) {
138 trigger_localised_error(
'SCH0004', E_USER_NOTICE);
142 if (empty($_REQUEST[$type_code.
'_root_assetid'][
'assetid']) && !isset($_REQUEST[$type_code.
'_root_folder'])) {
143 trigger_localised_error(
'SCH0005', E_USER_NOTICE);
147 $sm = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'search_manager');
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'));
155 $search_types = Array();
156 foreach ($_REQUEST[$type_code.
'_search_types'] as $type) {
157 if (!empty($type)) $search_types[$type] = 1;
161 if (isset($_REQUEST[$type_code.
'_root_folder'])) {
162 $root_assetid = Array();
164 $root_assetid = Array($_REQUEST[$type_code.
'_root_assetid'][
'assetid']);
167 $words = strtolower($_REQUEST[$type_code.
'_search_string']);
168 $words = explode(
' ', $words);
170 $search_details = Array(
171 'roots' => $root_assetid,
172 'asset_types' => $search_types,
173 'statuses' => $_REQUEST[$type_code.
'_statuses'],
175 'search_word' => Array(
177 'word_logic' =>
'AND',
178 'data_sources' => Array(
179 Array(
'type' =>
'include_all'),
183 'field_logic' =>
'AND',
186 $search_results = $sm->processSearch($search_details, TRUE);
189 if (!$search_results) {
190 trigger_localised_error(
'SCH0006', E_USER_NOTICE, $_REQUEST[$type_code.
'_search_string']);
197 if (empty($formatted_results)) {
198 trigger_localised_error(
'SCH0006', E_USER_NOTICE, $_REQUEST[$type_code.
'_search_string']);
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(),
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);
235 $search_string = preg_replace(
'/([\.\\\!\+\*\?\[\]\^\$\(\)\=\!<>\|\:\/])/i',
'\\\${1}',$search_string);
237 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
239 $formatted_results = Array();
242 foreach ($results as $result) {
243 $searched_results[$result[
'assetid']][] = $result[
'source'];
248 foreach ($searched_results as $assetid => $content) {
249 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
251 foreach ($content as $content_type) {
252 switch ($content_type[
'source']) {
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;
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]);
272 if (preg_match(
"/$search_string/i", $attribute)) {
273 $formatted_results[$assetid][
'attributes'][$matches[1]] = $attribute;
279 case preg_match(
'/metadata/', $content_type) > 0:
281 if (isset($formatted_results[$assetid][
'metadata'])) {
285 $metadata = $mm->getMetadata($assetid);
287 foreach ($metadata as $fieldid => $data) {
288 if (preg_match(
"/$search_string/i", $data[0][
'value'])) {
289 $formatted_results[$assetid][
'metadata'][$fieldid] = $data[0];
294 if (!isset($formatted_results[$assetid][
'metadata'])) {
295 $schemas = $mm->getSchemas($assetid, TRUE);
297 foreach ($schemas as $schemaid) {
298 $schema = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($schemaid);
299 $schema_data = $mm->getSchemaDefaultValues($schema->id);
301 foreach ($schema_data as $fieldid => $data) {
302 if (preg_match(
"/$search_string/i", $data[
'value'])) {
303 $formatted_results[$schema->id][
'schema'][$fieldid] = $data;
307 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($schema);
318 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
322 return $formatted_results;