PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/jfx-private/classes/JFX/Block.php

http://jfxcms.googlecode.com/
PHP | 941 lines | 728 code | 170 blank | 43 comment | 143 complexity | a152f511146e6364189b5029a5fa0cba MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. ############### COPYLEFT GPLv3 LICENSE ###############
  3. ##
  4. ## JFX Version 0.2.9
  5. ## Website Management Software
  6. ## www.jfxcms.com
  7. ##
  8. ## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
  9. ##
  10. ## Anthony Gallon
  11. ## oi_antz@hotmail.com
  12. ##
  13. ## Permission is hereby granted to any person having a copy of this software
  14. ## to freely use and modify as required so long as the copyright notices
  15. ## and branding remain intact.
  16. ##
  17. ## Full license details available at http://www.jfxcms.com/license
  18. ##
  19. ############### COPYLEFT GPLv3 LICENSE ###############
  20. class JFX_Block {
  21. /**
  22. * Children of this block
  23. * @var JFX_Block
  24. */
  25. public $elements = array();
  26. /**
  27. * Database table jfx_blocks
  28. * @var array
  29. */
  30. public $details = array(
  31. 'id' => '',
  32. 'parent_id' => '',
  33. 'module_key' => '',
  34. 'module_action' => '',
  35. 'sorting' => '',
  36. 'page_parent' => '',
  37. 'class_name' => '',
  38. 'ajax_enabled' => '',
  39. 'is_droppable' => '',
  40. 'page_id' => '',
  41. 'workspace_id' => '',
  42. 'exec_order' => '',
  43. 'template' => '',
  44. 'template_id' => '',
  45. 'block_style' => '',
  46. 'php_code' => '',
  47. 'is_template' => 0,
  48. 'template_script' => '',
  49. 'template_style' => ''
  50. );
  51. public $id = '';
  52. public $parentid = '';
  53. public $pageid = '';
  54. public $workspaceid = '';
  55. public $moduleKey = '';
  56. public $moduleAction = '';
  57. public $object = NULL;
  58. public $title = 'New Block';
  59. public $defaultContent = '';
  60. public $isPage = false;
  61. public $isBlock = true;
  62. public $isTemplate = false;
  63. /**
  64. * Config data
  65. * @var JFX_Block_Config
  66. */
  67. public $config = array();
  68. public $saved = false;
  69. public function __construct($blockId=null, $pageid=''){
  70. if($blockId !== null && $blockId!=''){
  71. $this->loadById($blockId, $pageid);
  72. }
  73. }
  74. public static function getBlockById($id, $pageId, $workspaceid = '', $recursive = true, $loadObject = true){
  75. $REGISTRY = JFX::registry('JFX_Block_Registry');
  76. $SETTINGS = JFX::registry('JFX_Settings');
  77. $CONFIG = JFX::registry('config');
  78. if($pageId==''){
  79. $pageId = $id;
  80. }
  81. $registryKey = 'JFX_Block_'.$id.'_'.$pageId.'_'.$workspaceid;
  82. if(!$REGISTRY->isRegistered($registryKey)){
  83. if(!array_key_exists($registryKey, $GLOBALS)){
  84. $GLOBALS[$registryKey] = true;
  85. }else{
  86. throw new exception('trying to decode '.$id.' for '.$pageId.' a second time');
  87. }
  88. if(is_numeric($id)){
  89. // its a page
  90. $block = new JFX_Page;
  91. $block->loadById($id, $pageId, $workspaceid, $recursive, $loadObject);
  92. $block->set('id', 'jfxpage-'.$pageId);
  93. }else if(self::isTemplate($id)){
  94. // its a template
  95. $block = new JFX_Template;
  96. $block->loadById($id, $pageId, $workspaceid, $recursive, $loadObject);
  97. }else{
  98. // its a block
  99. $block = new JFX_Block;
  100. $block->loadById($id, $pageId, $workspaceid, $recursive, $loadObject);
  101. }
  102. if($block->details['id']==''){
  103. if(is_numeric(substr($id, 8))){
  104. // try for page block
  105. $block = new JFX_Page;
  106. $block->loadById(substr($id, 8), $pageId, $workspaceid, $recursive, $loadObject);
  107. $block->set('id', 'jfxpage-'.$pageId);
  108. }else{
  109. // try for template block
  110. $block = new JFX_Block;
  111. $block->loadById($id, $pageId, $workspaceid, $recursive, $loadObject);
  112. }
  113. }
  114. if(!is_object($block) || (!($block instanceof JFX_Block) && !($block instanceof JFX_Page))){
  115. throw new Exception('Cannot load block by id: '.$id);
  116. }
  117. $REGISTRY->register($registryKey, $block);
  118. }
  119. return $REGISTRY->registry($registryKey);
  120. }
  121. public static function getTitleById($id, $pageId='', $language='', $workspace=''){
  122. $block = self::getBlockById($id, $pageId, $workspace);
  123. $SETTINGS = JFX::registry('JFX_Settings');
  124. $CORE = JFX::registry('JFX_Core');
  125. if($workspace==''){
  126. $workspace = $SETTINGS->get('live_workspace_id');
  127. }
  128. if((array_key_exists('template_id', $block->details) && strlen($block->details['template_id'])>0)){
  129. return self::getTitleById($block->details['template_id'], $pageId);
  130. }
  131. return $CORE->getContentLang('heading', $id, $language, $workspace);
  132. }
  133. public function setClassName($className){
  134. if(is_array($className)){
  135. $className = implode(' ', $className);
  136. }
  137. $this->details['class_name'] = $className;
  138. if(is_object($this->object) && method_exists($this->object, 'setClassName'))
  139. $this->object->setClassName($className, $this->details);
  140. }
  141. public function addClassName($className){
  142. $allClassnames = explode(' ', $this->details['class_name']);
  143. if(!in_array($className, $allClassnames)){
  144. $this->details['class_name'] = $this->details['class_name'].' '.$className;
  145. }
  146. }
  147. public function getClassName(){
  148. if(!array_key_exists('class_name', $this->details)){
  149. return '';
  150. }
  151. $className = $this->details['class_name'];
  152. if(is_object($this->object) && method_exists($this->object, 'getClassName'))
  153. $className .= $this->object->getClassName($this->details);
  154. return $className;
  155. }
  156. public function setTemplate($template){
  157. if(is_object($this->object) && method_exists($this->object, 'setTemplate'))
  158. $template = $this->object->setTemplate($template, $this->details);
  159. $this->details['template'] = $template;
  160. }
  161. public function getTemplate(){
  162. return $this->details['template'];
  163. }
  164. public function setStyle($style){
  165. $this->details['block_style'] = $style;
  166. }
  167. public static function generateBlockId($pageid, $module, $workspace=''){
  168. $DB = JFX::registry('db');
  169. $CONFIG = JFX::registry('config');
  170. $USER = JFX::registry('JFX_User');
  171. if($workspace==''){
  172. $workspace = $USER->workspace;
  173. }
  174. $countStart = 0;
  175. $idPattern = 'jfxpage-'.$pageid.'-'.$module.'-';
  176. $newId = $idPattern.$countStart;
  177. while($DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$newId}'")>0){
  178. $countStart++;
  179. $newId = $idPattern.$countStart;
  180. }
  181. return $newId;
  182. }
  183. public function delete(){
  184. if(!$this->canDelete()){
  185. return false;
  186. }
  187. $DB = JFX::registry('db');
  188. $CONFIG = JFX::registry('config');
  189. $LANG = JFX::registry('lang');
  190. $CORE = JFX::registry('JFX_Core');
  191. foreach($this->elements as $k=>$v){
  192. $v->delete();
  193. };
  194. $id = explode('-', $this->id);
  195. $pageid = $id[1];
  196. $moduleKey = $id[2];
  197. $instanceid = $id[3];
  198. $params = array(
  199. 'page_id' => $this->pageid,
  200. 'workspace_id' => $this->workspaceid,
  201. 'instance_id' => $instanceid
  202. );
  203. if(!is_object($this->object)) $this->loadObject();
  204. $this->object->delete($this->details);
  205. $DB->delete($CONFIG->dbprefix.'blocks', "id = '{$this->id}' AND workspace_id = '{$this->workspaceid}'");
  206. $CORE->deleteContentLang('heading', $this->id, '*', $this->workspaceid);
  207. }
  208. public function clearCache(){
  209. $CACHE = JFX::registry('JFX_Block_Cache');
  210. $CACHE->clear($this->id.'_'.$this->workspaceid);
  211. }
  212. public function loadObject(){
  213. if($this->moduleKey == '') $this->moduleKey = 'content';
  214. if(!is_object($this->object)) $this->object = JFX::registry('JFX_Module_'.ucfirst(strtolower($this->moduleKey)));
  215. $this->object->setBlockId($this->id);
  216. return $this->object;
  217. }
  218. public function loadById($id, $pageId='', $workspaceid = '', $recursive = true, $loadObject = true){
  219. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before load by id '.$id);
  220. $DB = JFX::registry('db');
  221. $LANG = JFX::registry('lang');
  222. $USER = JFX::registry('JFX_User');
  223. $CONFIG = JFX::registry('config');
  224. $REGISTRY = JFX::registry('JFX_Block_Registry');
  225. $CORE = JFX::registry('JFX_Core');
  226. $SETTINGS= JFX::registry('JFX_Settings');
  227. $this->config = new JFX_Block_Config($id);
  228. $templatePageid = $SETTINGS->get('template_page_id');
  229. $substrId = substr($id, 8);
  230. if($pageId==$id || is_numeric($substrId)){
  231. // its a page
  232. if(!is_numeric($id)){
  233. $id = substr($id, 8);
  234. }
  235. }
  236. if($pageId==''){
  237. $pageId = $SETTINGS->get('template_page_id');
  238. }
  239. $registryKey = 'JFX_Block_'.$id.'_'.$pageId.'_'.$workspaceid.'_'.intval($recursive).'_'.intval($loadObject);
  240. if(false && $REGISTRY->isRegistered($registryKey)){
  241. return $REGISTRY->registry($registryKey);
  242. }else{
  243. if($workspaceid == '') $workspaceid = $USER->workspace;
  244. $liveWorkspaceid = $SETTINGS->get('live_workspace_id');
  245. $pageQuery = '';
  246. $table = 'blocks';
  247. if($this->isPage){
  248. $table = 'pages';
  249. }else{
  250. if($pageId!=''){
  251. $pageQuery = " AND (page_id = '{$pageId}' OR page_id = '{$templatePageid}') ";
  252. }
  253. }
  254. $id = $DB->escape($id);
  255. $q = "SELECT * FROM {$CONFIG->dbprefix}{$table} WHERE id = '{$id}' {$pageQuery} AND (workspace_id = '{$workspaceid}' OR workspace_id = '{$liveWorkspaceid}')";
  256. $details = $DB->fetchRow($q);
  257. if(!is_array($details) || count($details)==0){
  258. return false;
  259. }
  260. $this->setDetails($details);
  261. $this->pageid = $pageId;
  262. $this->saved = true;
  263. if(!$this->isPage && $loadObject){
  264. $this->loadObject();
  265. $this->defaultContent = $this->object->getDefaultContent($this->moduleAction);
  266. }
  267. $this->title = $CORE->getContentLang('heading', $this->id, '', $this->workspaceid);
  268. if($recursive){
  269. $els = array();
  270. $parentId = $id;
  271. if($this->isPage){
  272. $parentId = 'jfxpage-'.$id;
  273. }
  274. $pageWhere = " AND page_id = '{$pageId}' ";
  275. if($templatePageid != $pageId){
  276. $pageWhere = " AND (page_id = '{$pageId}' OR page_id = '{$templatePageid}') ";
  277. }
  278. $q = "SELECT * FROM {$CONFIG->dbprefix}blocks WHERE parent_id = '{$parentId}'
  279. AND (workspace_id = '{$workspaceid}' OR workspace_id = '{$liveWorkspaceid}') {$pageWhere} ORDER BY
  280. sorting ASC";
  281. $tmpEls = $DB->fetchAll($q);
  282. if(!is_array($tmpEls)) $tmpEls = array();
  283. foreach($tmpEls as $k=>$v){
  284. if($this->getDetails('is_template') >0){
  285. $el = new JFX_Template;
  286. }else{
  287. $el = new JFX_Block;
  288. }
  289. $el->loadById($v['id'], $pageId, $workspaceid, $recursive, $loadObject);
  290. $els[] = $el;
  291. }
  292. if(!$this->isPage && strlen($this->getDetails('template_id'))>0){
  293. if(strlen($this->getDetails('template_id'))>0){
  294. $tmpBlock = JFX_Block::getBlockById($this->getDetails('template_id'), $pageId, $liveWorkspaceid, $recursive, $loadObject);
  295. }
  296. foreach($tmpBlock->elements as $el){
  297. $newDetails = array(
  298. 'page_id'=>$pageId
  299. );
  300. $el->setDetails($newDetails);
  301. $els[] = $el;
  302. }
  303. }
  304. $this->elements = $els;
  305. }else{
  306. $this->elements = array();
  307. }
  308. $hookParams = array('block'=>&$this);
  309. JFX::hook('JFX_Core.getBlockById', $hookParams);
  310. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After load by id '.$id);
  311. $REGISTRY->register($registryKey, $this);
  312. return true;
  313. }
  314. }
  315. public function hasConfig($keyname){
  316. return $this->config->has($keyname);
  317. }
  318. public function setConfig($keyname, $parm){
  319. return $this->config->set($keyname, $parm);
  320. }
  321. public function getConfig($keyname){
  322. return $this->config->get($keyname);
  323. if(in_array($keyname, $this->config)){
  324. return $this->config[$keyname];
  325. }else if(in_array($keyname, $this->details)){
  326. return $this->details[$keyname];
  327. }
  328. }
  329. public function __call($methodname, $parms){
  330. return $this->getConfig($methodname);
  331. }
  332. public static function isTemplate($blockId){
  333. $DB = JFX::registry('db');
  334. $CONFIG = JFX::registry('config');
  335. $SETTINGS = JFX::registry('JFX_Settings');
  336. if(substr($blockId, 0, 9)=='jfxpage-'.$SETTINGS->get('template_page_id')){
  337. return true;
  338. }
  339. }
  340. public function prepare(){
  341. $SMARTY = JFX::registry('Smarty');
  342. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before prepare '.$this->id);
  343. $REG = JFX::registry('JFX_Block_Registry');
  344. $CACHE = JFX::registry('JFX_Block_Cache');
  345. $VIEW = JFX::registry('JFX_View');
  346. $SMARTY = JFX::registry('Smarty');
  347. $DB = JFX::registry('db');
  348. $CONFIG = JFX::registry('config');
  349. $USER = JFX::registry('JFX_User');
  350. $CRYPT = JFX::registry('JFX_Crypt');
  351. $LINK = JFX::registry('JFX_Link');
  352. $THEME = JFX::registry('JFX_Theme');
  353. $CORE = JFX::registry('JFX_Core');
  354. $SETTINGS = JFX::registry('JFX_Settings');
  355. if(false && $CACHE->isCached($this->id.'_'.$this->workspaceid)){
  356. $content = $CACHE->get($this->id.'_'.$this->workspaceid);
  357. }else{
  358. if(!is_object($this->object)) $this->loadObject();
  359. $tempid = $this->id;
  360. if(trim($this->details['template_id'])!=''){
  361. // its a template being mirrored
  362. $this->details['id'] = $this->details['template_id'];
  363. $content = $this->object->prepare($this->details);
  364. }else{
  365. // not a template being mirrored
  366. $content = $this->object->prepare($this->details);
  367. }
  368. $hookParams = array(
  369. 'content' => &$content,
  370. 'block' => &$this
  371. );
  372. JFX::hook('JFX_Block.prepareContent', $hookParams);
  373. if(strstr($this->details['template'], ':|content|:')!==false) $content = str_replace(':|content|:', $content, $this->details['template']);
  374. if($this->object->useCache($this->details)) $CACHE->set($this->id.'_'.$this->workspaceid, $content);
  375. }
  376. $content = JFX::fetchSmarty($content);
  377. $REG->register('render_'.$this->id, $content);
  378. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After prepare '.$this->id);
  379. }
  380. public function render(){
  381. global $JFXMemDebug;
  382. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before render '.$this->id);
  383. $REG = JFX::registry('JFX_Block_Registry');
  384. if(!$REG->isRegistered('render_'.$this->id)){
  385. if(!is_object($this->object)) $this->loadObject();
  386. $this->prepare();
  387. }
  388. $hookParams = array('block'=>$this);
  389. JFX::hook('JFX_Block.renderPre', $hookParams);
  390. $output = '
  391. '/*<!-- start #'.$this->id.'-->*/.'
  392. <div id="'.$this->id.'" class="'.$this->getClassName().'" style="'.$this->details['block_style'].'">
  393. ';
  394. $output .= ' '.$REG->registry('render_'.$this->id).'
  395. ';
  396. foreach($this->elements as $k=>$v){
  397. $output .= '
  398. '.$v->render().'
  399. ';
  400. }
  401. $output .= '
  402. </div>
  403. '/*<!-- end #'.$this->id.'-->*/.'
  404. ';
  405. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After render '.$this->id);
  406. return $output;
  407. }
  408. public static function newModuleBlock($moduleKey, $moduleAction, $workspaceid=''){
  409. $DB = JFX::registry('db');
  410. $CONFIG = JFX::registry('config');
  411. if(!JFX_Module::isValidModule($moduleKey)) return false;
  412. if($workspaceid==''){
  413. $USER = JFX::registry('JFX_User');
  414. $workspaceid = $USER->workspace;
  415. }
  416. $block = new JFX_Block;
  417. $block->workspaceid = $workspaceid;
  418. $block->moduleKey = $moduleKey;
  419. $block->moduleAction = $moduleAction;
  420. $block->setDetails(array('workspace_id' => $workspaceid));
  421. $block->loadObject();
  422. $block->details = array_merge($block->details, $block->object->getDefaultDetails($moduleAction));
  423. $block->setTitle($block->object->getDefaultContent($moduleAction));
  424. $block->details = $block->details;
  425. return $block;
  426. }
  427. public function getDetails($keyname=''){
  428. $details = $this->details;
  429. if($keyname!=''){
  430. if(array_key_exists($keyname, $details)){
  431. return $details[$keyname];
  432. }
  433. }
  434. $this->loadObject();
  435. $details['default_content'] = $this->defaultContent;
  436. $details['workspace_id'] = $this->workspaceid;
  437. if(!$this->isPage){
  438. $details['content'] = $this->object->getContentRaw($details);
  439. }else{
  440. $details['page_id'] = $this->pageid;
  441. }
  442. $details['title'] = $this->title;
  443. return $details;
  444. }
  445. public function save(){
  446. $DB = JFX::registry('db');
  447. $LANG = JFX::registry('lang');
  448. $CONFIG = JFX::registry('config');
  449. $CORE = JFX::registry('JFX_Core');
  450. //$DB->debug(true);
  451. //$DB->showErrors(true);
  452. if($this->parentid == ''){
  453. throw new exception('Block needs a parent id before it can be saved');
  454. JFX::addError('Block needs a parent id before it can be saved');
  455. return false;
  456. }
  457. if($this->pageid == ''){
  458. JFX::addError('Block needs a page id before it can be saved');
  459. return false;
  460. }
  461. if($this->id == ''){
  462. JFX::addError('Block needs an id before it can be saved');
  463. return false;
  464. }
  465. $numExistingBlocks = $DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$this->id}' AND parent_id = '{$this->parentid}' AND workspace_id = '{$this->workspaceid}'");
  466. if($numExistingBlocks == 0){
  467. // first save, insert
  468. $DB->insert($CONFIG->dbprefix.'blocks', $this->details);
  469. $CORE->updateContentLang('heading', $this->id, $this->title, '', $this->workspaceid);
  470. }else if($numExistingBlocks > 0){
  471. // updating
  472. $DB->update($CONFIG->dbprefix.'blocks', $this->details, "id = '{$this->id}' AND workspace_id = '{$this->workspaceid}' AND page_id = '{$this->pageid}'");
  473. $CORE->updateContentLang('heading', $this->id, $this->title, '', $this->workspaceid);
  474. }
  475. foreach($this->elements as $k=>$el){
  476. $el->save();
  477. }
  478. }
  479. public function setRecursiveDetail($colname, $value){
  480. if(array_key_exists($colname, $this->details)) $this->details[$colname] = $value;
  481. if($colname == 'parent_id') $this->parentid = $value;
  482. if($colname == 'workspace_id') $this->workspaceid = $value;
  483. if($colname == 'module_action') $this->moduleAction = $value;
  484. if($colname == 'id') $this->id = $value;
  485. if($colname == 'page_id') $this->pageid = $value;
  486. foreach($this->elements as $k=>$v){
  487. if(is_object($v)) $this->elements[$k]->setRecursiveDetail($colname, $value);
  488. }
  489. }
  490. public function copy($newPageid, $newWorkspaceid){
  491. if($this->pageid != $newPageid || $this->workspaceid != $newWorkspaceid) $this->saved = false;
  492. $oldWorkspaceid = $this->workspaceid;
  493. $oldPageid = $this->pageid;
  494. $oldBlockid = $this->id;
  495. $this->workspaceid = $newWorkspaceid;
  496. $this->pageid = $newPageid;
  497. $this->setDetails('page_id', $newPageid);
  498. $this->pageid = $newPageid;
  499. $this->workspaceid = $newWorkspaceid;
  500. $this->details['workspace_id'] = $newWorkspaceid;
  501. $ids = explode('-', $oldBlockid);
  502. $this->id = 'jfxpage-'.$newPageid.'-'.$ids[2].'-'.$ids[3];
  503. $this->details['id'] = $this->id;
  504. $pids = explode('-', $this->parentid);
  505. if(count($pids)>2) $this->parentid = 'jfxpage-'.$newPageid.'-'.$pids[2].'-'.$pids[3];
  506. else $this->parentid = 'jfxpage-'.$newPageid;
  507. $this->details['parent_id'] = $this->parentid;
  508. $this->save();
  509. foreach($this->elements as $k=>$v){
  510. if(is_object($v)) $this->elements[$k]->copy($newPageid, $newWorkspaceid);
  511. }
  512. if(!is_object($this->object)) $this->loadObject();
  513. $this->object->copyToNewBlock($oldBlockid, $this->id, $oldWorkspaceid, $newWorkspaceid);
  514. }
  515. public function setTitle($title){
  516. $CORE = JFX::registry('JFX_Core', $language='', $workspace='');
  517. $CORE->updateContentLang('heading', $this->id, $title, $language, $workspace);
  518. }
  519. public function setDetails($details){
  520. if(is_array($details)){
  521. if(array_key_exists('id', $details)){
  522. $this->id = $details['id'];
  523. }
  524. if(array_key_exists('parent_id', $details)){
  525. $this->parentid = $details['parent_id'];
  526. $this->details['parent_id'] = $details['parent_id'];
  527. }
  528. if(array_key_exists('page_id', $details)){
  529. $this->pageid = $details['page_id'];
  530. $this->setDetails('page_id', $details['page_id']);
  531. }
  532. if(array_key_exists('workspace_id', $details)){
  533. $this->workspaceid = $details['workspace_id'];
  534. $this->details['workspace_id'] = $details['workspace_id'];
  535. }
  536. if(array_key_exists('pageid', $details)){
  537. $this->pageid = $details['page_id'];
  538. $this->setDetails('page_id', $details['page_id']);
  539. }
  540. if(array_key_exists('module_key', $details)){
  541. $this->moduleKey = $details['module_key'];
  542. }
  543. if(array_key_exists('module_action', $details)){
  544. $this->moduleAction = $details['module_action'];
  545. }
  546. foreach($this->details as $k=>$v){
  547. if(array_key_exists($k, $details)) $this->details[$k] = $details[$k];
  548. }
  549. if(array_key_exists('title', $details)){
  550. $this->title = $details['title'];
  551. }
  552. return true;
  553. }else if(is_object($details)){
  554. if(isset($details->id)){
  555. $this->id = $details->id;
  556. }
  557. if(isset($details->parent_id)){
  558. $this->parentid = $details->parent_id;
  559. $this->details['parent_id'] = $details->parent_id;
  560. }
  561. if(isset($details->page_id)){
  562. $this->pageid = $details->page_id;
  563. $this->setDetails('page_id', $details->page_id);
  564. }
  565. if(isset($details->workspace_id)){
  566. $this->workspaceid = $details->workspace_id;
  567. $this->details['workspace_id'] = $details->workspace_id;
  568. }
  569. if(isset($details->module_key)){
  570. $this->moduleKey = $details->module_key;
  571. }
  572. if(isset($details->pageid)){
  573. $this->pageid = $details->pageid;
  574. $this->setDetails('page_id', $details->pageid);
  575. }
  576. if(isset($details->module_action)){
  577. $this->moduleAction = $details->module_action;
  578. $this->details['module_action'] = $details->module_action;
  579. }
  580. if(isset($details->title)){
  581. $this->title = $details->title;
  582. }
  583. foreach($this->details as $k=>$v){
  584. if(array_key_exists($k, $details)) $this->details[$k] = $details->$k;
  585. }
  586. return true;
  587. }else{
  588. return false;
  589. }
  590. }
  591. public function moveUp(){
  592. if(!$this->canMoveUp()){
  593. return false;
  594. }
  595. $parentBlock = self::getBlockById($this->details['parent_id'], $this->details['page_id']);
  596. if(count($parentBlock->elements)>0 && array_key_exists(intval($this->details['sorting']), $parentBlock->elements)){
  597. $newSorting = $this->details['sorting']-1;
  598. $parentBlock->elements[$newSorting]->details['sorting'] = $this->details['sorting'];
  599. $parentBlock->elements[$newSorting]->save();
  600. $this->details['sorting'] = $newSorting;
  601. $this->save();
  602. }
  603. }
  604. public function moveDown(){
  605. if(!$this->canMoveDown()){
  606. return false;
  607. }
  608. $parentBlock = self::getBlockById($this->details['parent_id'], $this->details['page_id']);
  609. if(count($parentBlock->elements)>0 && array_key_exists($this->details['sorting'], $parentBlock->elements)){
  610. $newSorting = $this->details['sorting']+1;
  611. $parentBlock->elements[$newSorting]->details['sorting'] = $this->details['sorting'];
  612. $parentBlock->elements[$newSorting]->save();
  613. $this->details['sorting'] = $newSorting;
  614. $this->save();
  615. }
  616. }
  617. public function addModuleBlock($module, $action, $pageid=''){
  618. $DB = JFX::registry('db');
  619. $CONFIG = JFX::registry('config');
  620. if($pageid == '') $pageid = $this->pageid;
  621. $newBlock = JFX_Block::newModuleBlock($module, $action);
  622. $newId = JFX_Block::generateBlockId($pageid, $module);
  623. $parentBlock = JFX_Block::getBlockById($this->parentid, $this->pageid);
  624. $sorting = $DB->countRows($CONFIG->dbprefix.'blocks', "page_id = '{$this->pageid}' AND parent_id = '{$this->parentid}'");
  625. $newBlock->setDetails(
  626. array(
  627. 'id'=>$newId,
  628. 'template_id' => '',
  629. 'parent_id' => $this->id,
  630. 'page_id' => $pageid,
  631. 'sorting' => $sorting
  632. )
  633. );
  634. if($this instanceof JFX_Page){
  635. $newBlock->setDetails(array('page_parent'=>1));
  636. $newBlock->setDetails(array('is_droppable'=>1));
  637. }
  638. $newBlock->save();
  639. $this->elements[] = $newBlock;
  640. $this->save();
  641. }
  642. public function addTemplateBlock($templateId, $pageid='', $workspaceid=''){
  643. $DB = JFX::registry('db');
  644. $CONFIG = JFX::registry('config');
  645. $USER = JFX::registry('JFX_User');
  646. $pageid = ($pageid=='') ? $this->pageid : $pageid;
  647. $newBlock = self::getBlockById($templateId, $pageid, '');
  648. $parentBlock = self::getBlockById($this->parentid, $pageid);
  649. $sorting = $DB->countRows($CONFIG->dbprefix.'blocks', "page_id = '{$this->pageid}' AND parent_id = '{$this->parentid}'");
  650. $newId = self::generateBlockId($this->pageid, $newBlock->moduleKey);
  651. if($pageid=='') $pageid = $this->pageid;
  652. if($workspaceid==''){
  653. $workspaceid = $USER->workspace;
  654. }
  655. $newBlock->setDetails(
  656. array(
  657. 'id'=>$newId,
  658. 'template_id'=>$templateId,
  659. 'parent_id'=>$this->id,
  660. 'workspace_id' => $workspaceid,
  661. 'page_id'=>$pageid,
  662. 'sorting' => $sorting
  663. )
  664. );
  665. $newBlock->setTitle(self::getTitleById($this->id, $this->pageid, '', $this->workspaceid), '*', '*');
  666. $newBlock->save();
  667. $this->elements[] = $newBlock;
  668. $this->save();
  669. $page = JFX_Block::getBlockById($this->pageid, $this->pageid);
  670. $hookParams = array(
  671. 'page' => $page,
  672. 'block' => $this,
  673. 'new_block' => $newBlock,
  674. 'template_id' => $templateId
  675. );
  676. JFX::hook('JFX_Block.addTemplateBlock', $hookParams);
  677. }
  678. public function canMoveUp(){
  679. $canMove = false;
  680. $parentBlock = self::getBlockById($this->details['parent_id'], $this->details['page_id']);
  681. if(count($parentBlock->elements)>0){
  682. if($parentBlock->elements[0]->details['id']!==$this->id){
  683. $canMove = true;
  684. }
  685. }
  686. return $canMove;
  687. }
  688. public function canMoveDown(){
  689. $canMove = false;
  690. $parentBlock = self::getBlockById($this->details['parent_id'], $this->details['page_id']);
  691. if(count($parentBlock->elements)>0){
  692. if($parentBlock->elements[count($parentBlock->elements)-1]->details['id']!==$this->id){
  693. $canMove = true;
  694. }
  695. }
  696. return $canMove;
  697. }
  698. public function canInsert(){
  699. $canInsert = true;
  700. $SETTINGS = JFX::registry('JFX_Settings');
  701. if($this->isPage && $SETTINGS->get('template_page_id')==$this->pageid){
  702. $canInsert = false;
  703. }
  704. if(!$this->isPage && $this->details['is_droppable']<=0){
  705. $canInsert = false;
  706. }
  707. return $canInsert;
  708. }
  709. public function canDelete(){
  710. $canDelete = true;
  711. if(count($this->elements)>0){
  712. $canDelete = false;
  713. }
  714. if(strlen($this->details['template_id'])>0){
  715. $canDelete = true;
  716. }
  717. return $canDelete;
  718. }
  719. public function getElements(){
  720. $els = $this->elements;
  721. foreach($els as $k=>$v){
  722. if(isset($v->object)){
  723. $els[$k]->object->ownerLicense = '';
  724. $els[$k]->object->publicLicense = '';
  725. }
  726. $els[$k]->elements = $els[$k]->getElements();
  727. }
  728. return $els;
  729. }
  730. public function getDetailsRecursive(){
  731. $details = $this->getDetails();
  732. $details['elements'] = array();
  733. $elements = $this->getElements();
  734. foreach($elements as $k=>$v){
  735. $block = new JFX_Block;
  736. $block->loadById($v->id, $this->details['page_id']);
  737. $blockEls = $block->getElements();
  738. if(count($blockEls)>0){
  739. $details['elements'][$k] = $block->getDetailsRecursive();
  740. }else{
  741. $details['elements'][$k] = array_merge($block->getDetails(), array('elements'=>$block->getElements()));
  742. }
  743. }
  744. return $details;
  745. }
  746. public function __toJson(){
  747. // recursive function
  748. $USER = JFX::registry('JFX_User');
  749. $THEME = JFX::registry('JFX_Theme');
  750. $pageid = array_key_exists('page_id', $this->details)
  751. ? $this->details['page_id']
  752. : preg_replace('/[^0-9]/', '', $this->pageid)
  753. ;
  754. $this->action_buttons = $THEME->getBlockActionButtons($this->id, $this->pageid);
  755. $this->title = self::getTitleById($this->id, $this->pageid, '', $USER->workspace);
  756. if(!$this->isPage){
  757. $this->content = $this->object->getContent($this->id, $this->pageid);
  758. }
  759. if(empty($this->elements)){
  760. $this->elements = array();
  761. }
  762. $allBlocks = $this->elements;
  763. foreach($allBlocks as $k=>$block){
  764. $allBlocks[$k] = json_decode($block->__toJson());
  765. }
  766. $block = clone $this;
  767. $block->elements = $allBlocks;
  768. return json_encode($block);
  769. }
  770. }