PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/jsrewrite/jfx-private/classes/JFX/Block.php

http://jfxcms.googlecode.com/
PHP | 520 lines | 402 code | 83 blank | 35 comment | 80 complexity | c9a9623de82c3239176bbe3c8b10f414 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. ############### COPYLEFT GPLv3 LICENSE ###############
  3. ##
  4. ## JFX Version 0.2.7
  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. public $elements = array();
  22. public $details = array(
  23. 'id' => '',
  24. 'parent_id' => '',
  25. 'module_key' => '',
  26. 'module_action' => '',
  27. 'sorting' => '',
  28. 'page_parent' => '',
  29. 'class_name' => '',
  30. 'ajax_enabled' => '',
  31. 'is_droppable' => '',
  32. 'page_id' => '',
  33. 'exec_order' => '',
  34. 'template' => '',
  35. 'template_id' => '',
  36. 'block_style' => '',
  37. 'php_code' => '',
  38. 'is_template' => 0,
  39. 'template_script' => '',
  40. 'template_style' => ''
  41. );
  42. public $id = '';
  43. public $parentid = '';
  44. public $pageid = '';
  45. public $workspaceid = '';
  46. public $moduleKey = '';
  47. public $moduleAction = '';
  48. public $object = NULL;
  49. public $title = 'New Block';
  50. public $defaultContent = '';
  51. public $saved = false;
  52. public function __construct(){
  53. }
  54. public function setClassName($className){
  55. $this->details['class_name'] = $className;
  56. if(is_object($this->object) && method_exists('setClassName', $this->object))
  57. $this->object->setClassName($className, $this->details);
  58. }
  59. public function getClassName(){
  60. $className = $this->details['class_name'];
  61. if(is_object($this->object) && method_exists('getClassName', $this->object))
  62. $className .= $this->object->getClassName($this->details);
  63. return $className;
  64. }
  65. public function setTemplate($template){
  66. if(is_object($this->object) && method_exists('setTemplate', $this->object))
  67. $template = $this->object->setTemplate($template, $this->details);
  68. $this->details['template'] = $template;
  69. }
  70. public function getTemplate(){
  71. return $this->details['template'];
  72. }
  73. public function setStyle($style){
  74. $this->details['block_style'] = $style;
  75. }
  76. public function delete(){
  77. $DB = JFX::registry('db');
  78. $CONFIG = JFX::registry('config');
  79. $LANG = JFX::registry('lang');
  80. foreach($this->elements as $k=>$v){
  81. $v->delete();
  82. };
  83. $id = explode('-', $this->id);
  84. $pageid = $id[2];
  85. $moduleKey = $id[4];
  86. $instanceid = $id[5];
  87. $params = array(
  88. 'page_id' => $this->pageid,
  89. 'workspace_id' => $this->workspaceid,
  90. 'instance_id' => $instanceid
  91. );
  92. if(!is_object($this->object)) $this->loadObject();
  93. $this->object->delete($this->details);
  94. $DB->delete($CONFIG->dbprefix.'blocks', "id = '{$this->id}' AND workspace_id = '{$this->workspaceid}'");
  95. $LANG->deleteContent('core', 'block_title', $this->id, '*', $this->workspaceid);
  96. }
  97. public function clearCache(){
  98. $CACHE = JFX::registry('JFX_Block_Cache');
  99. $CACHE->clear($this->id.'_'.$this->workspaceid);
  100. }
  101. public function loadObject(){
  102. if($this->moduleKey == '') $this->moduleKey = 'content';
  103. if(!is_object($this->object)) $this->object = JFX::registry('JFX_Module_'.ucfirst(strtolower($this->moduleKey)));
  104. $this->object->setBlockId($this->id);
  105. return $this->object;
  106. }
  107. public function loadById($id, $workspaceid = '', $recursive = true, $loadObject = true){
  108. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before load by id '.$id);
  109. $DB = JFX::registry('db');
  110. $LANG = JFX::registry('lang');
  111. $USER = JFX::registry('JFX_User');
  112. $CONFIG = JFX::registry('config');
  113. if($workspaceid == '') $workspaceid = $USER->workspace;
  114. $id = $DB->escape($id);
  115. $details = $DB->fetchRow("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE id = '{$id}' AND workspace_id = '{$workspaceid}'");
  116. if(!is_array($details) || count($details)==0) return false;
  117. $this->details = $details;
  118. $this->id = $details['id'];
  119. $this->parentid = $details['parent_id'];
  120. $this->moduleKey = $details['module_key'];
  121. $this->moduleAction = $details['module_action'];
  122. $this->workspaceid = $details['workspace_id'];
  123. $this->pageid = $details['page_id'];
  124. $this->saved = true;
  125. if($loadObject){
  126. $this->loadObject();
  127. $this->defaultContent = $this->object->getDefaultContent($this->moduleAction);
  128. }
  129. $this->title = $LANG->getContent('core', 'block_title', $this->id, '', $this->workspaceid);
  130. if($recursive){
  131. $els = array();
  132. $tmpEls = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE parent_id = '{$id}' AND workspace_id = '{$workspaceid}' ORDER BY sorting ASC");
  133. if(!is_array($tmpEls)) $tmpEls = array();
  134. foreach($tmpEls as $k=>$v){
  135. $el = new JFX_Block;
  136. $el->loadById($v['id'], $workspaceid, $recursive, $loadObject);
  137. $els[] = $el;
  138. }
  139. $this->elements = $els;
  140. }else{
  141. $this->elements = array();
  142. }
  143. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After load by id '.$id);
  144. return true;
  145. }
  146. public function prepare(){
  147. $SMARTY = JFX::registry('Smarty');
  148. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before prepare '.$this->id);
  149. $REG = JFX::registry('JFX_Block_Registry');
  150. $CACHE = JFX::registry('JFX_Block_Cache');
  151. $VIEW = JFX::registry('JFX_View');
  152. $SMARTY = JFX::registry('Smarty');
  153. $DB = JFX::registry('db');
  154. $CONFIG = JFX::registry('config');
  155. $USER = JFX::registry('JFX_User');
  156. $CRYPT = JFX::registry('JFX_Crypt');
  157. $LINK = JFX::registry('JFX_Link');
  158. $THEME = JFX::registry('JFX_Theme');
  159. $CORE = JFX::registry('JFX_Core');
  160. $SETTINGS = JFX::registry('JFX_Settings');
  161. if(false && $CACHE->isCached($this->id.'_'.$this->workspaceid)){
  162. $content = $CACHE->get($this->id.'_'.$this->workspaceid);
  163. }else{
  164. if(!is_object($this->object)) $this->loadObject();
  165. $tempid = $this->id;
  166. if(trim($this->details['template_id'])!=''){
  167. // its a template being mirrored
  168. $this->details['id'] = $this->details['template_id'];
  169. $content = $this->object->prepare($this->details);
  170. }else{
  171. // not a template being mirrored
  172. $content = $this->object->prepare($this->details);
  173. }
  174. if(strstr($this->details['template'], ':|content|:')!==false) $content = str_replace(':|content|:', $content, $this->details['template']);
  175. if($this->object->useCache($this->details)) $CACHE->set($this->id.'_'.$this->workspaceid, $content);
  176. }
  177. $content = JFX::fetchSmarty($content);
  178. $REG->register('render_'.$this->id, $content);
  179. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After prepare '.$this->id);
  180. }
  181. public function render(){
  182. global $JFXMemDebug;
  183. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'Before render '.$this->id);
  184. $REG = JFX::registry('JFX_Block_Registry');
  185. $output = '
  186. '/*<!-- start #'.$this->id.'-->*/.'
  187. <div id="'.$this->id.'" class="'.$this->getClassName().'" style="'.$this->details['block_style'].'">
  188. ';
  189. if(!$REG->isRegistered('render_'.$this->id)){
  190. if(!is_object($this->object)) $this->loadObject();
  191. $this->prepare();
  192. }
  193. $output .= ' '.$REG->registry('render_'.$this->id).'
  194. ';
  195. foreach($this->elements as $k=>$v){
  196. $output .= '
  197. '.$v->render().'
  198. ';
  199. }
  200. $output .= '
  201. </div>
  202. '/*<!-- end #'.$this->id.'-->*/.'
  203. ';
  204. $JFXMemDebug[] = array('bytes'=>memory_get_usage(), 'file'=>__FILE__, 'line'=>__LINE__, 'time' => getmicrotime(), 'desc' => 'After render '.$this->id);
  205. return $output;
  206. }
  207. public static function newModuleBlock($moduleKey, $moduleAction){
  208. $DB = JFX::registry('db');
  209. $CONFIG = JFX::registry('config');
  210. if(!JFX_Module::isValidModule($moduleKey)) return false;
  211. $block = new JFX_Block;
  212. $block->moduleKey = $moduleKey;
  213. $block->moduleAction = $moduleAction;
  214. $block->loadObject();
  215. $block->details = array_merge($block->details, $block->object->getDefaultDetails($moduleAction));
  216. return $block;
  217. }
  218. public function getDetails(){
  219. $details = $this->details;
  220. $details['default_content'] = $this->defaultContent;
  221. $details['title'] = $this->title;
  222. return $details;
  223. }
  224. public function save(){
  225. $DB = JFX::registry('db');
  226. $LANG = JFX::registry('lang');
  227. $CONFIG = JFX::registry('config');
  228. //$DB->debug(true);
  229. //$DB->showErrors(true);
  230. if($this->parentid == ''){
  231. JFX::addError('Block needs a parent id before it can be saved');
  232. return false;
  233. }
  234. if($this->pageid == ''){
  235. JFX::addError('Block needs a page id before it can be saved');
  236. return false;
  237. }
  238. if($this->id == ''){
  239. JFX::addError('Block needs an id before it can be saved');
  240. return false;
  241. }
  242. $numExistingBlocks = $DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$this->id}' AND parent_id = '{$this->parentid}' AND workspace_id = '{$this->workspaceid}'");
  243. if($numExistingBlocks == 0){
  244. // first save, insert
  245. $DB->insert($CONFIG->dbprefix.'blocks', $this->details);
  246. $LANG->updateContent('core', 'block_title', $this->id, $this->title, '', $this->workspaceid);
  247. }else if($numExistingBlocks > 0){
  248. // updating
  249. $DB->update($CONFIG->dbprefix.'blocks', $this->details, "id = '{$this->id}' AND workspace_id = '{$this->workspaceid}' AND page_id = '{$this->pageid}'");
  250. /*
  251. echo '<pre>';
  252. var_dump($this->details);
  253. echo '</pre>';
  254. *
  255. */
  256. $LANG->updateContent('core', 'block_title', $this->id, $this->title, '', $this->workspaceid);
  257. }
  258. foreach($this->elements as $k=>$el){
  259. $el->save();
  260. }
  261. }
  262. public function setRecursiveDetail($colname, $value){
  263. if(array_key_exists($colname, $this->details)) $this->details[$colname] = $value;
  264. if($colname == 'parent_id') $this->parentid = $value;
  265. if($colname == 'workspace_id') $this->workspaceid = $value;
  266. if($colname == 'module_action') $this->moduleAction = $value;
  267. if($colname == 'id') $this->id = $value;
  268. if($colname == 'page_id') $this->pageid = $value;
  269. foreach($this->elements as $k=>$v){
  270. if(is_object($v)) $this->elements[$k]->setRecursiveDetail($colname, $value);
  271. }
  272. }
  273. public function copy($newPageid, $newWorkspaceid){
  274. if($this->pageid != $newPageid || $this->workspaceid != $newWorkspaceid) $this->saved = false;
  275. $oldWorkspaceid = $this->workspaceid;
  276. $oldPageid = $this->pageid;
  277. $oldBlockid = $this->id;
  278. $this->workspaceid = $newWorkspaceid;
  279. $this->pageid = $newPageid;
  280. $this->details['page_id'] = $newPageid;
  281. $this->pageid = $newPageid;
  282. $this->workspaceid = $newWorkspaceid;
  283. $this->details['workspace_id'] = $newWorkspaceid;
  284. $ids = explode('-', $oldBlockid);
  285. $this->id = 'jfxpage-'.$newPageid.'-'.$ids[2].'-'.$ids[3];
  286. $this->details['id'] = $this->id;
  287. $pids = explode('-', $this->parentid);
  288. if(count($pids)>2) $this->parentid = 'jfxpage-'.$newPageid.'-'.$pids[2].'-'.$pids[3];
  289. else $this->parentid = 'jfxpage-'.$newPageid;
  290. $this->details['parent_id'] = $this->parentid;
  291. $this->save();
  292. foreach($this->elements as $k=>$v){
  293. if(is_object($v)) $this->elements[$k]->copy($newPageid, $newWorkspaceid);
  294. }
  295. if(!is_object($this->object)) $this->loadObject();
  296. $this->object->copyToNewBlock($oldBlockid, $this->id, $oldWorkspaceid, $newWorkspaceid);
  297. }
  298. public function setDetails($details){
  299. if(is_array($details)){
  300. if(array_key_exists('id', $details)){
  301. $this->id = $details['id'];
  302. }
  303. if(array_key_exists('parent_id', $details)){
  304. $this->parentid = $details['parent_id'];
  305. $this->details['parent_id'] = $details['parent_id'];
  306. }
  307. if(array_key_exists('page_id', $details)){
  308. $this->pageid = $details['page_id'];
  309. $this->details['page_id'] = $details['page_id'];
  310. }
  311. if(array_key_exists('workspace_id', $details)){
  312. $this->workspaceid = $details['workspace_id'];
  313. $this->details['workspace_id'] = $details['workspace_id'];
  314. }
  315. if(array_key_exists('pageid', $details)){
  316. $this->pageid = $details['page_id'];
  317. $this->details['page_id'] = $details['page_id'];
  318. }
  319. if(array_key_exists('module_key', $details)){
  320. $this->moduleKey = $details['module_key'];
  321. }
  322. if(array_key_exists('module_action', $details)){
  323. $this->moduleAction = $details['module_action'];
  324. }
  325. foreach($this->details as $k=>$v){
  326. if(array_key_exists($k, $details)) $this->details[$k] = $details[$k];
  327. }
  328. if(array_key_exists('title', $details)){
  329. $this->title = $details['title'];
  330. }
  331. return true;
  332. }else if(is_object($details)){
  333. if(isset($details->id)){
  334. $this->id = $details->id;
  335. }
  336. if(isset($details->parent_id)){
  337. $this->parentid = $details->parent_id;
  338. $this->details['parent_id'] = $details->parent_id;
  339. }
  340. if(isset($details->page_id)){
  341. $this->pageid = $details->page_id;
  342. $this->details['page_id'] = $details->page_id;
  343. }
  344. if(isset($details->workspace_id)){
  345. $this->workspaceid = $details->workspace_id;
  346. $this->details['workspace_id'] = $details->workspace_id;
  347. }
  348. if(isset($details->module_key)){
  349. $this->moduleKey = $details->module_key;
  350. }
  351. if(isset($details->pageid)){
  352. $this->pageid = $details->pageid;
  353. $this->details['page_id'] = $details->pageid;
  354. }
  355. if(isset($details->module_action)){
  356. $this->moduleAction = $details->module_action;
  357. $this->details['module_action'] = $details->module_action;
  358. }
  359. if(isset($details->title)){
  360. $this->title = $details->title;
  361. }
  362. foreach($this->details as $k=>$v){
  363. if(array_key_exists($k, $details)) $this->details[$k] = $details->$k;
  364. }
  365. return true;
  366. }else{
  367. return false;
  368. }
  369. }
  370. public function getElements(){
  371. $els = $this->elements;
  372. foreach($els as $k=>$v){
  373. //echo 'processing '.$v->id.'<br />';
  374. if(isset($v->object)){
  375. $els[$k]->object->ownerLicense = '';
  376. $els[$k]->object->publicLicense = '';
  377. }
  378. $els[$k]->elements = $els[$k]->getElements();
  379. }
  380. return $els;
  381. }
  382. public function getDetailsRecursive(){
  383. $details = $this->getDetails();
  384. $details['elements'] = array();
  385. $elements = $this->getElements();
  386. foreach($elements as $k=>$v){
  387. $block = new JFX_Block;
  388. $block->loadById($v->id);
  389. $blockEls = $block->getElements();
  390. if(count($blockEls)>0){
  391. $details['elements'][$k] = $block->getDetailsRecursive();
  392. }else{
  393. $details['elements'][$k] = array_merge($block->getDetails(), array('elements'=>$block->getElements()));
  394. }
  395. }
  396. return $details;
  397. }
  398. public function __toJson(){
  399. // recursive function
  400. $newEls = array();
  401. foreach($this->elements as $k=>$el) $newEls[$k] = $el->__toJson();
  402. $jsonString = '{
  403. "id" : "'.$this->id.'",
  404. "parentid" : "'.$this->parentid.'",
  405. "pageid" : "'.$this->pageid.'",
  406. "module_key" : "'.$this->moduleKey.'",
  407. "details" : {';
  408. $searches = array(
  409. '"',
  410. "\n",
  411. "\r"
  412. );
  413. $replacements = array(
  414. '\"',
  415. '\n',
  416. ''
  417. );
  418. foreach($this->details as $k=>$v){
  419. $v = str_replace($searches, $replacements, $v);
  420. $jsonString .=
  421. '"'.$k.'" : "'.$v.'", ';
  422. }
  423. $jsonString .= '"title" : "'.$this->title.'"';
  424. // $jsonStringRev = strrev($jsonString);
  425. // if(substr($jsonStringRev, 0, 2)==' ,') $jsonStringRev = substr($jsonStringRev, 2);
  426. // $jsonString = strrev($jsonStringRev);
  427. $jsonString .= '}, ';
  428. if(count($newEls)>0) $jsonString .= '"elements": ['.implode(', ', $newEls).'], ';
  429. else $jsonString .= '"elements" : [], ';
  430. $jsonStringRev = strrev($jsonString);
  431. if(substr($jsonStringRev, 0, 2)==' ,') $jsonStringRev = substr($jsonStringRev, 2);
  432. $jsonString = strrev($jsonStringRev);
  433. $jsonString .= '}';
  434. return $jsonString;
  435. }
  436. }