PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Block/BlockType/BlockTypeList.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 145 lines | 110 code | 16 blank | 19 comment | 11 complexity | 500c229f0411b33825d14e83e80df43f MD5 | raw file
  1. <?php
  2. namespace Concrete\Core\Block\BlockType;
  3. use Core;
  4. use Loader;
  5. use Environment;
  6. use CacheLocal;
  7. use BlockType as ConcreteBlockType;
  8. use Package;
  9. use \Concrete\Core\Legacy\DatabaseItemList;
  10. class BlockTypeList extends DatabaseItemList {
  11. protected $autoSortColumns = array('btHandle', 'btID', 'btDisplayOrder');
  12. protected $includeInternalBlockTypes = false;
  13. function __construct() {
  14. $this->setQuery("select btID from BlockTypes");
  15. $this->sortByMultiple('btDisplayOrder asc', 'btName asc', 'btID asc');
  16. }
  17. public function includeInternalBlockTypes()
  18. {
  19. $this->includeInternalBlockTypes = true;
  20. }
  21. public function get($itemsToGet = 100, $offset = 0) {
  22. if (!$this->includeInternalBlockTypes) {
  23. $this->filter('btIsInternal', false);
  24. }
  25. $r = parent::get( $itemsToGet, intval($offset));
  26. $blocktypes = array();
  27. foreach($r as $row) {
  28. $bt = ConcreteBlockType::getByID($row['btID']);
  29. if (is_object($bt)) {
  30. $blocktypes[] = $bt;
  31. }
  32. }
  33. return $blocktypes;
  34. }
  35. public function filterByPackage(Package $pkg) {
  36. $this->filter('pkgID', $pkg->getPackageID());
  37. }
  38. /**
  39. * @todo comment this one
  40. * @param string $xml
  41. * @return void
  42. */
  43. public static function exportList($xml) {
  44. $btl = new static();
  45. $blocktypes = $btl->get();
  46. $nxml = $xml->addChild('blocktypes');
  47. foreach($blocktypes as $bt) {
  48. $type = $nxml->addChild('blocktype');
  49. $type->addAttribute('handle', $bt->getBlockTypeHandle());
  50. $type->addAttribute('package', $bt->getPackageHandle());
  51. }
  52. }
  53. /**
  54. * returns an array of Block Types used in the concrete5 Dashboard
  55. * @return BlockType[]
  56. */
  57. public static function getDashboardBlockTypes() {
  58. $btl = new static();
  59. $btl->filter(false, 'btHandle like \'dashboard_%\'');
  60. $blockTypes = $btl->get();
  61. return $blockTypes;
  62. }
  63. /**
  64. * Gets a list of block types that are not installed, used to get blocks that can be installed
  65. * This function only surveys the web/blocks directory - it's not looking at the package level.
  66. * @return BlockType[]
  67. */
  68. public static function getAvailableList() {
  69. $blocktypes = array();
  70. $dir = DIR_FILES_BLOCK_TYPES;
  71. $db = Loader::db();
  72. $btHandles = $db->GetCol("select btHandle from BlockTypes order by btDisplayOrder asc, btName asc, btID asc");
  73. $aDir = array();
  74. if (is_dir($dir)) {
  75. $handle = opendir($dir);
  76. while(($file = readdir($handle)) !== false) {
  77. if (strpos($file, '.') === false) {
  78. $fdir = $dir . '/' . $file;
  79. if (is_dir($fdir) && !in_array($file, $btHandles) && file_exists($fdir . '/' . FILENAME_BLOCK_CONTROLLER)) {
  80. $bt = BlockType::getByHandle($file);
  81. if (!is_object($bt)) {
  82. $bt = new BlockType;
  83. $bt->setBlockTypeHandle($file);
  84. $class = $bt->getBlockTypeClass();
  85. $bta = new $class;
  86. $bt->setBlockTypeName($bta->getBlockTypeName());
  87. $bt->setBlockTypeDescription($bta->getBlockTypeDescription());
  88. $bt->hasCustomViewTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_VIEW);
  89. $bt->hasCustomEditTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_EDIT);
  90. $bt->hasCustomAddTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_ADD);
  91. $bt->installed = false;
  92. } else {
  93. $bt->installed = true;
  94. }
  95. $blocktypes[] = $bt;
  96. }
  97. }
  98. }
  99. }
  100. return $blocktypes;
  101. }
  102. /**
  103. * gets a list of installed BlockTypes
  104. * @return BlockType[]
  105. */
  106. public static function getInstalledList() {
  107. $btl = new static();
  108. return $btl->get();
  109. }
  110. public static function resetBlockTypeDisplayOrder($column = 'btID') {
  111. $db = Loader::db();
  112. /** @var \Concrete\Core\Cache\Cache $cache */
  113. $cache = Core::make('cache');
  114. $stmt = $db->Prepare("UPDATE BlockTypes SET btDisplayOrder = ? WHERE btID = ?");
  115. $btDisplayOrder = 1;
  116. $blockTypes = $db->GetArray("SELECT btID, btHandle, btIsInternal FROM BlockTypes ORDER BY {$column} ASC");
  117. foreach ($blockTypes as $bt) {
  118. if ($bt['btIsInternal']) {
  119. $db->Execute($stmt, array(0, $bt['btID']));
  120. } else {
  121. $db->Execute($stmt, array($btDisplayOrder, $bt['btID']));
  122. $btDisplayOrder++;
  123. }
  124. $cache->delete('blockTypeByID/' .$bt['btID']);
  125. $cache->delete('blockTypeByHandle/' . $bt['btHandle']);
  126. }
  127. $cache->delete('blockTypeList');
  128. }
  129. }