1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13 use Joomla\String\StringHelper;
14 use Joomla\Utilities\ArrayHelper;
15
16 17 18 19 20
21 abstract class JModelAdmin extends JModelForm
22 {
23 24 25 26 27 28
29 protected $text_prefix = null;
30
31 32 33 34 35 36
37 protected $event_after_delete = null;
38
39 40 41 42 43 44
45 protected $event_after_save = null;
46
47 48 49 50 51 52
53 protected $event_before_delete = null;
54
55 56 57 58 59 60
61 protected $event_before_save = null;
62
63 64 65 66 67 68
69 protected $event_change_state = null;
70
71 72 73 74 75 76 77
78 protected $batch_copymove = 'category_id';
79
80 81 82 83 84 85
86 protected $batch_commands = array(
87 'assetgroup_id' => 'batchAccess',
88 'language_id' => 'batchLanguage',
89 'tag' => 'batchTag',
90 );
91
92 93 94 95 96 97
98 protected $associationsContext = null;
99
100 101 102 103 104 105 106 107
108 public function __construct($config = array())
109 {
110 parent::__construct($config);
111
112 if (isset($config['event_after_delete']))
113 {
114 $this->event_after_delete = $config['event_after_delete'];
115 }
116 elseif (empty($this->event_after_delete))
117 {
118 $this->event_after_delete = 'onContentAfterDelete';
119 }
120
121 if (isset($config['event_after_save']))
122 {
123 $this->event_after_save = $config['event_after_save'];
124 }
125 elseif (empty($this->event_after_save))
126 {
127 $this->event_after_save = 'onContentAfterSave';
128 }
129
130 if (isset($config['event_before_delete']))
131 {
132 $this->event_before_delete = $config['event_before_delete'];
133 }
134 elseif (empty($this->event_before_delete))
135 {
136 $this->event_before_delete = 'onContentBeforeDelete';
137 }
138
139 if (isset($config['event_before_save']))
140 {
141 $this->event_before_save = $config['event_before_save'];
142 }
143 elseif (empty($this->event_before_save))
144 {
145 $this->event_before_save = 'onContentBeforeSave';
146 }
147
148 if (isset($config['event_change_state']))
149 {
150 $this->event_change_state = $config['event_change_state'];
151 }
152 elseif (empty($this->event_change_state))
153 {
154 $this->event_change_state = 'onContentChangeState';
155 }
156
157 $config['events_map'] = isset($config['events_map']) ? $config['events_map'] : array();
158
159 $this->events_map = array_merge(
160 array(
161 'delete' => 'content',
162 'save' => 'content',
163 'change_state' => 'content',
164 'validate' => 'content',
165 ), $config['events_map']
166 );
167
168
169 if (isset($config['text_prefix']))
170 {
171 $this->text_prefix = strtoupper($config['text_prefix']);
172 }
173 elseif (empty($this->text_prefix))
174 {
175 $this->text_prefix = strtoupper($this->option);
176 }
177 }
178
179 180 181 182 183 184 185 186 187 188 189
190 public function batch($commands, $pks, $contexts)
191 {
192
193 $pks = array_unique($pks);
194 $pks = ArrayHelper::toInteger($pks);
195
196
197 if (array_search(0, $pks, true))
198 {
199 unset($pks[array_search(0, $pks, true)]);
200 }
201
202 if (empty($pks))
203 {
204 $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
205
206 return false;
207 }
208
209 $done = false;
210
211
212 $this->user = JFactory::getUser();
213 $this->table = $this->getTable();
214 $this->tableClassName = get_class($this->table);
215 $this->contentType = new JUcmType;
216 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
217 $this->batchSet = true;
218
219 if ($this->type == false)
220 {
221 $type = new JUcmType;
222 $this->type = $type->getTypeByAlias($this->typeAlias);
223 }
224
225 $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags');
226
227 if ($this->batch_copymove && !empty($commands[$this->batch_copymove]))
228 {
229 $cmd = ArrayHelper::getValue($commands, 'move_copy', 'c');
230
231 if ($cmd == 'c')
232 {
233 $result = $this->batchCopy($commands[$this->batch_copymove], $pks, $contexts);
234
235 if (is_array($result))
236 {
237 foreach ($result as $old => $new)
238 {
239 $contexts[$new] = $contexts[$old];
240 }
241 $pks = array_values($result);
242 }
243 else
244 {
245 return false;
246 }
247 }
248 elseif ($cmd == 'm' && !$this->batchMove($commands[$this->batch_copymove], $pks, $contexts))
249 {
250 return false;
251 }
252
253 $done = true;
254 }
255
256 foreach ($this->batch_commands as $identifier => $command)
257 {
258 if (!empty($commands[$identifier]))
259 {
260 if (!$this->$command($commands[$identifier], $pks, $contexts))
261 {
262 return false;
263 }
264
265 $done = true;
266 }
267 }
268
269 if (!$done)
270 {
271 $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
272
273 return false;
274 }
275
276
277 $this->cleanCache();
278
279 return true;
280 }
281
282 283 284 285 286 287 288 289 290 291 292
293 protected function batchAccess($value, $pks, $contexts)
294 {
295 if (empty($this->batchSet))
296 {
297
298 $this->user = JFactory::getUser();
299 $this->table = $this->getTable();
300 $this->tableClassName = get_class($this->table);
301 $this->contentType = new JUcmType;
302 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
303 }
304
305 foreach ($pks as $pk)
306 {
307 if ($this->user->authorise('core.edit', $contexts[$pk]))
308 {
309 $this->table->reset();
310 $this->table->load($pk);
311 $this->table->access = (int) $value;
312
313 if (!empty($this->type))
314 {
315 $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
316 }
317
318 if (!$this->table->store())
319 {
320 $this->setError($this->table->getError());
321
322 return false;
323 }
324 }
325 else
326 {
327 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
328
329 return false;
330 }
331 }
332
333
334 $this->cleanCache();
335
336 return true;
337 }
338
339 340 341 342 343 344 345 346 347 348 349
350 protected function batchCopy($value, $pks, $contexts)
351 {
352 if (empty($this->batchSet))
353 {
354
355 $this->user = JFactory::getUser();
356 $this->table = $this->getTable();
357 $this->tableClassName = get_class($this->table);
358 $this->contentType = new JUcmType;
359 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
360 }
361
362 $categoryId = $value;
363
364 if (!$this->checkCategoryId($categoryId))
365 {
366 return false;
367 }
368
369 $newIds = array();
370
371
372 while (!empty($pks))
373 {
374
375 $pk = array_shift($pks);
376
377 $this->table->reset();
378
379
380 if (!$this->table->load($pk))
381 {
382 if ($error = $this->table->getError())
383 {
384
385 $this->setError($error);
386
387 return false;
388 }
389 else
390 {
391
392 $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
393 continue;
394 }
395 }
396
397 $this->generateTitle($categoryId, $this->table);
398
399
400 $this->table->id = 0;
401
402
403 if (isset($this->table->published))
404 {
405 $this->table->published = 0;
406 }
407 elseif (isset($this->table->state))
408 {
409 $this->table->state = 0;
410 }
411
412 $hitsAlias = $this->table->getColumnAlias('hits');
413
414 if (isset($this->table->$hitsAlias))
415 {
416 $this->table->$hitsAlias = 0;
417 }
418
419
420 $this->table->catid = $categoryId;
421
422
423
424
425
426 if (!$this->table->check())
427 {
428 $this->setError($this->table->getError());
429
430 return false;
431 }
432
433 if (!empty($this->type))
434 {
435 $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
436 }
437
438
439 if (!$this->table->store())
440 {
441 $this->setError($this->table->getError());
442
443 return false;
444 }
445
446
447 $newId = $this->table->get('id');
448
449
450 $newIds[$pk] = $newId;
451 }
452
453
454 $this->cleanCache();
455
456 return $newIds;
457 }
458
459 460 461 462 463 464 465 466 467 468 469
470 protected function batchLanguage($value, $pks, $contexts)
471 {
472 if (empty($this->batchSet))
473 {
474
475 $this->user = JFactory::getUser();
476 $this->table = $this->getTable();
477 $this->tableClassName = get_class($this->table);
478 $this->contentType = new JUcmType;
479 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
480 }
481
482 foreach ($pks as $pk)
483 {
484 if ($this->user->authorise('core.edit', $contexts[$pk]))
485 {
486 $this->table->reset();
487 $this->table->load($pk);
488 $this->table->language = $value;
489
490 if (!empty($this->type))
491 {
492 $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
493 }
494
495 if (!$this->table->store())
496 {
497 $this->setError($this->table->getError());
498
499 return false;
500 }
501 }
502 else
503 {
504 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
505
506 return false;
507 }
508 }
509
510
511 $this->cleanCache();
512
513 return true;
514 }
515
516 517 518 519 520 521 522 523 524 525 526
527 protected function batchMove($value, $pks, $contexts)
528 {
529 if (empty($this->batchSet))
530 {
531
532 $this->user = JFactory::getUser();
533 $this->table = $this->getTable();
534 $this->tableClassName = get_class($this->table);
535 $this->contentType = new JUcmType;
536 $this->type = $this->contentType->getTypeByTable($this->tableClassName);
537 }
538
539 $categoryId = (int) $value;
540
541 if (!$this->checkCategoryId($categoryId))
542 {
543 return false;
544 }
545
546
547 foreach ($pks as $pk)
548 {
549 if (!$this->user->authorise('core.edit', $contexts[$pk]))
550 {
551 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
552
553 return false;
554 }
555
556
557 if (!$this->table->load($pk))
558 {
559 if ($error = $this->table->getError())
560 {
561
562 $this->setError($error);
563
564 return false;
565 }
566 else
567 {
568
569 $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
570 continue;
571 }
572 }
573
574
575 $this->table->catid = $categoryId;
576
577
578 if (!$this->table->check())
579 {
580 $this->setError($this->table->getError());
581
582 return false;
583 }
584
585 if (!empty($this->type))
586 {
587 $this->createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
588 }
589
590
591 if (!$this->table->store())
592 {
593 $this->setError($this->table->getError());
594
595 return false;
596 }
597 }
598
599
600 $this->cleanCache();
601
602 return true;
603 }
604
605 606 607 608 609 610 611 612 613 614 615
616 protected function batchTag($value, $pks, $contexts)
617 {
618
619 $user = JFactory::getUser();
620 $table = $this->getTable();
621
622 foreach ($pks as $pk)
623 {
624 if ($user->authorise('core.edit', $contexts[$pk]))
625 {
626 $table->reset();
627 $table->load($pk);
628 $tags = array($value);
629
630 631 632
633 $tagsObserver = $table->getObserverOfClass('JTableObserverTags');
634 $result = $tagsObserver->setNewTags($tags, false);
635
636 if (!$result)
637 {
638 $this->setError($table->getError());
639
640 return false;
641 }
642 }
643 else
644 {
645 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
646
647 return false;
648 }
649 }
650
651
652 $this->cleanCache();
653
654 return true;
655 }
656
657 658 659 660 661 662 663 664 665
666 protected function canDelete($record)
667 {
668 return JFactory::getUser()->authorise('core.delete', $this->option);
669 }
670
671 672 673 674 675 676 677 678 679
680 protected function canEditState($record)
681 {
682 return JFactory::getUser()->authorise('core.edit.state', $this->option);
683 }
684
685 686 687 688 689 690 691 692 693
694 public function checkin($pks = array())
695 {
696 $pks = (array) $pks;
697 $table = $this->getTable();
698 $count = 0;
699
700 if (empty($pks))
701 {
702 $pks = array((int) $this->getState($this->getName() . '.id'));
703 }
704
705 $checkedOutField = $table->getColumnAlias('checked_out');
706
707
708 foreach ($pks as $pk)
709 {
710 if ($table->load($pk))
711 {
712 if ($table->{$checkedOutField} > 0)
713 {
714 if (!parent::checkin($pk))
715 {
716 return false;
717 }
718
719 $count++;
720 }
721 }
722 else
723 {
724 $this->setError($table->getError());
725
726 return false;
727 }
728 }
729
730 return $count;
731 }
732
733 734 735 736 737 738 739 740 741
742 public function checkout($pk = null)
743 {
744 $pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id');
745
746 return parent::checkout($pk);
747 }
748
749 750 751 752 753 754 755 756 757
758 public function delete(&$pks)
759 {
760 $dispatcher = JEventDispatcher::getInstance();
761 $pks = (array) $pks;
762 $table = $this->getTable();
763
764
765 JPluginHelper::importPlugin($this->events_map['delete']);
766
767
768 foreach ($pks as $i => $pk)
769 {
770 if ($table->load($pk))
771 {
772 if ($this->canDelete($table))
773 {
774 $context = $this->option . '.' . $this->name;
775
776
777 $result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
778
779 if (in_array(false, $result, true))
780 {
781 $this->setError($table->getError());
782
783 return false;
784 }
785
786
787 if ($this->associationsContext && JLanguageAssociations::isEnabled())
788 {
789 $db = $this->getDbo();
790 $query = $db->getQuery(true)
791 ->select('COUNT(*) as count, ' . $db->quoteName('as1.key'))
792 ->from($db->quoteName('#__associations') . ' AS as1')
793 ->join('LEFT', $db->quoteName('#__associations') . ' AS as2 ON ' . $db->quoteName('as1.key') . ' = ' . $db->quoteName('as2.key'))
794 ->where($db->quoteName('as1.context') . ' = ' . $db->quote($this->associationsContext))
795 ->where($db->quoteName('as1.id') . ' = ' . (int) $pk)
796 ->group($db->quoteName('as1.key'));
797
798 $db->setQuery($query);
799 $row = $db->loadAssoc();
800
801 if (!empty($row['count']))
802 {
803 $query = $db->getQuery(true)
804 ->delete($db->quoteName('#__associations'))
805 ->where($db->quoteName('context') . ' = ' . $db->quote($this->associationsContext))
806 ->where($db->quoteName('key') . ' = ' . $db->quote($row['key']));
807
808 if ($row['count'] > 2)
809 {
810 $query->where($db->quoteName('id') . ' = ' . (int) $pk);
811 }
812
813 $db->setQuery($query);
814 $db->execute();
815 }
816 }
817
818 if (!$table->delete($pk))
819 {
820 $this->setError($table->getError());
821
822 return false;
823 }
824
825
826 $dispatcher->trigger($this->event_after_delete, array($context, $table));
827 }
828 else
829 {
830
831 unset($pks[$i]);
832 $error = $this->getError();
833
834 if ($error)
835 {
836 JLog::add($error, JLog::WARNING, 'jerror');
837
838 return false;
839 }
840 else
841 {
842 JLog::add(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
843
844 return false;
845 }
846 }
847 }
848 else
849 {
850 $this->setError($table->getError());
851
852 return false;
853 }
854 }
855
856
857 $this->cleanCache();
858
859 return true;
860 }
861
862 863 864 865 866 867 868 869 870 871 872
873 protected function generateNewTitle($category_id, $alias, $title)
874 {
875
876 $table = $this->getTable();
877
878 while ($table->load(array('alias' => $alias, 'catid' => $category_id)))
879 {
880 $title = StringHelper::increment($title);
881 $alias = StringHelper::increment($alias, 'dash');
882 }
883
884 return array($title, $alias);
885 }
886
887 888 889 890 891 892 893 894 895
896 public function getItem($pk = null)
897 {
898 $pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id');
899 $table = $this->getTable();
900
901 if ($pk > 0)
902 {
903
904 $return = $table->load($pk);
905
906
907 if ($return === false && $table->getError())
908 {
909 $this->setError($table->getError());
910
911 return false;
912 }
913 }
914
915
916 $properties = $table->getProperties(1);
917 $item = ArrayHelper::toObject($properties, 'JObject');
918
919 if (property_exists($item, 'params'))
920 {
921 $registry = new Registry($item->params);
922 $item->params = $registry->toArray();
923 }
924
925 return $item;
926 }
927
928 929 930 931 932 933 934 935 936
937 protected function getReorderConditions($table)
938 {
939 return array();
940 }
941
942 943 944 945 946 947 948
949 protected function populateState()
950 {
951 $table = $this->getTable();
952 $key = $table->getKeyName();
953
954
955 $pk = JFactory::getApplication()->input->getInt($key);
956 $this->setState($this->getName() . '.id', $pk);
957
958
959 $value = JComponentHelper::getParams($this->option);
960 $this->setState('params', $value);
961 }
962
963 964 965 966 967 968 969 970 971
972 protected function prepareTable($table)
973 {
974
975 }
976
977 978 979 980 981 982 983 984 985 986
987 public function publish(&$pks, $value = 1)
988 {
989 $dispatcher = JEventDispatcher::getInstance();
990 $user = JFactory::getUser();
991 $table = $this->getTable();
992 $pks = (array) $pks;
993
994
995 JPluginHelper::importPlugin($this->events_map['change_state']);
996
997
998 foreach ($pks as $i => $pk)
999 {
1000 $table->reset();
1001
1002 if ($table->load($pk))
1003 {
1004 if (!$this->canEditState($table))
1005 {
1006
1007 unset($pks[$i]);
1008
1009 JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
1010
1011 return false;
1012 }
1013
1014
1015 if (property_exists($table, 'checked_out') && $table->checked_out && ($table->checked_out != $user->id))
1016 {
1017 JLog::add(JText::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), JLog::WARNING, 'jerror');
1018
1019
1020 unset($pks[$i]);
1021
1022 return false;
1023 }
1024 }
1025 }
1026
1027
1028 if (!$table->publish($pks, $value, $user->get('id')))
1029 {
1030 $this->setError($table->getError());
1031
1032 return false;
1033 }
1034
1035 $context = $this->option . '.' . $this->name;
1036
1037
1038 $result = $dispatcher->trigger($this->event_change_state, array($context, $pks, $value));
1039
1040 if (in_array(false, $result, true))
1041 {
1042 $this->setError($table->getError());
1043
1044 return false;
1045 }
1046
1047
1048 $this->cleanCache();
1049
1050 return true;
1051 }
1052
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
1066 public function reorder($pks, $delta = 0)
1067 {
1068 $table = $this->getTable();
1069 $pks = (array) $pks;
1070 $result = true;
1071
1072 $allowed = true;
1073
1074 foreach ($pks as $i => $pk)
1075 {
1076 $table->reset();
1077
1078 if ($table->load($pk) && $this->checkout($pk))
1079 {
1080
1081 if (!$this->canEditState($table))
1082 {
1083
1084 unset($pks[$i]);
1085 $this->checkin($pk);
1086 JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
1087 $allowed = false;
1088 continue;
1089 }
1090
1091 $where = $this->getReorderConditions($table);
1092
1093 if (!$table->move($delta, $where))
1094 {
1095 $this->setError($table->getError());
1096 unset($pks[$i]);
1097 $result = false;
1098 }
1099
1100 $this->checkin($pk);
1101 }
1102 else
1103 {
1104 $this->setError($table->getError());
1105 unset($pks[$i]);
1106 $result = false;
1107 }
1108 }
1109
1110 if ($allowed === false && empty($pks))
1111 {
1112 $result = null;
1113 }
1114
1115
1116 if ($result == true)
1117 {
1118 $this->cleanCache();
1119 }
1120
1121 return $result;
1122 }
1123
1124 1125 1126 1127 1128 1129 1130 1131 1132
1133 public function save($data)
1134 {
1135 $dispatcher = JEventDispatcher::getInstance();
1136 $table = $this->getTable();
1137 $context = $this->option . '.' . $this->name;
1138
1139 if (!empty($data['tags']) && $data['tags'][0] != '')
1140 {
1141 $table->newTags = $data['tags'];
1142 }
1143
1144 $key = $table->getKeyName();
1145 $pk = (!empty($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
1146 $isNew = true;
1147
1148
1149 JPluginHelper::importPlugin($this->events_map['save']);
1150
1151
1152 try
1153 {
1154
1155 if ($pk > 0)
1156 {
1157 $table->load($pk);
1158 $isNew = false;
1159 }
1160
1161
1162 if (!$table->bind($data))
1163 {
1164 $this->setError($table->getError());
1165
1166 return false;
1167 }
1168
1169
1170 $this->prepareTable($table);
1171
1172
1173 if (!$table->check())
1174 {
1175 $this->setError($table->getError());
1176
1177 return false;
1178 }
1179
1180
1181 $result = $dispatcher->trigger($this->event_before_save, array($context, $table, $isNew, $data));
1182
1183 if (in_array(false, $result, true))
1184 {
1185 $this->setError($table->getError());
1186
1187 return false;
1188 }
1189
1190
1191 if (!$table->store())
1192 {
1193 $this->setError($table->getError());
1194
1195 return false;
1196 }
1197
1198
1199 $this->cleanCache();
1200
1201
1202 $dispatcher->trigger($this->event_after_save, array($context, $table, $isNew, $data));
1203 }
1204 catch (Exception $e)
1205 {
1206 $this->setError($e->getMessage());
1207
1208 return false;
1209 }
1210
1211 if (isset($table->$key))
1212 {
1213 $this->setState($this->getName() . '.id', $table->$key);
1214 }
1215
1216 $this->setState($this->getName() . '.new', $isNew);
1217
1218 if ($this->associationsContext && JLanguageAssociations::isEnabled() && !empty($data['associations']))
1219 {
1220 $associations = $data['associations'];
1221
1222
1223 $associations = ArrayHelper::toInteger($associations);
1224
1225
1226 foreach ($associations as $tag => $id)
1227 {
1228 if (!$id)
1229 {
1230 unset($associations[$tag]);
1231 }
1232 }
1233
1234
1235 if ($associations && ($table->language == '*'))
1236 {
1237 JFactory::getApplication()->enqueueMessage(
1238 JText::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'),
1239 'warning'
1240 );
1241 }
1242
1243
1244 $db = $this->getDbo();
1245 $query = $db->getQuery(true)
1246 ->select($db->qn('key'))
1247 ->from($db->qn('#__associations'))
1248 ->where($db->qn('context') . ' = ' . $db->quote($this->associationsContext))
1249 ->where($db->qn('id') . ' = ' . (int) $table->$key);
1250 $db->setQuery($query);
1251 $old_key = $db->loadResult();
1252
1253
1254 $query = $db->getQuery(true)
1255 ->delete($db->qn('#__associations'))
1256 ->where($db->qn('context') . ' = ' . $db->quote($this->associationsContext));
1257
1258 if ($associations)
1259 {
1260 $query->where('(' . $db->qn('id') . ' IN (' . implode(',', $associations) . ') OR '
1261 . $db->qn('key') . ' = ' . $db->q($old_key) . ')');
1262 }
1263 else
1264 {
1265 $query->where($db->qn('key') . ' = ' . $db->q($old_key));
1266 }
1267
1268 $db->setQuery($query);
1269 $db->execute();
1270
1271
1272 if ($table->language != '*')
1273 {
1274 $associations[$table->language] = (int) $table->$key;
1275 }
1276
1277 if (count($associations) > 1)
1278 {
1279
1280 $key = md5(json_encode($associations));
1281 $query = $db->getQuery(true)
1282 ->insert('#__associations');
1283
1284 foreach ($associations as $id)
1285 {
1286 $query->values(((int) $id) . ',' . $db->quote($this->associationsContext) . ',' . $db->quote($key));
1287 }
1288
1289 $db->setQuery($query);
1290 $db->execute();
1291 }
1292 }
1293
1294 return true;
1295 }
1296
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
1307 public function saveorder($pks = array(), $order = null)
1308 {
1309 $table = $this->getTable();
1310 $tableClassName = get_class($table);
1311 $contentType = new JUcmType;
1312 $type = $contentType->getTypeByTable($tableClassName);
1313 $tagsObserver = $table->getObserverOfClass('JTableObserverTags');
1314 $conditions = array();
1315
1316 if (empty($pks))
1317 {
1318 return JError::raiseWarning(500, JText::_($this->text_prefix . '_ERROR_NO_ITEMS_SELECTED'));
1319 }
1320
1321 $orderingField = $table->getColumnAlias('ordering');
1322
1323
1324 foreach ($pks as $i => $pk)
1325 {
1326 $table->load((int) $pk);
1327
1328
1329 if (!$this->canEditState($table))
1330 {
1331
1332 unset($pks[$i]);
1333 JLog::add(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), JLog::WARNING, 'jerror');
1334 }
1335 elseif ($table->$orderingField != $order[$i])
1336 {
1337 $table->$orderingField = $order[$i];
1338
1339 if ($type)
1340 {
1341 $this->createTagsHelper($tagsObserver, $type, $pk, $type->type_alias, $table);
1342 }
1343
1344 if (!$table->store())
1345 {
1346 $this->setError($table->getError());
1347
1348 return false;
1349 }
1350
1351
1352 $condition = $this->getReorderConditions($table);
1353 $found = false;
1354
1355 foreach ($conditions as $cond)
1356 {
1357 if ($cond[1] == $condition)
1358 {
1359 $found = true;
1360 break;
1361 }
1362 }
1363
1364 if (!$found)
1365 {
1366 $key = $table->getKeyName();
1367 $conditions[] = array($table->$key, $condition);
1368 }
1369 }
1370 }
1371
1372
1373 foreach ($conditions as $cond)
1374 {
1375 $table->load($cond[0]);
1376 $table->reorder($cond[1]);
1377 }
1378
1379
1380 $this->cleanCache();
1381
1382 return true;
1383 }
1384
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
1398 public function createTagsHelper($tagsObserver, $type, $pk, $typeAlias, $table)
1399 {
1400 if (!empty($tagsObserver) && !empty($type))
1401 {
1402 $table->tagsHelper = new JHelperTags;
1403 $table->tagsHelper->typeAlias = $typeAlias;
1404 $table->tagsHelper->tags = explode(',', $table->tagsHelper->getTagIds($pk, $typeAlias));
1405 }
1406 }
1407
1408 1409 1410 1411 1412 1413 1414 1415 1416
1417 protected function checkCategoryId($categoryId)
1418 {
1419
1420 if ($categoryId)
1421 {
1422 $categoryTable = JTable::getInstance('Category');
1423
1424 if (!$categoryTable->load($categoryId))
1425 {
1426 if ($error = $categoryTable->getError())
1427 {
1428
1429 $this->setError($error);
1430
1431 return false;
1432 }
1433 else
1434 {
1435 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
1436
1437 return false;
1438 }
1439 }
1440 }
1441
1442 if (empty($categoryId))
1443 {
1444 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
1445
1446 return false;
1447 }
1448
1449
1450 $extension = JFactory::getApplication()->input->get('option', '');
1451
1452 if (!$this->user->authorise('core.create', $extension . '.category.' . $categoryId))
1453 {
1454 $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
1455
1456 return false;
1457 }
1458
1459 return true;
1460 }
1461
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
1473 public function generateTitle($categoryId, $table)
1474 {
1475
1476 $data = $this->generateNewTitle($categoryId, $table->alias, $table->title);
1477 $table->title = $data['0'];
1478 $table->alias = $data['1'];
1479 }
1480 }
1481