PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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