PageRenderTime 107ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/concreteOLD/models/block_types.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 847 lines | 597 code | 138 blank | 112 comment | 104 complexity | 61757f464448f4b38b7846b7d26b7922 MD5 | raw file
  1. <?php
  2. defined('C5_EXECUTE') or die("Access Denied.");
  3. /**
  4. * Contains the blocktype object, the block type list (which is just a wrapper for querying the system for block types, and the block type
  5. * DB wrapper for ADODB.
  6. * @package Blocks
  7. * @author Andrew Embler <andrew@concrete5.org>
  8. * @category Concrete
  9. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  10. * @license http://www.concrete5.org/license/ MIT License
  11. *
  12. */
  13. /**
  14. *
  15. * The block type list object holds types of blocks, takes care of querying the file system for newly available
  16. * @author Andrew Embler <andrew@concrete5.org>
  17. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  18. * @license http://www.concrete5.org/license/ MIT License
  19. * @package Blocks
  20. * @category Concrete
  21. */
  22. class BlockTypeList extends Object {
  23. public $btArray = array();
  24. /**
  25. * Gets an array of BlockTypes for a given Package
  26. * @param Package $pkg
  27. * @return BlockType[]
  28. */
  29. public static function getByPackage($pkg) {
  30. $db = Loader::db();
  31. $r = $db->Execute("select btID from BlockTypes where pkgID = ?", $pkg->getPackageID());
  32. $blockTypes = array();
  33. while ($row = $r->FetchRow()) {
  34. $blockTypes[] = BlockType::getByID($row['btID']);
  35. }
  36. return $blockTypes;
  37. }
  38. public static function exportList($xml) {
  39. $attribs = BlockTypeList::getInstalledList();
  40. $nxml = $xml->addChild('blocktypes');
  41. foreach($attribs as $bt) {
  42. $type = $nxml->addChild('blocktype');
  43. $type->addAttribute('handle', $bt->getBlockTypeHandle());
  44. $type->addAttribute('package', $bt->getPackageHandle());
  45. }
  46. }
  47. public static function getDashboardBlockTypes($ap) {
  48. $blockTypeIDs = $ap->getAddBlockTypes();
  49. $db = Loader::db();
  50. $btIDs = $db->GetCol('select btID from BlockTypes where btHandle like "dashboard_%" order by btID asc');
  51. $blockTypes = array();
  52. foreach($btIDs as $btID) {
  53. if (in_array($btID, $blockTypeIDs)) {
  54. $blockTypes[] = BlockType::getByID($btID);
  55. }
  56. }
  57. return $blockTypes;
  58. }
  59. function BlockTypeList($allowedBlocks = null) {
  60. $db = Loader::db();
  61. $this->btArray = array();
  62. $q = "select btID from BlockTypes where btIsInternal = 0 ";
  63. if ($allowedBlocks != null) {
  64. $q .= ' and btID in (' . implode(',', $allowedBlocks) . ') ';
  65. }
  66. $q .= ' order by btID asc';
  67. $r = $db->query($q);
  68. if ($r) {
  69. while ($row = $r->fetchRow()) {
  70. $bt = BlockType::getByID($row['btID']);
  71. if (is_object($bt)) {
  72. $this->btArray[] = $bt;
  73. }
  74. }
  75. $r->free();
  76. }
  77. return $this;
  78. }
  79. function getBlockTypeList() {
  80. return $this->btArray;
  81. }
  82. // This function only surveys the web/ directory - it's not looking at the package level.
  83. public static function getAvailableList() {
  84. $blocktypes = array();
  85. $dir = DIR_FILES_BLOCK_TYPES;
  86. $db = Loader::db();
  87. $btHandles = $db->GetCol("select btHandle from BlockTypes");
  88. $aDir = array();
  89. if (is_dir($dir)) {
  90. $handle = opendir($dir);
  91. while(($file = readdir($handle)) !== false) {
  92. if (strpos($file, '.') === false) {
  93. $fdir = $dir . '/' . $file;
  94. if (is_dir($fdir) && !in_array($file, $btHandles) && file_exists($fdir . '/' . FILENAME_BLOCK_CONTROLLER)) {
  95. $bt = new BlockType;
  96. $bt->btHandle = $file;
  97. $class = $bt->getBlockTypeClassFromHandle($file);
  98. require_once($fdir . '/' . FILENAME_BLOCK_CONTROLLER);
  99. if (!class_exists($class)) {
  100. continue;
  101. }
  102. $bta = new $class;
  103. $bt->btName = $bta->getBlockTypeName();
  104. $bt->btDescription = $bta->getBlockTypeDescription();
  105. $bt->hasCustomViewTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_VIEW);
  106. $bt->hasCustomEditTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_EDIT);
  107. $bt->hasCustomAddTemplate = file_exists(DIR_FILES_BLOCK_TYPES . '/' . $file . '/' . FILENAME_BLOCK_ADD);
  108. $btID = $db->GetOne("select btID from BlockTypes where btHandle = ?", array($file));
  109. $bt->installed = ($btID > 0);
  110. $bt->btID = $btID;
  111. $blocktypes[] = $bt;
  112. }
  113. }
  114. }
  115. }
  116. return $blocktypes;
  117. }
  118. public static function getInstalledList() {
  119. $db = Loader::db();
  120. $r = $db->query("select btID from BlockTypes order by btName asc");
  121. $btArray = array();
  122. while ($row = $r->fetchRow()) {
  123. $bt = BlockType::getByID($row['btID']);
  124. if (is_object($bt)) {
  125. $btArray[] = $bt;
  126. }
  127. }
  128. return $btArray;
  129. }
  130. // not really sure why these have two different calls.
  131. function getBlockTypeArray() {
  132. $db = Loader::db();
  133. $q = "select btID from BlockTypes order by btID asc";
  134. $r = $db->query($q);
  135. $btArray = array();
  136. if ($r) {
  137. while ($row = $r->fetchRow()) {
  138. $bt = BlockType::getByID($row['btID']);
  139. if (is_object($bt)) {
  140. $btArray[] = $bt;
  141. }
  142. }
  143. $r->free();
  144. }
  145. return $btArray;
  146. }
  147. function getAreaBlockTypes(&$a, &$cp) {
  148. $btl = new BlockTypeList();
  149. $btlaTMP = $btl->getBlockTypeList();
  150. $btla = array();
  151. foreach($btlaTMP as $bt) {
  152. $bt->setAreaPermissions($a, $cp);
  153. $btla[] = $bt;
  154. }
  155. return $btla;
  156. }
  157. function getBlockTypeAddAction(&$a) {
  158. $step = ($_REQUEST['step']) ? '&step=' . $_REQUEST['step'] : '';
  159. $arHandle = urlencode($a->getAreaHandle());
  160. $c = $a->getAreaCollectionObject();
  161. $cID = $c->getCollectionID();
  162. $valt = Loader::helper('validation/token');
  163. $str = DIR_REL . "/" . DISPATCHER_FILENAME . "?cID={$cID}&amp;areaName={$arHandle}&amp;mode=edit&amp;btask=add" . $step . '&' . $valt->getParameter();
  164. return $str;
  165. }
  166. function getBlockTypeAliasAction(&$a) {
  167. $step = ($_REQUEST['step']) ? '&step=' . $_REQUEST['step'] : '';
  168. $arHandle = urlencode($a->getAreaHandle());
  169. $c = $a->getAreaCollectionObject();
  170. $cID = $c->getCollectionID();
  171. $str = DIR_REL . "/" . DISPATCHER_FILENAME . "?cID={$cID}&amp;areaName={$arHandle}&amp;mode=edit&amp;btask=alias" . $step . '&' . $valt->getParameter();
  172. return $str;
  173. }
  174. }
  175. /**
  176. *
  177. * @access private
  178. */
  179. class BlockTypeDB extends ADOdb_Active_Record {
  180. public $_table = 'BlockTypes';
  181. }
  182. /**
  183. *
  184. * Any type of content that can be added to pages is represented as a type of block, and thereby a block type object.
  185. * @package Blocks
  186. * @author Andrew Embler <andrew@concrete5.org>
  187. * @license http://www.concrete5.org/license/ MIT License
  188. * @package Blocks
  189. * @category Concrete
  190. */
  191. class BlockType extends Object {
  192. public $addBTUArray = array();
  193. public $addBTGArray = array();
  194. public $controller;
  195. public static function getByHandle($handle) {
  196. $ca = new Cache();
  197. $bt = $ca->get('blockTypeByHandle', $handle);
  198. if (!is_object($bt)) {
  199. $where = 'btHandle = ?';
  200. $bt = BlockType::get($where, array($handle));
  201. $ca->set('blockTypeByHandle', $handle, $bt);
  202. }
  203. if (is_object($bt)) {
  204. $bt->controller = Loader::controller($bt);
  205. return $bt;
  206. }
  207. }
  208. /**
  209. * @param int $btID
  210. * @return BlockType
  211. */
  212. public static function getByID($btID) {
  213. $ca = new Cache();
  214. $bt = $ca->get('blockTypeByID', $btID);
  215. if (!is_object($bt)) {
  216. $where = 'btID = ?';
  217. $bt = BlockType::get($where, array($btID));
  218. $ca->set('blockTypeByID', $btID, $bt);
  219. }
  220. if (is_object($bt)) {
  221. $bt->controller = Loader::controller($bt);
  222. return $bt;
  223. }
  224. return $bt;
  225. }
  226. private static function get($where, $properties) {
  227. $db = Loader::db();
  228. $q = "SELECT btID, btName, btDescription, btHandle, pkgID, btActiveWhenAdded, btIsInternal, btCopyWhenPropagate, btIncludeAll, btInterfaceWidth, btInterfaceHeight from BlockTypes where {$where}";
  229. $r = $db->query($q, $properties);
  230. if ($r->numRows() > 0) {
  231. $row = $r->fetchRow();
  232. $bt = new BlockType;
  233. $bt->setPropertiesFromArray($row);
  234. return $bt;
  235. }
  236. }
  237. /**
  238. * @access private
  239. */
  240. function isBlockTypeInternal() {return $this->btIsInternal;}
  241. /**
  242. * Returns true if the block type is internal (and therefore cannot be removed)
  243. */
  244. public function isInternalBlockType() {
  245. return $this->btIsInternal;
  246. }
  247. /**
  248. * Returns true if the block type ships with concrete5
  249. */
  250. public function isCoreBlockType() {
  251. return is_dir(DIR_FILES_BLOCK_TYPES_CORE . '/' . $this->getBlockTypeHandle());
  252. }
  253. public function hasAddTemplate() {
  254. $bv = new BlockView();
  255. $bv->setBlockObject($this);
  256. $path = $bv->getBlockPath(FILENAME_BLOCK_ADD);
  257. if (file_exists($path . '/' . FILENAME_BLOCK_ADD)) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. function getBlockTypeInterfaceWidth() {return $this->btInterfaceWidth;}
  263. function getBlockTypeInterfaceHeight() {return $this->btInterfaceHeight;}
  264. public function getPackageID() {return $this->pkgID;}
  265. public function getPackageHandle() {
  266. return PackageList::getHandle($this->pkgID);
  267. }
  268. function canAddBlock($obj) {
  269. switch(strtolower(get_class($obj))) {
  270. case 'group':
  271. return in_array($obj->getGroupID(), $this->addBTGArray);
  272. break;
  273. case 'userinfo':
  274. return in_array($obj->getUserID(), $this->addBTUArray);
  275. break;
  276. }
  277. }
  278. /**
  279. * Returns the number of unique instances of this block
  280. */
  281. public function getCount() {
  282. $db = Loader::db();
  283. $count = $db->GetOne("select count(btID) from Blocks where btID = ?", array($this->btID));
  284. return $count;
  285. }
  286. /**
  287. * Not a permissions call. Actually checks to see whether this block is not an internal one.
  288. */
  289. public function canUnInstall() {
  290. /*$cnt = $this->getCount();
  291. if ($cnt > 0 || $this->isBlockTypeInternal()) {
  292. return false;
  293. }*/
  294. return (!$this->isBlockTypeInternal());
  295. }
  296. function getBlockTypeDescription() {
  297. return $this->btDescription;
  298. }
  299. function getBlockTypeCustomTemplates() {
  300. $btHandle = $this->getBlockTypeHandle();
  301. $pkgHandle = $this->getPackageHandle();
  302. $templates = array();
  303. $fh = Loader::helper('file');
  304. if (file_exists(DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES)) {
  305. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES));
  306. }
  307. /*
  308. if ($pkgHandle != null) {
  309. if (is_dir(DIR_PACKAGES . '/' . $pkgHandle)) {
  310. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_PACKAGES . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES));
  311. } else {
  312. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_PACKAGES_CORE . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES));
  313. }
  314. }
  315. */
  316. // NOW, we check to see if this btHandle has any custom templates that have been installed as separate packages
  317. $pl = PackageList::get();
  318. $packages = $pl->getPackages();
  319. foreach($packages as $pkg) {
  320. $d = (is_dir(DIR_PACKAGES . '/' . $pkg->getPackageHandle())) ? DIR_PACKAGES . '/'. $pkg->getPackageHandle() : DIR_PACKAGES_CORE . '/'. $pkg->getPackageHandle();
  321. if (is_dir($d . '/' . DIRNAME_BLOCKS . '/' . $btHandle . '/' . DIRNAME_BLOCK_TEMPLATES)) {
  322. $templates = array_merge($templates, $fh->getDirectoryContents($d . '/' . DIRNAME_BLOCKS . '/' . $btHandle . '/' . DIRNAME_BLOCK_TEMPLATES));
  323. }
  324. }
  325. if (file_exists(DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES)) {
  326. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES));
  327. }
  328. $templates = array_unique($templates);
  329. return $templates;
  330. }
  331. function getBlockTypeComposerTemplates() {
  332. $btHandle = $this->getBlockTypeHandle();
  333. $pkgHandle = $this->getPackageHandle();
  334. $templates = array();
  335. $fh = Loader::helper('file');
  336. if (file_exists(DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER)) {
  337. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER));
  338. }
  339. if (file_exists(DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER)) {
  340. $templates = array_merge($templates, $fh->getDirectoryContents(DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . DIRNAME_BLOCK_TEMPLATES_COMPOSER));
  341. }
  342. $templates = array_unique($templates);
  343. return $templates;
  344. }
  345. function setAreaPermissions(&$area, &$cp) {
  346. $db = Loader::db();
  347. if ($area->overrideCollectionPermissions()) {
  348. $setBlocksVia = "AREA";
  349. } else {
  350. if ($area->getAreaCollectionInheritID() > 0) {
  351. // see if the area/page we're supposed to be getting these from actually has a record
  352. $arOverrideCollectionPermissions = $db->getOne("select arOverrideCollectionPermissions from Areas where cID = ? and arHandle = ?", array($area->getAreaCollectionInheritID(), $area->getAreaHandle()));
  353. if ($arOverrideCollectionPermissions) {
  354. $setBlocksVia = "AREA";
  355. }
  356. }
  357. if (!isset($setBlocksVia)) {
  358. $setBlocksVia = "PAGE";
  359. }
  360. }
  361. if ($setBlocksVia == "AREA") {
  362. $c = $area->getAreaCollectionObject();
  363. $cpID = ($area->getAreaCollectionInheritID() > 0) ? $area->getAreaCollectionInheritID() : $c->getCollectionID();
  364. $v = array($cpID, $area->getAreaHandle(), $this->getBlockTypeID());
  365. $q = "select uID, gID from AreaGroupBlockTypes where cID = ? and arHandle = ? and btID = ?";
  366. $r = $db->query($q, $v);
  367. while ($row = $r->fetchRow()) {
  368. if ($row['uID'] != 0) {
  369. $this->addBTUArray[] = $row['uID'];
  370. }
  371. if ($row['gID'] != 0) {
  372. $this->addBTGArray[] = $row['gID'];
  373. }
  374. }
  375. } else {
  376. $cID = $area->getCollectionID();
  377. // we grab all the uID/gID combos from PagePermissions that can edit the page
  378. // then we allow them to add all the blocks they want
  379. $cInheritCID = $db->getOne('select cInheritPermissionsFromCID from Pages where cID = ?', array($cID));
  380. $v = array($cInheritCID);
  381. $q = "select uID, gID, cgPermissions from PagePermissions where cID = ?";
  382. $r = $db->query($q, $v);
  383. while ($row = $r->fetchRow()) {
  384. if ($row['uID'] != 0 && strpos($row['cgPermissions'], 'wa') !== false) {
  385. $this->addBTUArray[] = $row['uID'];
  386. }
  387. if ($row['gID'] != 0 && strpos($row['cgPermissions'], 'wa') !== false) {
  388. $this->addBTGArray[] = $row['gID'];
  389. }
  390. }
  391. }
  392. }
  393. function installBlockTypeFromPackage($btHandle, $pkg, $btID = 0) {
  394. $dir1 = DIR_PACKAGES . '/' . $pkg->getPackageHandle() . '/' . DIRNAME_BLOCKS;
  395. $dir2 = DIR_PACKAGES_CORE . '/' . $pkg->getPackageHandle() . '/' . DIRNAME_BLOCKS;
  396. if (file_exists($dir1)) {
  397. $dir = $dir1;
  398. } else {
  399. $dir = $dir2;
  400. }
  401. // now we check to see if it's been overridden in the site root and if so we do it there
  402. if ($btID > 0) {
  403. // this is only necessary when it's an existing refresh
  404. if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $btHandle . '/' . FILENAME_BLOCK_CONTROLLER)) {
  405. $dir = DIR_FILES_BLOCK_TYPES;
  406. }
  407. }
  408. $bt = new BlockType;
  409. $bt->btHandle = $btHandle;
  410. $bt->pkgHandle = $pkg->getPackageHandle();
  411. $bt->pkgID = $pkg->getPackageID();
  412. return BlockType::doInstallBlockType($btHandle, $bt, $dir, $btID);
  413. }
  414. public function refresh() {
  415. if ($this->getPackageID() > 0) {
  416. $pkg = Package::getByID($this->getPackageID());
  417. $resp = BlockType::installBlockTypeFromPackage($this->getBlockTypeHandle(), $pkg, $this->getBlockTypeID());
  418. if ($resp != '') {
  419. throw new Exception($resp);
  420. }
  421. } else {
  422. $resp = BlockType::installBlockType($this->getBlockTypeHandle(), $this->getBlockTypeID());
  423. if ($resp != '') {
  424. throw new Exception($resp);
  425. }
  426. }
  427. }
  428. function installBlockType($btHandle, $btID = 0) {
  429. if ($btID == 0) {
  430. // then we don't allow one to already exist
  431. $db = Loader::db();
  432. $cnt = $db->GetOne("select btID from BlockTypes where btHandle = ?", array($btHandle));
  433. if ($cnt > 0) {
  434. return false;
  435. }
  436. }
  437. if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $btHandle . '/' . FILENAME_BLOCK_CONTROLLER)) {
  438. $dir = DIR_FILES_BLOCK_TYPES;
  439. } else {
  440. $dir = DIR_FILES_BLOCK_TYPES_CORE;
  441. }
  442. $bt = new BlockType;
  443. $bt->btHandle = $btHandle;
  444. $bt->pkgHandle = null;
  445. $bt->pkgID = 0;
  446. return BlockType::doInstallBlockType($btHandle, $bt, $dir, $btID);
  447. }
  448. /**
  449. * Renders a particular view of a block type, using the public $controller variable as the block type's controller
  450. */
  451. public function render($view) {
  452. $bv = new BlockView();
  453. $bv->setController($this->controller);
  454. $bv->render($this, $view);
  455. }
  456. public function getController() {
  457. return $this->controller;
  458. }
  459. private function doInstallBlockType($btHandle, $bt, $dir, $btID = 0) {
  460. $db = Loader::db();
  461. if (file_exists($dir . '/' . $btHandle . '/' . FILENAME_BLOCK_CONTROLLER)) {
  462. $class = $bt->getBlockTypeClassFromHandle();
  463. $path = $dir . '/' . $btHandle;
  464. if (!class_exists($class)) {
  465. require_once($dir . '/' . $btHandle . '/' . FILENAME_BLOCK_CONTROLLER);
  466. }
  467. if (!class_exists($class)) {
  468. throw new Exception(t("%s not found. Please check that the block controller file contains the correct class name.", $class));
  469. }
  470. $bta = new $class;
  471. // first run the subclass methods. If they work then we install the block
  472. $r = $bta->install($path);
  473. if (!$r->result) {
  474. return $r->message;
  475. }
  476. $btd = new BlockTypeDB();
  477. $btd->btHandle = $btHandle;
  478. $btd->btName = $bta->getBlockTypeName();
  479. $btd->btDescription = $bta->getBlockTypeDescription();
  480. $btd->btActiveWhenAdded = $bta->isActiveWhenAdded();
  481. $btd->btCopyWhenPropagate = $bta->isCopiedWhenPropagated();
  482. $btd->btIncludeAll = $bta->includeAll();
  483. $btd->btIsInternal = $bta->isBlockTypeInternal();
  484. $btd->btInterfaceHeight = $bta->getInterfaceHeight();
  485. $btd->btInterfaceWidth = $bta->getInterfaceWidth();
  486. $btd->pkgID = $bt->getPackageID();
  487. if ($btID > 0) {
  488. $btd->btID = $btID;
  489. $r = $btd->Replace();
  490. } else {
  491. $r = $btd->save();
  492. }
  493. // now we remove the block type from cache
  494. $ca = new Cache();
  495. $ca->delete('blockTypeByID', $btID);
  496. $ca->delete('blockTypeByHandle', $btHandle);
  497. $ca->delete('blockTypeList', false);
  498. if (!$r) {
  499. return $db->ErrorMsg();
  500. }
  501. } else {
  502. return t("No block found with the handle %s.", $btHandle);
  503. }
  504. }
  505. /*
  506. * Returns a path to where the block type's files are located.
  507. * @access public
  508. * @return string $path
  509. */
  510. public function getBlockTypePath() {
  511. if ($this->getPackageID() > 0) {
  512. $pkgHandle = $this->getPackageHandle();
  513. $dirp = (is_dir(DIR_PACKAGES . '/' . $pkgHandle)) ? DIR_PACKAGES : DIR_PACKAGES_CORE;
  514. $dir = $dirp . '/' . $pkgHandle . '/' . DIRNAME_BLOCKS . '/' . $this->getBlockTypeHandle();
  515. } else {
  516. if (is_dir(DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle())) {
  517. $dir = DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle();
  518. } else {
  519. $dir = DIR_FILES_BLOCK_TYPES_CORE . '/' . $this->getBlockTypeHandle();
  520. }
  521. }
  522. return $dir;
  523. }
  524. /*
  525. * @access private
  526. *
  527. */
  528. private function _getClass() {
  529. $btHandle = $this->btHandle;
  530. $pkgHandle = $this->getPackageHandle();
  531. if (file_exists(DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER)) {
  532. $classfile = DIR_FILES_BLOCK_TYPES . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER;
  533. } else if ($pkgHandle != null) {
  534. if (file_exists(DIR_PACKAGES . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER)) {
  535. $classfile = DIR_PACKAGES . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER;
  536. } else if (file_exists(DIR_PACKAGES_CORE . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER)) {
  537. $classfile = DIR_PACKAGES_CORE . "/{$pkgHandle}/" . DIRNAME_BLOCKS . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER;
  538. }
  539. } else {
  540. if (file_exists(DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER)) {
  541. $classfile = DIR_FILES_BLOCK_TYPES_CORE . "/{$btHandle}/" . FILENAME_BLOCK_CONTROLLER;
  542. }
  543. }
  544. if ($classfile) {
  545. require_once($classfile);
  546. // takes the handle and performs some magic to get the class;
  547. $btHandle = $this->getBlockTypeHandle();
  548. // split by underscores or dashes
  549. $words = preg_split('/\_|\-/', $btHandle);
  550. for ($i = 0; $i < count($words); $i++) {
  551. $words[$i] = ucfirst($words[$i]);
  552. }
  553. $class = implode('', $words);
  554. $class = $class . 'BlockController';
  555. return $class;
  556. }
  557. }
  558. public function inc($file, $args = array()) {
  559. extract($args);
  560. $bt = $this;
  561. global $c;
  562. global $a;
  563. if ($this->getPackageID() > 0) {
  564. if (is_dir(DIR_PACKAGES . '/' . $this->getPackageHandle())) {
  565. include(DIR_PACKAGES . '/' . $this->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $this->getBlockTypeHandle() . '/' . $file);
  566. } else {
  567. include(DIR_PACKAGES_CORE . '/' . $this->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $this->getBlockTypeHandle() . '/' . $file);
  568. }
  569. } else if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle() . '/' . $file)) {
  570. include(DIR_FILES_BLOCK_TYPES . '/' . $this->getBlockTypeHandle() . '/' . $file);
  571. } else {
  572. include(DIR_FILES_BLOCK_TYPES_CORE . '/' . $this->getBlockTypeHandle() . '/' . $file);
  573. }
  574. }
  575. public function getBlockTypeClass() {
  576. $btHandle = $this->getBlockTypeHandle();
  577. return $this->_getClass($btHandle);
  578. }
  579. public function getBlockTypeClassFromHandle() {
  580. return $this->_getClass();
  581. }
  582. /**
  583. * Removes the block type. Also removes instances of content.
  584. */
  585. public function delete() {
  586. $db = Loader::db();
  587. $r = $db->Execute('select cID, cvID, b.bID, arHandle from CollectionVersionBlocks cvb inner join Blocks b on b.bID = cvb.bID where btID = ?', array($this->getBlockTypeID()));
  588. while ($row = $r->FetchRow()) {
  589. $nc = Page::getByID($row['cID'], $row['cvID']);
  590. $b = Block::getByID($row['bID'], $nc, $row['arHandle']);
  591. $b->deleteBlock();
  592. }
  593. $ca = new Cache();
  594. $ca->delete('blockTypeByID', $this->btID);
  595. $ca->delete('blockTypeByHandle', $btHandle);
  596. $ca->delete('blockTypeList', false);
  597. $db->Execute("delete from BlockTypes where btID = ?", array($this->btID));
  598. }
  599. /**
  600. * Allows block types to be updated
  601. * @param array $data
  602. */
  603. public function update($data) {
  604. $db = Loader::db();
  605. $btHandle = $this->btHandle;
  606. $btName = $this->btName;
  607. $this->btDescription = $this->btDescription;
  608. if (isset($data['btHandle'])) {
  609. $btHandle = $data['btHandle'];
  610. }
  611. if (isset($data['btName'])) {
  612. $btName = $data['btName'];
  613. }
  614. if (isset($data['btDescription'])) {
  615. $btDescription = $data['btDescription'];
  616. }
  617. $db->Execute('update BlockTypes set btHandle = ?, btName = ?, btDescription = ? where btID = ?', array($btHandle, $btName, $btDescription, $this->btID));
  618. // now we remove the block type from cache
  619. $ca = new Cache();
  620. $ca->delete('blockTypeByID', $this->btID);
  621. $ca->delete('blockTypeByHandle', $btHandle);
  622. $ca->delete('blockTypeList', false);
  623. }
  624. /*
  625. * Adds a block to the system without adding it to a collection.
  626. * Passes page and area data along if it is available, however.
  627. */
  628. public function add($data, $c = false, $a = false) {
  629. $db = Loader::db();
  630. $u = new User();
  631. if (isset($data['uID'])) {
  632. $uID = $data['uID'];
  633. } else {
  634. $uID = $u->getUserID();
  635. }
  636. $btID = $this->btID;
  637. $dh = Loader::helper('date');
  638. $bDate = $dh->getSystemDateTime();
  639. $bIsActive = ($this->btActiveWhenAdded == 1) ? 1 : 0;
  640. $v = array($_POST['bName'], $bDate, $bDate, $bIsActive, $btID, $uID);
  641. $q = "insert into Blocks (bName, bDateAdded, bDateModified, bIsActive, btID, uID) values (?, ?, ?, ?, ?, ?)";
  642. $r = $db->prepare($q);
  643. $res = $db->execute($r, $v);
  644. $bIDnew = $db->Insert_ID();
  645. // we get the block object for the block we just added
  646. if ($res) {
  647. $nb = Block::getByID($bIDnew);
  648. $btHandle = $this->getBlockTypeHandle();
  649. $class = $this->getBlockTypeClass();
  650. $bc = new $class($nb);
  651. if (is_object($c)) {
  652. $bc->setCollectionObject($c);
  653. }
  654. $bc->save($data);
  655. // the previous version of the block above is cached without the values
  656. $nb->refreshCache();
  657. return Block::getByID($bIDnew);
  658. }
  659. }
  660. function getBlockTypeID() {
  661. return $this->btID;
  662. }
  663. function getBlockTypeHandle() {
  664. return $this->btHandle;
  665. }
  666. // getBlockAddAction vs. getBlockTypeAddAction() - The difference is very simple. We call getBlockTypeAddAction() to grab the
  667. // action properties for the form that presents the drop-down select menu for selecting which type of block to add. We call the other
  668. // function when we've already chosen a type to add, and we're interested in actually adding the block - content completed - to the database
  669. function getBlockAddAction(&$a, $alternateHandler = null) {
  670. // Note: This is fugly, since we're just grabbing query string variables, but oh well. Not _everything_ can be object oriented
  671. $btID = $this->btID;
  672. $step = ($_REQUEST['step']) ? '&step=' . $_REQUEST['step'] : '';
  673. $c = $a->getAreaCollectionObject();
  674. $cID = $c->getCollectionID();
  675. $arHandle = urlencode($a->getAreaHandle());
  676. $valt = Loader::helper('validation/token');
  677. if ($alternateHandler) {
  678. $str = $alternateHandler . "?cID={$cID}&arHandle={$arHandle}&btID={$btID}&mode=edit" . $step . '&' . $valt->getParameter();
  679. } else {
  680. $str = DIR_REL . "/" . DISPATCHER_FILENAME . "?cID={$cID}&arHandle={$arHandle}&btID={$btID}&mode=edit" . $step . '&' . $valt->getParameter();
  681. }
  682. return $str;
  683. }
  684. function getBlockTypeName() {
  685. return $this->btName;
  686. }
  687. function isInstalled() {
  688. return $this->installed;
  689. }
  690. function getBlockTypeActiveWhenAdded() {
  691. return $this->btActiveWhenAdded;
  692. }
  693. function isCopiedWhenPropagated() {
  694. return $this->btCopyWhenPropagate;
  695. }
  696. function includeAll() {
  697. return $this->btIncludeAll;
  698. }
  699. function hasCustomEditTemplate() {
  700. return $this->hasCustomEditTemplate;
  701. }
  702. function hasCustomViewTemplate() {
  703. return $this->hasCustomViewTemplate;
  704. }
  705. function hasCustomAddTemplate() {
  706. return $this->hasCustomAddTemplate;
  707. }
  708. }
  709. ?>