17 require_once SQ_SYSTEM_ROOT.
'/core/assets/system/tool/tool.inc';
44 $this->
Tool($assetid);
62 $sort_type =
'sort_by_field';
64 $selected_asset_type =
'';
65 $selected_asset_attribute =
'';
68 if (isset($_POST[$type_code.
'_parent'][
'assetid'])) {
69 $parent_asset_id = (int) $_POST[$type_code.
'_parent'][
'assetid'];
72 if (isset($_POST[$type_code.
'_sort_type'])) {
73 $sort_type = addslashes($_POST[$type_code.
'_sort_type']);
76 if (isset($_POST[$type_code.
'_field'])) {
77 $sort_field = addslashes($_POST[$type_code.
'_field']);
80 if (isset($_POST[$type_code.
'_asset_type'])) {
81 $selected_asset_type = addslashes($_POST[$type_code.
'_asset_type']);
84 if (isset($_POST[$type_code.
'_asset_attribute'])) {
85 $selected_asset_attribute = addslashes($_POST[$type_code.
'_asset_attribute']);
88 if (isset($_POST[$type_code.
'_direction'])) {
89 $sort_direction = addslashes($_POST[$type_code.
'_direction']);
92 $sort_by_field = ($sort_type ==
'sort_by_field');
94 $o->openSection(translate(
'tool_asset_sorter_sort_options'));
95 $o->openField(translate(
'parent'));
96 asset_finder($type_code.
'_parent', $parent_asset_id);
98 $o->openField(translate(
'tool_asset_sorter_sort_type'));
100 'sort_by_field' =>
'Asset Field',
101 'sort_by_attribute' =>
'Asset Attribute'
103 combo_box($type_code.
'_sort_type', $options, FALSE, $sort_type, 0,
'onchange="setFieldType(this);"');
105 $o->openField(translate(
'tool_asset_sorter_sort_field'),
'',
'', !$sort_by_field,
'sort_field');
106 $options = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfoFields();
108 unset($options[
'force_secure']);
109 unset($options[
'version']);
110 unset($options[
'charset']);
111 unset($options[
'languages']);
112 combo_box($type_code.
'_field', $options, FALSE, $sort_field);
114 $o->openField(translate(
'asset_type'),
'',
'', $sort_by_field,
'sort_attr_asset');
115 hidden_field($type_code.
'_asset_type_old', $selected_asset_type);
116 asset_type_chooser($type_code.
'_asset_type', FALSE, Array($selected_asset_type), TRUE);
118 $o->openField(translate(
'tool_asset_sorter_sort_attribute'),
'',
'', $sort_by_field,
'sort_attr_attr');
119 if ($selected_asset_type ==
'') {
120 echo
'<b>['.translate(
'asset_type_not_selected').
']</b>';
122 $attrs = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($selected_asset_type);
125 foreach ($attrs as $attr_name => $val) {
126 $options[$attr_name] = $attr_name;
130 combo_box($type_code.
'_asset_attribute', $options, FALSE, $selected_asset_attribute);
133 $o->openField(translate(
'tool_asset_sorter_sort_direction'));
134 combo_box($type_code.
'_direction', Array(
'ASC' =>
'Ascending',
'DESC' =>
'Descending'), FALSE, $sort_direction);
136 $o->openField(translate(
'note'));
137 $o->note(translate(
'tool_asset_sorter_usage_notes'));
140 $o->addJsInclude(sq_web_path(
'lib').
'/js/tool_asset_sorter.js');
159 $parentid = $_REQUEST[$type_code.
'_parent'][
'assetid'];
160 if (empty($parentid))
return FALSE;
161 $parent = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($parentid);
162 if (is_null($parent))
return FALSE;
164 $sort_type = $_POST[$type_code.
'_sort_type'];
166 $sort_field = $_REQUEST[$type_code.
'_field'];
167 $sortable_fields = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfoFields();
170 $sort_asset_type = $_POST[$type_code.
'_asset_type'];
171 $old_sort_asset_type = $_POST[$type_code.
'_asset_type_old'];
172 if (($sort_type ==
'sort_by_attribute') && ($sort_asset_type != $old_sort_asset_type)) {
176 $sort_asset_attribute =
'';
177 if (($sort_type ==
'sort_by_attribute') && empty($sort_asset_type)) {
181 if (isset($_POST[$type_code.
'_asset_attribute'])) {
182 $sort_asset_attribute = $_POST[$type_code.
'_asset_attribute'];
186 $direction = $_REQUEST[$type_code.
'_direction'];
187 $direction = (strtolower($direction) ==
'asc') ?
'ASC' :
'DESC';
190 if(!empty($sort_field) && !preg_match(
'/^[a-z_]+$/i', $sort_field))
192 if(!empty($sort_asset_type) && !preg_match(
'/^[a-z_]+$/i', $sort_asset_type))
196 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
205 if ($sort_type ==
'sort_by_attribute') {
207 $sql =
'SELECT l.linkid
208 FROM sq_ast a, sq_ast_attr t, sq_ast_attr_val v, sq_ast_lnk l
209 WHERE majorid = :parentid
210 AND link_type IN (:link_type_1, :link_type_2)
211 AND a.assetid = l.minorid
212 AND a.type_code = :sort_asset_type
213 AND t.name = :sort_asset_attr
214 AND a.assetid = v.assetid
215 AND t.attrid = v.attrid ';
219 $sql .=
'ORDER BY TO_CHAR(v.custom_val) '.$direction;
221 $sql .=
'ORDER BY v.custom_val '.$direction;
232 }
catch (Exception $e) {
233 throw new Exception(
'Unable to get link ids for parent: '.$parentid.
' due to database error: '.$e->getMessage());
237 Tool_Asset_Sorter::_chunkQuery($linkids, $chunk_size);
238 $index_delta = count($linkids);
241 $sql =
'SELECT l.linkid
243 JOIN sq_ast a ON l.minorid = a.assetid
244 WHERE majorid = :parentid
245 AND link_type IN (:link_type_1, :link_type_2)
246 AND a.type_code != '.MatrixDAL::quote($sort_asset_type).
'
247 ORDER BY l.sort_order';
250 $sql =
'SELECT l.linkid
252 JOIN sq_ast a ON l.minorid = a.assetid
253 WHERE majorid = :parentid
254 AND link_type IN (:link_type_1, :link_type_2)
255 ORDER BY a.'.$sort_field.
' '.$direction.
', a.assetid DESC';
264 }
catch (Exception $e) {
265 throw new Exception(
'Unable to get link ids for parent: '.$parentid.
' due to database error: '.$e->getMessage());
270 Tool_Asset_Sorter::_chunkQuery($linkids, $chunk_size, $index_delta);
273 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
275 $em = $GLOBALS[
'SQ_SYSTEM']->getEventManager();
276 $em->broadcastEvent($parent,
'AssetUpdate', Array());
278 $o->openSection(
'Asset Sorting Complete');
280 if ($sort_type ==
'sort_by_attribute') {
281 $asset_types = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypes();
282 $asset_type_name = $asset_types[$sort_asset_type][
'name'];
283 echo $asset_type_name.
' child assets of '.get_asset_tag_line($parentid).
' have been sorted by '.$sort_asset_attribute;
285 echo
'The children of '.get_asset_tag_line($parentid).
' have been sorted by '.$sortable_fields[$sort_field];
306 private static function _chunkQuery($linkids, $chunk_size, $index_delta = 0)
308 foreach (array_chunk($linkids, $chunk_size, TRUE) as $chunk) {
310 foreach ($chunk as $i => $linkid) {
311 $cases .=
'WHEN (linkid = '.$linkid.
') THEN '.($i+$index_delta).
' ';
313 $sql =
'UPDATE sq_ast_lnk
314 SET sort_order = CASE '.$cases.
' ELSE sort_order END
315 WHERE linkid IN ('.implode(
', ', $chunk).
')';
318 }
catch (Exception $e) {
319 throw new Exception(
'Unable to update link table with new sort order due to database error: '.$e->getMessage());