XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
blocks.php
Go to the documentation of this file.
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 
11 include '../../../mainfile.php';
12 
13 // Deactivate the logger
14 error_reporting(0);
15 $xoopsLogger->activated = false;
16 
17 function response($data, $error=0, $token=0){
18  global $xoopsSecurity;
19 
20  if($token)
21  $data = array_merge($data, array('token'=>$xoopsSecurity->createToken()));
22 
23  $data = array_merge($data, array('error'=>$error));
24 
25  echo json_encode($data);
26  die();
27 
28 }
29 
30 // Check Security settings
31 if(!$xoopsSecurity->checkReferer(1)){
32  response(array('message'=>__('Operation not allowed!','rmcommon')), 1, 0);
33  die();
34 }
35 
36 
40 function insertBlock(){
41  global $xoopsSecurity;
42 
43  $mod = rmc_server_var($_POST, 'module', '');
44  $id = rmc_server_var($_POST, 'block', '');
45  $token = rmc_server_var($_POST, 'XOOPS_TOKEN_REQUEST', '');
46  $canvas = rmc_server_var($_POST, 'canvas', '');
47 
48  if (!$xoopsSecurity->check()){
49  response(array('message'=>__('Sorry, you are not allowed to view this page','rmcommon')), 1, 0);
50  die();
51  }
52 
53  if($mod=='' || $id==''){
54 
55  $data = array(
56  'message' => __('The block specified seems to be invalid. Please try again.','rmcommon')
57  );
58 
59  response($data, 1, 0);
60 
61  }
62  $module = RMFunctions::load_module($mod);
63  if(!$module){
64  response(array('message'=>__('The specified module does not exists!','rmcommon')), 1, 0);
65  }
66 
67  $module->loadInfoAsVar($mod);
68  $blocks = $module->getInfo('blocks');
69  $ms = $module->name().'<br />';
70  $found = false;
71  foreach($blocks as $bk){
72  $str = isset($bk['show_func']) ? $bk['show_func'] : '';
73  $str .= isset($bk['edit_func']) ? $bk['edit_func'] : '';
74  $str .= isset($bk['dir']) ? $bk['dir'] : $mod;
75  $idb = md5($str);
76  if($idb==$id){
77  $found = true;
78  break;
79  }
80  }
81 
82  if(!$found){
83  response(array('message'=>__('The specified block does not exists, please verify your selection.','rmcommon')), 1, 1);
84  }
85 
86  $block = new RMInternalBlock();
87 
88  if ($canvas<=0){
89  $db = XoopsDatabaseFactory::getDatabaseConnection();
90  // Get a default side
91  $sql = "SELECT id_position, name FROM ".$db->prefix("rmc_blocks_positions")." ORDER BY id_position LIMIT 0, 1";
92  $result = $db->query($sql);
93  if ($result)
94  list($canvas, $canvas_name) = $db->fetchRow($result);
95  }
96 
97  $block->setReadGroups(array(0));
98  $block->setVar('name', $bk['name']);
99  $block->setVar('element', $mod);
100  $block->setVar('element_type', $bk['plugin']==1 ? 'plugin' : 'module');
101  $block->setVar('canvas', $canvas);
102  $block->setVar('visible', 0);
103  $block->setVar('type', $bk['type']);
104  $block->setVar('isactive', 1);
105  $block->setVar('dirname', isset($bk['dir']) ? $bk['dir'] : $mod);
106  $block->setVar('file', $bk['file']);
107  $block->setVar('show_func', $bk['show_func']);
108  $block->setVar('edit_func', $bk['edit_func']);
109  $block->setVar('description', $bk['description']);
110  $block->setVar('widget', $id);
111  $block->setVar('options', is_array($bk['options']) ? serialize($bk['options']) : serialize(explode("|", $bk['options'])));
112  $block->setVar('template', $bk['template']);
113  $block->sections(array(0));
114 
115  if(!$block->save()){
116 
117  response(array('message'=>sprintf(__('Block could not be created due to: %s. Please try again!', 'rmcommon'), $block->errors())), 1, 1);
118 
119  }
120 
121  RMEvents::get()->run_event('rmcommon.block.added', $block);
122 
124 
125  $ret = array(
126  'id' => $block->id(),
127  'name' => $block->getVar('name'),
128  'module' => $block->getVar('element'),
129  'description' => $block->getVar('description'),
130  'canvas' => $pos[$canvas],
131  'weight' => $block->getVar('weight'),
132  'message' => __('Block added successfully! Please configure it.','rmcommon')
133  );
134 
135  response($ret, 0, 1);
136  die();
137 }
138 
142 function configure_block(){
144 
145  if (!$xoopsSecurity->check()){
146  response(array('message'=>__('Sorry, you are not allowed to view this page','rmcommon')), 1, 0);
147  die();
148  }
149 
150  $id = rmc_server_var($_POST, 'block', 0);
151  $mod = rmc_server_var($_POST, 'module', '');
152 
153  if($id<=0){
154  response(array('message'=>__('The block that you specified seems to be invalid. Please try again', 'rmcommon')), 1, 1);
155  }
156 
157  $block = new RMInternalBlock($id);
158  if($block->isNew()){
159  response(array('message'=>__('The block that you specified does not exists!. Please try again', 'rmcommon')), 1, 1);
160  }
161 
162  $positions = RMBlocksFunctions::block_positions();
163  $form = new RMForm('','','');
164  $canvas = new RMFormModules('', 'bk_mod', 1, 1, $block->sections(), 3, null, false, 1);
165 
166  // Groups
167  $groups = new RMFormGroups('', 'bk_groups', true, 1, 3, $block->readGroups());
168 
169  $block_options = $block->getOptions();
170 
171  ob_start();
172  include RMTemplate::get()->get_template('rmc_block_form.php', 'module', 'rmcommon');
173  $form = ob_get_clean();
174 
175  $ret = array(
176  'id'=>$block->id(),
177  'content'=>$form,
178  );
179  response($ret, 0, 1);
180 
181  die();
182 
183 }
184 
185 function save_block_config(){
186  global $xoopsSecurity;
187 
188  foreach($_POST as $k => $v){
189  $$k = $v;
190  }
191 
192  if(!$xoopsSecurity->check($XOOPS_TOKEN_REQUEST)){
193  response(array('message'=>__('Session token expired. Please try again.','rmcommon')), 1, 0);
194  die();
195  }
196 
197  if($bid<=0){
198  response(array('message'=>__('You must provide a block ID!','rmcommon')), 1, 1);
199  die();
200  }
201 
202  $block = new RMInternalBlock($bid);
203  if($block->isNew()){
204  response(array('message'=>__('Specified block does not exists!','rmcommon')), 1, 1);
205  die();
206  }
207 
208  if(isset($options)) $block->setVar('options', serialize($options));
209  $block->setVar('name', $bk_name);
210  $block->setVar('canvas', $bk_pos);
211  $block->setVar('weight', $bk_weight);
212  $block->setVar('visible', $bk_visible);
213  $block->setVar('bcachetime', $bk_cache);
214  if(isset($bk_content)){
215  $block->setVar('content', $bk_content);
216  $block->setVar('content_type', $bk_ctype);
217  }
218 
219  // Set modules
220  $block->sections($bk_mod);
221  // Set Groups
222  $block->setReadGroups($bk_groups);
223 
224  if($block->save()){
225  response(array(
226  'message' => __('Block updated successfully!','rmcommon')
227  ), 0, 1);
228  }
229 
230  die();
231 
232 }
233 
234 
236  global $xoopsSecurity;
237 
238  if(!$xoopsSecurity->check($XOOPS_TOKEN_REQUEST)){
239  response(array('message'=>__('Session token expired. Please try again.','rmcommon')), 1, 0);
240  die();
241  }
242 
243  $id = rmc_server_var($_POST, 'id', 0);
244  $name = rmc_server_var($_POST, 'name', '');
245  $tag = rmc_server_var($_POST, 'tag', '');
246  $active = rmc_server_var($_POST, 'active', 1);
247 
248  if($id<=0){
249  response(array('message'=>__('Specified position is not valid!','rmcommon')), 1, 1);
250  die();
251  }
252 
253  if($name==''||$tag==''){
254  response(array('message'=>__('You must fill name and tag input fields!','rmcommon')), 1, 1);
255  die();
256  }
257 
258  $pos = new RMBlockPosition($id);
259  if($pos->isNew()){
260  response(array('message'=>__('Specified block position does not exists!','rmcommon')), 1, 1);
261  die();
262  }
263 
264  $db = XoopsDatabaseFactory::getDatabaseConnection();
265  $sql = "SELECT COUNT(*) FROM ".$db->prefix("rmc_blocks_positions")." WHERE (name='$name' OR tag='$tag') AND id_position<>$id";
266 
267  list($num) = $db->fetchRow($db->query($sql));
268 
269  if($num>0){
270  response(array('message'=>__('Already exists another block position with same name or tag!','rmcommon')), 1, 1);
271  die();
272  }
273 
274  $pos->setVar('name', $name);
275  $pos->setVar('tag', $tag);
276  $pos->setVar('active', $active);
277 
278  if($pos->save()){
279  response(array('message'=>__('Changes saved successfully!','rmcommon')), 0, 1);
280  die();
281  } else {
282  response(array('message'=>__('Changes could not be saved!','rmcommon')), 1, 1);
283  die();
284  }
285 
286 }
287 
291 function save_block_order(){
292  global $xoopsSecurity;
293 
294  if(!$xoopsSecurity->check())
295  response(array('message'=>__('Session token expired!','rmcommon')), 1, 0);
296 
297  parse_str(rmc_server_var($_POST, 'items', ''));
298 
299  if(!isset($item))
300  response(array('message'=>__('No items has been specified!','rmcommon')), 1, 1);
301 
302  $db = XoopsDatabaseFactory::getDatabaseConnection();
303 
304  $i = 0;
305  foreach($item as $id => $v){
306  $sql = "UPDATE ".$db->prefix("rmc_blocks")." SET weight=$i WHERE bid=$id";
307  $db->queryF($sql);
308  $i++;
309  }
310 
311  showMessage(__('Blocks order saved successfully!','rmcommon'), 0);
312 
313  response(array(
314  'message' => __('Changes saved successfully!','rmcommon')
315  ), 0, 1);
316 
317 }
318 
319 
320 $action = rmc_server_var($_REQUEST, 'action', '');
321 
322 switch($action){
323  case 'insert':
324  insertBlock();
325  break;
326  case 'settings':
327  configure_block();
328  break;
329  case 'saveconfig':
331  break;
332  case 'savepos':
334  break;
335  case 'save_orders':
337  break;
338 }