PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/rmcommon/trunk/ajax/blocks.php

http://bitcero-modules.googlecode.com/
PHP | 338 lines | 240 code | 75 blank | 23 comment | 29 complexity | 106f4d68faa69bdf1a307dcb92791a6d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: blocks.php 1037 2012-09-07 21:19:12Z i.bitcero $
  3. // --------------------------------------------------------------
  4. // Red México Common Utilities
  5. // A framework for Red México Modules
  6. // Author: Eduardo Cortés <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. include '../../../mainfile.php';
  11. // Deactivate the logger
  12. error_reporting(0);
  13. $xoopsLogger->activated = false;
  14. function response($data, $error=0, $token=0){
  15. global $xoopsSecurity;
  16. if($token)
  17. $data = array_merge($data, array('token'=>$xoopsSecurity->createToken()));
  18. $data = array_merge($data, array('error'=>$error));
  19. echo json_encode($data);
  20. die();
  21. }
  22. // Check Security settings
  23. if(!$xoopsSecurity->checkReferer(1)){
  24. response(array('message'=>__('Operation not allowed!','rmcommon')), 1, 0);
  25. die();
  26. }
  27. /**
  28. * This function allos to insert a new block in database
  29. */
  30. function insertBlock(){
  31. global $xoopsSecurity;
  32. $mod = rmc_server_var($_POST, 'module', '');
  33. $id = rmc_server_var($_POST, 'block', '');
  34. $token = rmc_server_var($_POST, 'XOOPS_TOKEN_REQUEST', '');
  35. $canvas = rmc_server_var($_POST, 'canvas', '');
  36. if (!$xoopsSecurity->check()){
  37. response(array('message'=>__('Sorry, you are not allowed to view this page','rmcommon')), 1, 0);
  38. die();
  39. }
  40. if($mod=='' || $id==''){
  41. $data = array(
  42. 'message' => __('The block specified seems to be invalid. Please try again.','rmcommon')
  43. );
  44. response($data, 1, 0);
  45. }
  46. $module = RMFunctions::load_module($mod);
  47. if(!$module){
  48. response(array('message'=>__('The specified module does not exists!','rmcommon')), 1, 0);
  49. }
  50. $module->loadInfoAsVar($mod);
  51. $blocks = $module->getInfo('blocks');
  52. $ms = $module->name().'<br />';
  53. $found = false;
  54. foreach($blocks as $bk){
  55. $str = isset($bk['show_func']) ? $bk['show_func'] : '';
  56. $str .= isset($bk['edit_func']) ? $bk['edit_func'] : '';
  57. $str .= isset($bk['dir']) ? $bk['dir'] : $mod;
  58. $idb = md5($str);
  59. if($idb==$id){
  60. $found = true;
  61. break;
  62. }
  63. }
  64. if(!$found){
  65. response(array('message'=>__('The specified block does not exists, please verify your selection.','rmcommon')), 1, 1);
  66. }
  67. $block = new RMInternalBlock();
  68. if ($canvas<=0){
  69. $db = XoopsDatabaseFactory::getDatabaseConnection();
  70. // Get a default side
  71. $sql = "SELECT id_position, name FROM ".$db->prefix("rmc_blocks_positions")." ORDER BY id_position LIMIT 0, 1";
  72. $result = $db->query($sql);
  73. if ($result)
  74. list($canvas, $canvas_name) = $db->fetchRow($result);
  75. }
  76. $block->setReadGroups(array(0));
  77. $block->setVar('name', $bk['name']);
  78. $block->setVar('element', $mod);
  79. $block->setVar('element_type', $bk['plugin']==1 ? 'plugin' : 'module');
  80. $block->setVar('canvas', $canvas);
  81. $block->setVar('visible', 0);
  82. $block->setVar('type', $bk['type']);
  83. $block->setVar('isactive', 1);
  84. $block->setVar('dirname', isset($bk['dir']) ? $bk['dir'] : $mod);
  85. $block->setVar('file', $bk['file']);
  86. $block->setVar('show_func', $bk['show_func']);
  87. $block->setVar('edit_func', $bk['edit_func']);
  88. $block->setVar('description', $bk['description']);
  89. $block->setVar('widget', $id);
  90. $block->setVar('options', is_array($bk['options']) ? serialize($bk['options']) : serialize(explode("|", $bk['options'])));
  91. $block->setVar('template', $bk['template']);
  92. $block->sections(array(0));
  93. if(!$block->save()){
  94. response(array('message'=>sprintf(__('Block could not be created due to: %s. Please try again!', 'rmcommon'), $block->errors())), 1, 1);
  95. }
  96. RMEvents::get()->run_event('rmcommon.block.added', $block);
  97. $pos = RMBlocksFunctions::block_positions();
  98. $ret = array(
  99. 'id' => $block->id(),
  100. 'name' => $block->getVar('name'),
  101. 'module' => $block->getVar('element'),
  102. 'description' => $block->getVar('description'),
  103. 'canvas' => $pos[$canvas],
  104. 'weight' => $block->getVar('weight'),
  105. 'message' => __('Block added successfully! Please configure it.','rmcommon')
  106. );
  107. response($ret, 0, 1);
  108. die();
  109. }
  110. /**
  111. * Return the form to configure blocks
  112. */
  113. function configure_block(){
  114. global $xoopsSecurity, $xoopsLogger;
  115. if (!$xoopsSecurity->check()){
  116. response(array('message'=>__('Sorry, you are not allowed to view this page','rmcommon')), 1, 0);
  117. die();
  118. }
  119. $id = rmc_server_var($_POST, 'block', 0);
  120. $mod = rmc_server_var($_POST, 'module', '');
  121. if($id<=0){
  122. response(array('message'=>__('The block that you specified seems to be invalid. Please try again', 'rmcommon')), 1, 1);
  123. }
  124. $block = new RMInternalBlock($id);
  125. if($block->isNew()){
  126. response(array('message'=>__('The block that you specified does not exists!. Please try again', 'rmcommon')), 1, 1);
  127. }
  128. $positions = RMBlocksFunctions::block_positions();
  129. $form = new RMForm('','','');
  130. $canvas = new RMFormModules('', 'bk_mod', 1, 1, $block->sections(), 3, null, false, 1);
  131. // Groups
  132. $groups = new RMFormGroups('', 'bk_groups', true, 1, 3, $block->readGroups());
  133. $block_options = $block->getOptions();
  134. ob_start();
  135. include RMTemplate::get()->get_template('rmc_block_form.php', 'module', 'rmcommon');
  136. $form = ob_get_clean();
  137. $ret = array(
  138. 'id'=>$block->id(),
  139. 'content'=>$form,
  140. );
  141. response($ret, 0, 1);
  142. die();
  143. }
  144. function save_block_config(){
  145. global $xoopsSecurity;
  146. foreach($_POST as $k => $v){
  147. $$k = $v;
  148. }
  149. if(!$xoopsSecurity->check($XOOPS_TOKEN_REQUEST)){
  150. response(array('message'=>__('Session token expired. Please try again.','rmcommon')), 1, 0);
  151. die();
  152. }
  153. if($bid<=0){
  154. response(array('message'=>__('You must provide a block ID!','rmcommon')), 1, 1);
  155. die();
  156. }
  157. $block = new RMInternalBlock($bid);
  158. if($block->isNew()){
  159. response(array('message'=>__('Specified block does not exists!','rmcommon')), 1, 1);
  160. die();
  161. }
  162. if(isset($options)) $block->setVar('options', serialize($options));
  163. $block->setVar('name', $bk_name);
  164. $block->setVar('canvas', $bk_pos);
  165. $block->setVar('weight', $bk_weight);
  166. $block->setVar('visible', $bk_visible);
  167. $block->setVar('bcachetime', $bk_cache);
  168. if(isset($bk_content)){
  169. $block->setVar('content', $bk_content);
  170. $block->setVar('content_type', $bk_ctype);
  171. }
  172. // Set modules
  173. $block->sections($bk_mod);
  174. // Set Groups
  175. $block->setReadGroups($bk_groups);
  176. if($block->save()){
  177. response(array(
  178. 'message' => __('Block updated successfully!','rmcommon')
  179. ), 0, 1);
  180. }
  181. die();
  182. }
  183. function save_block_position(){
  184. global $xoopsSecurity;
  185. if(!$xoopsSecurity->check($XOOPS_TOKEN_REQUEST)){
  186. response(array('message'=>__('Session token expired. Please try again.','rmcommon')), 1, 0);
  187. die();
  188. }
  189. $id = rmc_server_var($_POST, 'id', 0);
  190. $name = rmc_server_var($_POST, 'name', '');
  191. $tag = rmc_server_var($_POST, 'tag', '');
  192. $active = rmc_server_var($_POST, 'active', 1);
  193. if($id<=0){
  194. response(array('message'=>__('Specified position is not valid!','rmcommon')), 1, 1);
  195. die();
  196. }
  197. if($name==''||$tag==''){
  198. response(array('message'=>__('You must fill name and tag input fields!','rmcommon')), 1, 1);
  199. die();
  200. }
  201. $pos = new RMBlockPosition($id);
  202. if($pos->isNew()){
  203. response(array('message'=>__('Specified block position does not exists!','rmcommon')), 1, 1);
  204. die();
  205. }
  206. $db = XoopsDatabaseFactory::getDatabaseConnection();
  207. $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_blocks_positions")." WHERE (name='$name' OR tag='$tag') AND id_position<>$id";
  208. list($num) = $db->fetchRow($db->query($sql));
  209. if($num>0){
  210. response(array('message'=>__('Already exists another block position with same name or tag!','rmcommon')), 1, 1);
  211. die();
  212. }
  213. $pos->setVar('name', $name);
  214. $pos->setVar('tag', $tag);
  215. $pos->setVar('active', $active);
  216. if($pos->save()){
  217. response(array('message'=>__('Changes saved successfully!','rmcommon')), 0, 1);
  218. die();
  219. } else {
  220. response(array('message'=>__('Changes could not be saved!','rmcommon')), 1, 1);
  221. die();
  222. }
  223. }
  224. /**
  225. * Save blocks orders
  226. */
  227. function save_block_order(){
  228. global $xoopsSecurity;
  229. if(!$xoopsSecurity->check())
  230. response(array('message'=>__('Session token expired!','rmcommon')), 1, 0);
  231. parse_str(rmc_server_var($_POST, 'items', ''));
  232. if(!isset($item))
  233. response(array('message'=>__('No items has been specified!','rmcommon')), 1, 1);
  234. $db = XoopsDatabaseFactory::getDatabaseConnection();
  235. $i = 0;
  236. foreach($item as $id => $v){
  237. $sql = "UPDATE ".$db->prefix("rmc_blocks")." SET weight=$i WHERE bid=$id";
  238. $db->queryF($sql);
  239. $i++;
  240. }
  241. showMessage(__('Blocks order saved successfully!','rmcommon'), 0);
  242. response(array(
  243. 'message' => __('Changes saved successfully!','rmcommon')
  244. ), 0, 1);
  245. }
  246. $action = rmc_server_var($_REQUEST, 'action', '');
  247. switch($action){
  248. case 'insert':
  249. insertBlock();
  250. break;
  251. case 'settings':
  252. configure_block();
  253. break;
  254. case 'saveconfig':
  255. save_block_config();
  256. break;
  257. case 'savepos':
  258. save_block_position();
  259. break;
  260. case 'save_orders':
  261. save_block_order();
  262. break;
  263. }