PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rmcommon/tags/rmcommon-2.1.2b/ajax/blocks.php

http://bitcero-modules.googlecode.com/
PHP | 237 lines | 164 code | 53 blank | 20 comment | 19 complexity | 776e92272ccbdf62a90c8a69f9d6ddb7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id$
  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 $idb => $bk){
  55. if($idb==$id){
  56. $found = true;
  57. break;
  58. }
  59. }
  60. if(!$found){
  61. response(array('message'=>__('The specified block does not exists, please verify your selection.','rmcommon')), 1, 1);
  62. }
  63. $block = new RMBlock();
  64. if ($canvas<=0){
  65. $db = Database::getInstance();
  66. // Get a default side
  67. $sql = "SELECT id_position, name FROM ".$db->prefix("rmc_blocks_positions")." ORDER BY id_position LIMIT 0, 1";
  68. $result = $db->query($sql);
  69. if ($result)
  70. list($canvas, $canvas_name) = $db->fetchRow($result);
  71. }
  72. $block->setVar('name', $bk['name']);
  73. $block->setVar('element', $mod);
  74. $block->setVar('element_type', 'module');
  75. $block->setVar('canvas', $canvas);
  76. $block->setVar('visible', 1);
  77. $block->setVar('type', 'normal');
  78. $block->setVar('isactive', 1);
  79. $block->setVar('dirname', isset($bk['dir']) ? $bk['dir'] : $mod);
  80. $block->setVar('file', $bk['file']);
  81. $block->setVar('show_func', $bk['show_func']);
  82. $block->setVar('edit_func', $bk['edit_func']);
  83. $block->setVar('description', $bk['description']);
  84. $block->setVar('widget', $id);
  85. $block->setVar('options', is_array($bk['options']) ? serialize($bk['options']) : serialize(explode("|", $bk['options'])));
  86. $block->setVar('template', $bk['template']);
  87. if(!$block->save()){
  88. response(array('message'=>sprintf(__('Block could not be created due to: %s. Please try again!', 'rmcommon'), $block->errors())), 1, 1);
  89. }
  90. RMEvents::get()->run_event('rmcommon.block.added', $block);
  91. $pos = RMBlocksFunctions::block_positions();
  92. $ret = array(
  93. 'id' => $block->id(),
  94. 'name' => $block->getVar('name'),
  95. 'module' => $block->getVar('element'),
  96. 'description' => $block->getVar('description'),
  97. 'canvas' => $pos[$canvas],
  98. 'weight' => $block->getVar('weight'),
  99. 'message' => __('Block added successfully! Please configure it.','rmcommon')
  100. );
  101. response($ret, 0, 1);
  102. die();
  103. }
  104. /**
  105. * Return the form to configure blocks
  106. */
  107. function configure_block(){
  108. global $xoopsSecurity;
  109. if (!$xoopsSecurity->check()){
  110. response(array('message'=>__('Sorry, you are not allowed to view this page','rmcommon')), 1, 0);
  111. die();
  112. }
  113. $id = rmc_server_var($_POST, 'block', 0);
  114. $mod = rmc_server_var($_POST, 'module', '');
  115. if($id<=0){
  116. response(array('message'=>__('The block that you specified seems to be invalid. Please try again', 'rmcommon')), 1, 1);
  117. }
  118. $block = new RMBlock($id);
  119. if($block->isNew()){
  120. response(array('message'=>__('The block that you specified does not exists!. Please try again', 'rmcommon')), 1, 1);
  121. }
  122. $positions = RMBlocksFunctions::block_positions();
  123. $form = new RMForm('','','');
  124. $canvas = new RMFormModules('', 'bk_mod', 1, 1, array(0), 3, null, false, 1);
  125. // Groups
  126. $groups = new RMFormGroups('', 'bk_groups', true, 1, 3, $block->readGroups());
  127. $block_options = $block->getOptions();
  128. ob_start();
  129. include RMTemplate::get()->get_template('rmc_block_form.php', 'module', 'rmcommon');
  130. $form = ob_get_clean();
  131. $ret = array(
  132. 'id'=>$block->id(),
  133. 'content'=>$form,
  134. );
  135. response($ret, 0, 1);
  136. die();
  137. }
  138. function save_block_config(){
  139. global $xoopsSecurity;
  140. foreach($_POST as $k => $v){
  141. $$k = $v;
  142. }
  143. if(!$xoopsSecurity->check($XOOPS_TOKEN_REQUEST)){
  144. response(array('message'=>__('Session token expired. Please try again.','rmcommon')), 1, 0);
  145. die();
  146. }
  147. if($bid<=0){
  148. response(array('message'=>__('You must provide a block ID!','rmcommon')), 1, 1);
  149. die();
  150. }
  151. $block = new RMBlock($bid);
  152. if($block->isNew()){
  153. response(array('message'=>__('Specified block does not exists!','rmcommon')), 1, 1);
  154. die();
  155. }
  156. if(isset($options)) $block->setVar('options', serialize($options));
  157. $block->setVar('name', $bk_name);
  158. $block->setVar('canvas', $bk_pos);
  159. $block->setVar('weight', $bk_weight);
  160. $block->setVar('visible', $bk_visible);
  161. $block->setVar('bcachetime', $bk_cache);
  162. // Set modules
  163. $block->sections($bk_mod);
  164. // Set Groups
  165. $block->setReadGroups($bk_groups);
  166. if($block->save()){
  167. response(array(
  168. 'message' => __('Block updated successfully!','rmcommon')
  169. ), 0, 1);
  170. }
  171. die();
  172. }
  173. $action = rmc_server_var($_REQUEST, 'action', '');
  174. switch($action){
  175. case 'insert':
  176. insertBlock();
  177. break;
  178. case 'settings':
  179. configure_block();
  180. break;
  181. case 'saveconfig':
  182. save_block_config();
  183. break;
  184. }