/administrator/components/com_content/src/Helper/ContentHelper.php

https://github.com/joomla/joomla-cms · PHP · 183 lines · 105 code · 36 blank · 42 comment · 12 complexity · 2bbaa4e5e90f6f2e0bf381da1f500ee6 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_content
  5. *
  6. * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. namespace Joomla\Component\Content\Administrator\Helper;
  10. use Joomla\CMS\Factory;
  11. use Joomla\CMS\Form\Form;
  12. use Joomla\CMS\Language\Text;
  13. use Joomla\CMS\Table\Category;
  14. use Joomla\CMS\Workflow\WorkflowServiceInterface;
  15. use Joomla\Database\ParameterType;
  16. use Joomla\Registry\Registry;
  17. /**
  18. * Content component helper.
  19. *
  20. * @since 1.6
  21. */
  22. class ContentHelper extends \Joomla\CMS\Helper\ContentHelper
  23. {
  24. /**
  25. * Check if state can be deleted
  26. *
  27. * @param int $id Id of state to delete
  28. *
  29. * @return boolean
  30. *
  31. * @since 4.0.0
  32. */
  33. public static function canDeleteState(int $id): bool
  34. {
  35. $db = Factory::getDbo();
  36. $query = $db->getQuery(true);
  37. $query->select('id')
  38. ->from($db->quoteName('#__content'))
  39. ->where($db->quoteName('state') . ' = :id')
  40. ->bind(':id', $id, ParameterType::INTEGER);
  41. $db->setQuery($query);
  42. $states = $db->loadResult();
  43. return empty($states);
  44. }
  45. /**
  46. * Method to filter transitions by given id of state
  47. *
  48. * @param array $transitions Array of transitions
  49. * @param int $pk Id of state
  50. * @param int $workflowId Id of the workflow
  51. *
  52. * @return array
  53. *
  54. * @since 4.0.0
  55. */
  56. public static function filterTransitions(array $transitions, int $pk, int $workflowId = 0): array
  57. {
  58. return array_values(
  59. array_filter(
  60. $transitions,
  61. function ($var) use ($pk, $workflowId) {
  62. return in_array($var['from_stage_id'], [-1, $pk]) && $workflowId == $var['workflow_id'];
  63. }
  64. )
  65. );
  66. }
  67. /**
  68. * Prepares a form
  69. *
  70. * @param Form $form The form to change
  71. * @param array|object $data The form data
  72. *
  73. * @return void
  74. */
  75. public static function onPrepareForm(Form $form, $data)
  76. {
  77. if ($form->getName() != 'com_categories.categorycom_content') {
  78. return;
  79. }
  80. $db = Factory::getDbo();
  81. $data = (array) $data;
  82. // Make workflows translatable
  83. Factory::getLanguage()->load('com_workflow', JPATH_ADMINISTRATOR);
  84. $form->setFieldAttribute('workflow_id', 'default', 'inherit');
  85. $component = Factory::getApplication()->bootComponent('com_content');
  86. if (
  87. !$component instanceof WorkflowServiceInterface
  88. || !$component->isWorkflowActive('com_content.article')
  89. ) {
  90. $form->removeField('workflow_id', 'params');
  91. return;
  92. }
  93. $query = $db->getQuery(true);
  94. $query->select($db->quoteName('title'))
  95. ->from($db->quoteName('#__workflows'))
  96. ->where(
  97. [
  98. $db->quoteName('default') . ' = 1',
  99. $db->quoteName('published') . ' = 1',
  100. $db->quoteName('extension') . ' = ' . $db->quote('com_content.article'),
  101. ]
  102. );
  103. $defaulttitle = $db->setQuery($query)->loadResult();
  104. $option = Text::_('COM_WORKFLOW_INHERIT_WORKFLOW_NEW');
  105. if (!empty($data['id'])) {
  106. $category = new Category($db);
  107. $categories = $category->getPath((int) $data['id']);
  108. // Remove the current category, because we search for inheritance from parent.
  109. array_pop($categories);
  110. $option = Text::sprintf('COM_WORKFLOW_INHERIT_WORKFLOW', Text::_($defaulttitle));
  111. if (!empty($categories)) {
  112. $categories = array_reverse($categories);
  113. $query = $db->getQuery(true);
  114. $query->select($db->quoteName('title'))
  115. ->from($db->quoteName('#__workflows'))
  116. ->where(
  117. [
  118. $db->quoteName('id') . ' = :workflowId',
  119. $db->quoteName('published') . ' = 1',
  120. $db->quoteName('extension') . ' = ' . $db->quote('com_content.article'),
  121. ]
  122. )
  123. ->bind(':workflowId', $workflow_id, ParameterType::INTEGER);
  124. $db->setQuery($query);
  125. foreach ($categories as $cat) {
  126. $cat->params = new Registry($cat->params);
  127. $workflow_id = $cat->params->get('workflow_id');
  128. if ($workflow_id == 'inherit') {
  129. continue;
  130. } elseif ($workflow_id == 'use_default') {
  131. break;
  132. } elseif ($workflow_id = (int) $workflow_id) {
  133. $title = $db->loadResult();
  134. if (!is_null($title)) {
  135. $option = Text::sprintf('COM_WORKFLOW_INHERIT_WORKFLOW', Text::_($title));
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. $field = $form->getField('workflow_id', 'params');
  143. $field->addOption($option, ['value' => 'inherit']);
  144. $field->addOption(Text::sprintf('COM_WORKFLOW_USE_DEFAULT_WORKFLOW', Text::_($defaulttitle)), ['value' => 'use_default']);
  145. $field->addOption('- ' . Text::_('COM_CONTENT_WORKFLOWS') . ' -', ['disabled' => 'true']);
  146. }
  147. }