/administrator/components/com_categories/models/fields/categoryparent.php

https://github.com/rvsjoen/joomla-cms · PHP · 168 lines · 98 code · 20 blank · 50 comment · 23 complexity · d24f34ac037fae0a0a38a6527e659d53 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_categories
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. JFormHelper::loadFieldClass('list');
  11. /**
  12. * Form Field class for the Joomla Framework.
  13. *
  14. * @package Joomla.Administrator
  15. * @subpackage com_categories
  16. * @since 1.6
  17. */
  18. class JFormFieldCategoryParent extends JFormFieldList
  19. {
  20. /**
  21. * The form field type.
  22. *
  23. * @var string
  24. * @since 1.6
  25. */
  26. protected $type = 'CategoryParent';
  27. /**
  28. * Method to get the field options.
  29. *
  30. * @return array The field option objects.
  31. * @since 1.6
  32. */
  33. protected function getOptions()
  34. {
  35. // Initialise variables.
  36. $options = array();
  37. $name = (string) $this->element['name'];
  38. // Let's get the id for the current item, either category or content item.
  39. $jinput = JFactory::getApplication()->input;
  40. // For categories the old category is the category id 0 for new category.
  41. if ($this->element['parent'])
  42. {
  43. $oldCat = $jinput->get('id',0);
  44. $oldParent = $this->form->getValue($name);
  45. }
  46. else
  47. // For items the old category is the category they are in when opened or 0 if new.
  48. {
  49. $thisItem = $jinput->get('id',0);
  50. $oldCat = $this->form->getValue($name);
  51. }
  52. $db = JFactory::getDbo();
  53. $query = $db->getQuery(true);
  54. $query->select('a.id AS value, a.title AS text, a.level');
  55. $query->from('#__categories AS a');
  56. $query->join('LEFT', $db->quoteName('#__categories').' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  57. // Filter by the type
  58. if ($extension = $this->form->getValue('extension')) {
  59. $query->where('(a.extension = '.$db->quote($extension).' OR a.parent_id = 0)');
  60. }
  61. if ($this->element['parent'])
  62. {
  63. // Prevent parenting to children of this item.
  64. if ($id = $this->form->getValue('id')) {
  65. $query->join('LEFT', $db->quoteName('#__categories').' AS p ON p.id = '.(int) $id);
  66. $query->where('NOT(a.lft >= p.lft AND a.rgt <= p.rgt)');
  67. $rowQuery = $db->getQuery(true);
  68. $rowQuery->select('a.id AS value, a.title AS text, a.level, a.parent_id');
  69. $rowQuery->from('#__categories AS a');
  70. $rowQuery->where('a.id = ' . (int) $id);
  71. $db->setQuery($rowQuery);
  72. $row = $db->loadObject();
  73. }
  74. }
  75. $query->where('a.published IN (0,1)');
  76. $query->group('a.id, a.title, a.level, a.lft, a.rgt, a.extension, a.parent_id');
  77. $query->order('a.lft ASC');
  78. // Get the options.
  79. $db->setQuery($query);
  80. $options = $db->loadObjectList();
  81. // Check for a database error.
  82. if ($db->getErrorNum()) {
  83. JError::raiseWarning(500, $db->getErrorMsg());
  84. }
  85. // Pad the option text with spaces using depth level as a multiplier.
  86. for ($i = 0, $n = count($options); $i < $n; $i++)
  87. {
  88. // Translate ROOT
  89. if ($options[$i]->level == 0) {
  90. $options[$i]->text = JText::_('JGLOBAL_ROOT_PARENT');
  91. }
  92. $options[$i]->text = str_repeat('- ', $options[$i]->level).$options[$i]->text;
  93. }
  94. // Initialise variables.
  95. // Get the current user object.
  96. $user = JFactory::getUser();
  97. // For new items we want a list of categories you are allowed to create in.
  98. if ($oldCat == 0)
  99. {
  100. foreach ($options as $i => $option)
  101. {
  102. // To take save or create in a category you need to have create rights for that category
  103. // unless the item is already in that category.
  104. // Unset the option if the user isn't authorised for it. In this field assets are always categories.
  105. if ($user->authorise('core.create', $extension . '.category.' . $option->value) != true )
  106. {
  107. unset($options[$i]);
  108. }
  109. }
  110. }
  111. // If you have an existing category id things are more complex.
  112. else
  113. {
  114. //$categoryOld = $this->form->getValue($name);
  115. foreach ($options as $i => $option)
  116. {
  117. // If you are only allowed to edit in this category but not edit.state, you should not get any
  118. // option to change the category parent for a category or the category for a content item,
  119. // but you should be able to save in that category.
  120. if ($user->authorise('core.edit.state', $extension . '.category.' . $oldCat) != true)
  121. {
  122. if ($option->value != $oldCat)
  123. {echo 'y';
  124. unset($options[$i]);
  125. }
  126. }
  127. // However, if you can edit.state you can also move this to another category for which you have
  128. // create permission and you should also still be able to save in the current category.
  129. elseif
  130. (($user->authorise('core.create', $extension . '.category.' . $option->value) != true)
  131. && $option->value != $oldCat)
  132. {echo 'x';
  133. unset($options[$i]);
  134. }
  135. }
  136. }
  137. if (isset($row) && !isset($options[0])) {
  138. if ($row->parent_id == '1') {
  139. $parent = new stdClass;
  140. $parent->text = JText::_('JGLOBAL_ROOT_PARENT');
  141. array_unshift($options, $parent);
  142. }
  143. }
  144. // Merge any additional options in the XML definition.
  145. $options = array_merge(parent::getOptions(), $options);
  146. return $options;
  147. }
  148. }