[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_associations 6 * 7 * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Associations\Administrator\Helper; 12 13 use Joomla\CMS\Association\AssociationExtensionInterface; 14 use Joomla\CMS\Association\AssociationServiceInterface; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Helper\ContentHelper; 17 use Joomla\CMS\Language\LanguageHelper; 18 use Joomla\CMS\Language\Text; 19 use Joomla\CMS\Layout\LayoutHelper; 20 use Joomla\CMS\Router\Route; 21 use Joomla\Database\ParameterType; 22 use Joomla\Registry\Registry; 23 24 // phpcs:disable PSR1.Files.SideEffects 25 \defined('_JEXEC') or die; 26 // phpcs:enable PSR1.Files.SideEffects 27 28 /** 29 * Associations component helper. 30 * 31 * @since 3.7.0 32 */ 33 class AssociationsHelper extends ContentHelper 34 { 35 /** 36 * Array of Registry objects of extensions 37 * 38 * @var array 39 * @since 3.7.0 40 */ 41 public static $extensionsSupport = null; 42 43 /** 44 * List of extensions name with support 45 * 46 * @var array 47 * @since 3.7.0 48 */ 49 public static $supportedExtensionsList = array(); 50 51 /** 52 * Get the associated items for an item 53 * 54 * @param string $extensionName The extension name with com_ 55 * @param string $typeName The item type 56 * @param int $itemId The id of item for which we need the associated items 57 * 58 * @return array 59 * 60 * @since 3.7.0 61 */ 62 public static function getAssociationList($extensionName, $typeName, $itemId) 63 { 64 if (!self::hasSupport($extensionName)) { 65 return array(); 66 } 67 68 // Get the extension specific helper method 69 $helper = self::getExtensionHelper($extensionName); 70 71 return $helper->getAssociationList($typeName, $itemId); 72 } 73 74 /** 75 * Get the the instance of the extension helper class 76 * 77 * @param string $extensionName The extension name with com_ 78 * 79 * @return \Joomla\CMS\Association\AssociationExtensionHelper|null 80 * 81 * @since 3.7.0 82 */ 83 public static function getExtensionHelper($extensionName) 84 { 85 if (!self::hasSupport($extensionName)) { 86 return null; 87 } 88 89 $support = self::$extensionsSupport[$extensionName]; 90 91 return $support->get('helper'); 92 } 93 94 /** 95 * Get item information 96 * 97 * @param string $extensionName The extension name with com_ 98 * @param string $typeName The item type 99 * @param int $itemId The id of item for which we need the associated items 100 * 101 * @return \Joomla\CMS\Table\Table|null 102 * 103 * @since 3.7.0 104 */ 105 public static function getItem($extensionName, $typeName, $itemId) 106 { 107 if (!self::hasSupport($extensionName)) { 108 return null; 109 } 110 111 // Get the extension specific helper method 112 $helper = self::getExtensionHelper($extensionName); 113 114 return $helper->getItem($typeName, $itemId); 115 } 116 117 /** 118 * Check if extension supports associations 119 * 120 * @param string $extensionName The extension name with com_ 121 * 122 * @return boolean 123 * 124 * @since 3.7.0 125 */ 126 public static function hasSupport($extensionName) 127 { 128 if (\is_null(self::$extensionsSupport)) { 129 self::getSupportedExtensions(); 130 } 131 132 return \in_array($extensionName, self::$supportedExtensionsList); 133 } 134 135 /** 136 * Loads the helper for the given class. 137 * 138 * @param string $extensionName The extension name with com_ 139 * 140 * @return AssociationExtensionInterface|null 141 * 142 * @since 4.0.0 143 */ 144 private static function loadHelper($extensionName) 145 { 146 $component = Factory::getApplication()->bootComponent($extensionName); 147 148 if ($component instanceof AssociationServiceInterface) { 149 return $component->getAssociationsExtension(); 150 } 151 152 // Check if associations helper exists 153 if (!file_exists(JPATH_ADMINISTRATOR . '/components/' . $extensionName . '/helpers/associations.php')) { 154 return null; 155 } 156 157 require_once JPATH_ADMINISTRATOR . '/components/' . $extensionName . '/helpers/associations.php'; 158 159 $componentAssociationsHelperClassName = self::getExtensionHelperClassName($extensionName); 160 161 if (!class_exists($componentAssociationsHelperClassName, false)) { 162 return null; 163 } 164 165 // Create an instance of the helper class 166 return new $componentAssociationsHelperClassName(); 167 } 168 169 /** 170 * Get the extension specific helper class name 171 * 172 * @param string $extensionName The extension name with com_ 173 * 174 * @return string 175 * 176 * @since 3.7.0 177 */ 178 private static function getExtensionHelperClassName($extensionName) 179 { 180 $realName = self::getExtensionRealName($extensionName); 181 182 return ucfirst($realName) . 'AssociationsHelper'; 183 } 184 185 /** 186 * Get the real extension name. This means without com_ 187 * 188 * @param string $extensionName The extension name with com_ 189 * 190 * @return string 191 * 192 * @since 3.7.0 193 */ 194 private static function getExtensionRealName($extensionName) 195 { 196 return strpos($extensionName, 'com_') === false ? $extensionName : substr($extensionName, 4); 197 } 198 199 /** 200 * Get the associated language edit links Html. 201 * 202 * @param string $extensionName Extension Name 203 * @param string $typeName ItemType 204 * @param integer $itemId Item id. 205 * @param string $itemLanguage Item language code. 206 * @param boolean $addLink True for adding edit links. False for just text. 207 * @param boolean $assocLanguages True for showing non associated content languages. False only languages with associations. 208 * 209 * @return string The language HTML 210 * 211 * @since 3.7.0 212 */ 213 public static function getAssociationHtmlList($extensionName, $typeName, $itemId, $itemLanguage, $addLink = true, $assocLanguages = true) 214 { 215 // Get the associations list for this item. 216 $items = self::getAssociationList($extensionName, $typeName, $itemId); 217 218 $titleFieldName = self::getTypeFieldName($extensionName, $typeName, 'title'); 219 220 // Get all content languages. 221 $languages = LanguageHelper::getContentLanguages(array(0, 1), false); 222 $content_languages = array_column($languages, 'lang_code'); 223 224 // Display warning if Content Language is trashed or deleted 225 foreach ($items as $item) { 226 if (!\in_array($item['language'], $content_languages)) { 227 Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_ASSOCIATIONS_CONTENTLANGUAGE_WARNING', $item['language']), 'warning'); 228 } 229 } 230 231 $canEditReference = self::allowEdit($extensionName, $typeName, $itemId); 232 $canCreate = self::allowAdd($extensionName, $typeName); 233 234 // Create associated items list. 235 foreach ($languages as $langCode => $language) { 236 // Don't do for the reference language. 237 if ($langCode == $itemLanguage) { 238 continue; 239 } 240 241 // Don't show languages with associations, if we don't want to show them. 242 if ($assocLanguages && isset($items[$langCode])) { 243 unset($items[$langCode]); 244 continue; 245 } 246 247 // Don't show languages without associations, if we don't want to show them. 248 if (!$assocLanguages && !isset($items[$langCode])) { 249 continue; 250 } 251 252 // Get html parameters. 253 if (isset($items[$langCode])) { 254 $title = $items[$langCode][$titleFieldName]; 255 $additional = ''; 256 257 if (isset($items[$langCode]['catid'])) { 258 $db = Factory::getDbo(); 259 260 // Get the category name 261 $query = $db->getQuery(true) 262 ->select($db->quoteName('title')) 263 ->from($db->quoteName('#__categories')) 264 ->where($db->quoteName('id') . ' = :id') 265 ->bind(':id', $items[$langCode]['catid'], ParameterType::INTEGER); 266 267 $db->setQuery($query); 268 $categoryTitle = $db->loadResult(); 269 270 $additional = '<strong>' . Text::sprintf('JCATEGORY_SPRINTF', $categoryTitle) . '</strong> <br>'; 271 } elseif (isset($items[$langCode]['menutype'])) { 272 $db = Factory::getDbo(); 273 274 // Get the menutype name 275 $query = $db->getQuery(true) 276 ->select($db->quoteName('title')) 277 ->from($db->quoteName('#__menu_types')) 278 ->where($db->quoteName('menutype') . ' = :menutype') 279 ->bind(':menutype', $items[$langCode]['menutype']); 280 281 $db->setQuery($query); 282 $menutypeTitle = $db->loadResult(); 283 284 $additional = '<strong>' . Text::sprintf('COM_MENUS_MENU_SPRINTF', $menutypeTitle) . '</strong><br>'; 285 } 286 287 $labelClass = 'bg-secondary'; 288 $target = $langCode . ':' . $items[$langCode]['id'] . ':edit'; 289 $allow = $canEditReference 290 && self::allowEdit($extensionName, $typeName, $items[$langCode]['id']) 291 && self::canCheckinItem($extensionName, $typeName, $items[$langCode]['id']); 292 293 $additional .= $addLink && $allow ? Text::_('COM_ASSOCIATIONS_EDIT_ASSOCIATION') : ''; 294 } else { 295 $items[$langCode] = array(); 296 297 $title = Text::_('COM_ASSOCIATIONS_NO_ASSOCIATION'); 298 $additional = $addLink ? Text::_('COM_ASSOCIATIONS_ADD_NEW_ASSOCIATION') : ''; 299 $labelClass = 'bg-warning text-dark'; 300 $target = $langCode . ':0:add'; 301 $allow = $canCreate; 302 } 303 304 // Generate item Html. 305 $options = array( 306 'option' => 'com_associations', 307 'view' => 'association', 308 'layout' => 'edit', 309 'itemtype' => $extensionName . '.' . $typeName, 310 'task' => 'association.edit', 311 'id' => $itemId, 312 'target' => $target, 313 ); 314 315 $url = Route::_('index.php?' . http_build_query($options)); 316 $url = $allow && $addLink ? $url : ''; 317 $text = $language->lang_code; 318 319 $tooltip = '<strong>' . htmlspecialchars($language->title, ENT_QUOTES, 'UTF-8') . '</strong><br>' 320 . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '<br><br>' . $additional; 321 $classes = 'badge ' . $labelClass; 322 323 $items[$langCode]['link'] = '<a href="' . $url . '" class="' . $classes . '">' . $text . '</a>' 324 . '<div role="tooltip">' . $tooltip . '</div>'; 325 } 326 327 return LayoutHelper::render('joomla.content.associations', $items); 328 } 329 330 /** 331 * Get all extensions with associations support. 332 * 333 * @return array The extensions. 334 * 335 * @since 3.7.0 336 */ 337 public static function getSupportedExtensions() 338 { 339 if (!\is_null(self::$extensionsSupport)) { 340 return self::$extensionsSupport; 341 } 342 343 self::$extensionsSupport = array(); 344 345 $extensions = self::getEnabledExtensions(); 346 347 foreach ($extensions as $extension) { 348 $support = self::getSupportedExtension($extension->element); 349 350 if ($support->get('associationssupport') === true) { 351 self::$supportedExtensionsList[] = $extension->element; 352 } 353 354 self::$extensionsSupport[$extension->element] = $support; 355 } 356 357 return self::$extensionsSupport; 358 } 359 360 /** 361 * Get item context based on the item key. 362 * 363 * @param string $extensionName The extension identifier. 364 * 365 * @return \Joomla\Registry\Registry The item properties. 366 * 367 * @since 3.7.0 368 */ 369 public static function getSupportedExtension($extensionName) 370 { 371 $result = new Registry(); 372 373 $result->def('component', $extensionName); 374 $result->def('associationssupport', false); 375 $result->def('helper', null); 376 377 $helper = self::loadHelper($extensionName); 378 379 if (!$helper) { 380 return $result; 381 } 382 383 $result->set('helper', $helper); 384 385 if ($helper->hasAssociationsSupport() === false) { 386 return $result; 387 } 388 389 $result->set('associationssupport', true); 390 391 // Get the translated titles. 392 $languagePath = JPATH_ADMINISTRATOR . '/components/' . $extensionName; 393 $lang = Factory::getLanguage(); 394 395 $lang->load($extensionName . '.sys', JPATH_ADMINISTRATOR); 396 $lang->load($extensionName . '.sys', $languagePath); 397 $lang->load($extensionName, JPATH_ADMINISTRATOR); 398 $lang->load($extensionName, $languagePath); 399 400 $result->def('title', Text::_(strtoupper($extensionName))); 401 402 // Get the supported types 403 $types = $helper->getItemTypes(); 404 $rTypes = array(); 405 406 foreach ($types as $typeName) { 407 $details = $helper->getType($typeName); 408 $context = 'component'; 409 $title = $helper->getTypeTitle($typeName); 410 $languageKey = $typeName; 411 412 $typeNameExploded = explode('.', $typeName); 413 414 if (array_pop($typeNameExploded) === 'category') { 415 $languageKey = strtoupper($extensionName) . '_CATEGORIES'; 416 $context = 'category'; 417 } 418 419 if ($lang->hasKey(strtoupper($extensionName . '_' . $title . 'S'))) { 420 $languageKey = strtoupper($extensionName . '_' . $title . 'S'); 421 } 422 423 $title = $lang->hasKey($languageKey) ? Text::_($languageKey) : Text::_('COM_ASSOCIATIONS_ITEMS'); 424 425 $rType = new Registry(); 426 427 $rType->def('name', $typeName); 428 $rType->def('details', $details); 429 $rType->def('title', $title); 430 $rType->def('context', $context); 431 432 $rTypes[$typeName] = $rType; 433 } 434 435 $result->def('types', $rTypes); 436 437 return $result; 438 } 439 440 /** 441 * Get all installed and enabled extensions 442 * 443 * @return mixed 444 * 445 * @since 3.7.0 446 */ 447 private static function getEnabledExtensions() 448 { 449 $db = Factory::getDbo(); 450 451 $query = $db->getQuery(true) 452 ->select('*') 453 ->from($db->quoteName('#__extensions')) 454 ->where($db->quoteName('type') . ' = ' . $db->quote('component')) 455 ->where($db->quoteName('enabled') . ' = 1'); 456 457 $db->setQuery($query); 458 459 return $db->loadObjectList(); 460 } 461 462 /** 463 * Get all the content languages. 464 * 465 * @return array Array of objects all content languages by language code. 466 * 467 * @since 3.7.0 468 */ 469 public static function getContentLanguages() 470 { 471 return LanguageHelper::getContentLanguages(array(0, 1)); 472 } 473 474 /** 475 * Get the associated items for an item 476 * 477 * @param string $extensionName The extension name with com_ 478 * @param string $typeName The item type 479 * @param int $itemId The id of item for which we need the associated items 480 * 481 * @return boolean 482 * 483 * @since 3.7.0 484 */ 485 public static function allowEdit($extensionName, $typeName, $itemId) 486 { 487 if (!self::hasSupport($extensionName)) { 488 return false; 489 } 490 491 // Get the extension specific helper method 492 $helper = self::getExtensionHelper($extensionName); 493 494 if (method_exists($helper, 'allowEdit')) { 495 return $helper->allowEdit($typeName, $itemId); 496 } 497 498 return Factory::getUser()->authorise('core.edit', $extensionName); 499 } 500 501 /** 502 * Check if user is allowed to create items. 503 * 504 * @param string $extensionName The extension name with com_ 505 * @param string $typeName The item type 506 * 507 * @return boolean True on allowed. 508 * 509 * @since 3.7.0 510 */ 511 public static function allowAdd($extensionName, $typeName) 512 { 513 if (!self::hasSupport($extensionName)) { 514 return false; 515 } 516 517 // Get the extension specific helper method 518 $helper = self::getExtensionHelper($extensionName); 519 520 if (method_exists($helper, 'allowAdd')) { 521 return $helper->allowAdd($typeName); 522 } 523 524 return Factory::getUser()->authorise('core.create', $extensionName); 525 } 526 527 /** 528 * Check if an item is checked out 529 * 530 * @param string $extensionName The extension name with com_ 531 * @param string $typeName The item type 532 * @param int $itemId The id of item for which we need the associated items 533 * 534 * @return boolean True if item is checked out. 535 * 536 * @since 3.7.0 537 */ 538 public static function isCheckoutItem($extensionName, $typeName, $itemId) 539 { 540 if (!self::hasSupport($extensionName)) { 541 return false; 542 } 543 544 if (!self::typeSupportsCheckout($extensionName, $typeName)) { 545 return false; 546 } 547 548 // Get the extension specific helper method 549 $helper = self::getExtensionHelper($extensionName); 550 551 if (method_exists($helper, 'isCheckoutItem')) { 552 return $helper->isCheckoutItem($typeName, $itemId); 553 } 554 555 $item = self::getItem($extensionName, $typeName, $itemId); 556 557 $checkedOutFieldName = $helper->getTypeFieldName($typeName, 'checked_out'); 558 559 return $item->{$checkedOutFieldName} != 0; 560 } 561 562 /** 563 * Check if user can checkin an item. 564 * 565 * @param string $extensionName The extension name with com_ 566 * @param string $typeName The item type 567 * @param int $itemId The id of item for which we need the associated items 568 * 569 * @return boolean True on allowed. 570 * 571 * @since 3.7.0 572 */ 573 public static function canCheckinItem($extensionName, $typeName, $itemId) 574 { 575 if (!self::hasSupport($extensionName)) { 576 return false; 577 } 578 579 if (!self::typeSupportsCheckout($extensionName, $typeName)) { 580 return true; 581 } 582 583 // Get the extension specific helper method 584 $helper = self::getExtensionHelper($extensionName); 585 586 if (method_exists($helper, 'canCheckinItem')) { 587 return $helper->canCheckinItem($typeName, $itemId); 588 } 589 590 $item = self::getItem($extensionName, $typeName, $itemId); 591 592 $checkedOutFieldName = $helper->getTypeFieldName($typeName, 'checked_out'); 593 594 $userId = Factory::getUser()->id; 595 596 return ($item->{$checkedOutFieldName} == $userId || $item->{$checkedOutFieldName} == 0); 597 } 598 599 /** 600 * Check if the type supports checkout 601 * 602 * @param string $extensionName The extension name with com_ 603 * @param string $typeName The item type 604 * 605 * @return boolean True on allowed. 606 * 607 * @since 3.7.0 608 */ 609 public static function typeSupportsCheckout($extensionName, $typeName) 610 { 611 if (!self::hasSupport($extensionName)) { 612 return false; 613 } 614 615 // Get the extension specific helper method 616 $helper = self::getExtensionHelper($extensionName); 617 618 $support = $helper->getTypeSupport($typeName); 619 620 return !empty($support['checkout']); 621 } 622 623 /** 624 * Get a table field name for a type 625 * 626 * @param string $extensionName The extension name with com_ 627 * @param string $typeName The item type 628 * @param string $fieldName The item type 629 * 630 * @return boolean True on allowed. 631 * 632 * @since 3.7.0 633 */ 634 public static function getTypeFieldName($extensionName, $typeName, $fieldName) 635 { 636 if (!self::hasSupport($extensionName)) { 637 return false; 638 } 639 640 // Get the extension specific helper method 641 $helper = self::getExtensionHelper($extensionName); 642 643 return $helper->getTypeFieldName($typeName, $fieldName); 644 } 645 646 /** 647 * Gets the language filter system plugin extension id. 648 * 649 * @return integer The language filter system plugin extension id. 650 * 651 * @since 3.7.2 652 */ 653 public static function getLanguagefilterPluginId() 654 { 655 $db = Factory::getDbo(); 656 $query = $db->getQuery(true) 657 ->select($db->quoteName('extension_id')) 658 ->from($db->quoteName('#__extensions')) 659 ->where($db->quoteName('folder') . ' = ' . $db->quote('system')) 660 ->where($db->quoteName('element') . ' = ' . $db->quote('languagefilter')); 661 $db->setQuery($query); 662 663 try { 664 $result = (int) $db->loadResult(); 665 } catch (\RuntimeException $e) { 666 Factory::getApplication()->enqueueMessage($e->getMessage(), 'error'); 667 } 668 669 return $result; 670 } 671 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |