PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/libraries/datagrid.php

http://rapyd-framework.googlecode.com/
PHP | 460 lines | 364 code | 33 blank | 63 comment | 38 complexity | 3f39209ef171c57bfc963e45e9d93e1f MD5 | raw file
  1. <?php
  2. if (!defined('RAPYD_PATH'))
  3. exit('No direct script access allowed');
  4. /**
  5. * Datagrid library
  6. *
  7. * @package Core
  8. * @author Felice Ostuni
  9. * @copyright (c) 2011 Rapyd Team
  10. * @license http://www.rapyd.com/license
  11. */
  12. class datagrid_library extends dataset_library
  13. {
  14. protected $fields = array();
  15. public $columns = array();
  16. public $actions = array();
  17. public $rows = array();
  18. public $checkbox_form = false;
  19. public $output = "";
  20. public $add_url = "";
  21. public $add_hash = "";
  22. /**
  23. * internal, it build append columns, called only if $config is passed to the constructor
  24. *
  25. * @param type $fields
  26. */
  27. protected function set_columns($columns)
  28. {
  29. foreach ($columns as $column)
  30. {
  31. $this->set_column($column);
  32. }
  33. }
  34. /**
  35. * called automagically by column($column) or cplumn($pattern, $label, $orderby)
  36. * where pattern is fieldname or a string containing html/placeholders/formatting tags like:
  37. * "<ucfirst>{firstnamename}</ucfirst> {lastname}"
  38. *
  39. * @return object column object
  40. */
  41. public function set_column()
  42. {
  43. $column = array();
  44. if (func_num_args() == 3)
  45. {
  46. list($column['pattern'], $column['label'], $column['orderby']) = func_get_args();
  47. }
  48. if (func_num_args() == 2)
  49. {
  50. list($column['pattern'], $column['label']) = func_get_args();
  51. }
  52. if (func_num_args() == 1)
  53. {
  54. $column = func_get_arg(0);
  55. }
  56. //share source with columns
  57. if (!isset($column['source']))
  58. $column['source'] = $this->source;
  59. //detect if is a checkbox column
  60. if (isset($column['checkbox']))
  61. $this->checkbox_form = true;
  62. $column = new datagrid_column($column);
  63. $this->columns[] = $column;
  64. return $column;
  65. }
  66. function action_button($config = null)
  67. {
  68. $caption = (isset($config['caption'])) ? $config['caption'] : rpd::lang('btn.delete');
  69. $action_name = (isset($config['name'])) ? $config['name'] : 'delete';
  70. $action = "javascript:document.forms['grid" . $this->cid . "'].grid_action.value='" . $action_name . "';document.forms['grid" . $this->cid . "'].submit()";
  71. $this->button("btn_" . $action_name, $caption, $action, "TR");
  72. }
  73. // --------------------------------------------------------------------
  74. function add_button($config = null)
  75. {
  76. $caption = (isset($config['caption'])) ? $config['caption'] : rpd::lang('btn.add');
  77. $url = null;
  78. if (isset($config['url']) OR $this->add_url != "")
  79. {
  80. $url = (isset($config['url'])) ? $config['url'] : $this->add_url;
  81. }
  82. $url = rpd_url_helper::append('create' . $this->cid, 1, $url);
  83. if (isset($config['hash']) OR $this->add_hash != "")
  84. {
  85. $url .= '#' . $this->add_hash;
  86. }
  87. $action = "javascript:window.location.href='" . $url . "'";
  88. $this->button("btn_add", $caption, $action, "TR");
  89. }
  90. /**
  91. * detect current dataform "action" checking current url
  92. */
  93. protected function sniff_action()
  94. {
  95. if (isset($_POST['grid_action']))
  96. {
  97. $action = $_POST['grid_action'];
  98. if (isset($this->actions[$action]))
  99. call_user_func($this->actions[$action]);
  100. }
  101. }
  102. // --------------------------------------------------------------------
  103. protected function build_grid()
  104. {
  105. rpd_html_helper::css('datagrid.css');
  106. $data = get_object_vars($this);
  107. $this->build_buttons();
  108. if ($this->checkbox_form)
  109. {
  110. rpd::load('helper', 'form');
  111. $attributes = array('class' => 'form', 'name' => 'grid');
  112. $url = ($this->url != '') ? $this->url : rpd_url_helper::get_url();
  113. $data['form_begin'] = rpd_form_helper::open($url, $attributes);
  114. $data['form_end'] = rpd_form_helper::close();
  115. $data['hidden'] = rpd_form_helper::hidden('grid_action', 'true');
  116. } else
  117. {
  118. $data['hidden'] = "";
  119. $data['form_begin'] = "";
  120. $data['form_end'] = "";
  121. }
  122. $data['container'] = $this->button_containers();
  123. //table rows
  124. foreach ($this->data as $tablerow)
  125. {
  126. unset($row);
  127. foreach ($this->columns as $column)
  128. {
  129. unset($cell);
  130. $column->reset_pattern();
  131. $column->set_row($tablerow);
  132. $cell = get_object_vars($column);
  133. $cell["value"] = $column->get_value();
  134. $cell["type"] = $column->column_type;
  135. $row[] = $cell;
  136. }
  137. $data["rows"][] = $row;
  138. }
  139. $data["pagination"] = $this->pagination;
  140. $data["total_rows"] = $this->total_rows;
  141. return rpd::view('datagrid', $data);
  142. }
  143. // --------------------------------------------------------------------
  144. protected function build_excel()
  145. {
  146. $filename = $this->label . ".xls";
  147. header("Content-Type: application/vnd.ms-excel");
  148. header("Content-Disposition: inline; filename=$filename");
  149. $data = get_object_vars($this);
  150. //table rows
  151. foreach ($this->data as $tablerow)
  152. {
  153. unset($row);
  154. foreach ($this->columns as $column)
  155. {
  156. unset($cell);
  157. $column->reset_pattern();
  158. $column->set_row($tablerow);
  159. $cell = get_object_vars($column);
  160. $cell["value"] = $column->get_value();
  161. $cell["type"] = $column->column_type;
  162. $row[] = $cell;
  163. }
  164. $data["rows"][] = $row;
  165. }
  166. $data["total_rows"] = $this->total_rows;
  167. return rpd::view('datagrid_excel', $data);
  168. }
  169. // --------------------------------------------------------------------
  170. protected function build_csv()
  171. {
  172. $output = '';
  173. $filename = preg_replace('/[^0-9a-z\_\-]/i', '', $this->label) . ".csv";
  174. header('Pragma: private');
  175. header('Cache-control: private, must-revalidate');
  176. header("Content-type: application/csv");
  177. header("Content-Disposition: attachment; filename=" . $filename);
  178. $data = get_object_vars($this);
  179. foreach ($this->columns as $column)
  180. {
  181. $labels[] = $column->label;
  182. }
  183. $output.= '"' . implode('";"', $labels) . '"' . "\n";
  184. //rows
  185. foreach ($this->data as $tablerow)
  186. {
  187. unset($values);
  188. foreach ($this->columns as $column)
  189. {
  190. $column->reset_pattern();
  191. $column->set_row($tablerow);
  192. $values[] = str_replace('"', '""', strip_tags($column->get_value())); //quota " come "" (notazione excel)
  193. }
  194. $rows[] = '"' . implode('";"', $values) . '"';
  195. }
  196. //$output.= implode("\n", $rows) . "\n";
  197. echo $output.= implode("\n", $rows) . "\n";
  198. die;
  199. //return mb_convert_encoding($output, 'iso-8859-1', 'utf-8');
  200. }
  201. // --------------------------------------------------------------------
  202. public function build($method = 'grid')
  203. {
  204. parent::build();
  205. //sniff and perform action
  206. $this->sniff_action();
  207. foreach ($this->columns as & $column)
  208. {
  209. if (isset($column->orderby))
  210. {
  211. $column->orderby_asc_url = $this->orderby_link($column->orderby, 'asc');
  212. $column->orderby_desc_url = $this->orderby_link($column->orderby, 'desc');
  213. }
  214. }
  215. $method = 'build_' . $method;
  216. $this->output = $this->$method();
  217. }
  218. }
  219. /**
  220. * Datagrid column
  221. *
  222. * @package Core
  223. * @author Felice Ostuni
  224. * @copyright (c) 2011 Rapyd Team
  225. * @license http://www.rapyd.com/license
  226. */
  227. class datagrid_column extends rpd_component_library
  228. {
  229. public $url = "";
  230. public $link = "";
  231. public $onclick = "";
  232. public $label = "";
  233. public $attributes = array();
  234. public $tr_attributes = array();
  235. public $tr_attr = '';
  236. public $column_type = "normal"; //orderby, detail, ation
  237. public $orderby = false;
  238. public $checkbox = "";
  239. public $check = "";
  240. public static $checkbox_id = 1;
  241. public $orderby_asc_url;
  242. public $orderby_desc_url;
  243. protected $pattern = "";
  244. protected $pattern_type = null;
  245. protected $field = null;
  246. protected $field_name = null;
  247. protected $field_list = array();
  248. // --------------------------------------------------------------------
  249. public function __construct($config = array())
  250. {
  251. parent::__construct($config);
  252. $this->check_pattern();
  253. }
  254. // --------------------------------------------------------------------
  255. protected function set_pattern($pattern)
  256. {
  257. $this->pattern = is_object($pattern) ? clone ($pattern) : $pattern;
  258. $this->rpattern = is_object($pattern) ? clone ($pattern) : $pattern;
  259. }
  260. // --------------------------------------------------------------------
  261. protected function check_pattern()
  262. {
  263. if (is_object($this->pattern))
  264. {
  265. $this->pattern_type = "field_object";
  266. $this->field = $this->pattern;
  267. if ($this->orderby === true)
  268. {
  269. $this->orderby = $this->field->name;
  270. }
  271. } else
  272. {
  273. $this->field_list = parent::parse_pattern($this->pattern);
  274. if (is_array($this->field_list))
  275. {
  276. $this->pattern_type = "pattern";
  277. if ($this->orderby === true)
  278. {
  279. $this->orderby = $this->field_list[0];
  280. }
  281. } else
  282. {
  283. $this->pattern_type = "field_name";
  284. $this->field_name = $this->pattern;
  285. if ($this->orderby === true)
  286. {
  287. $this->orderby = (isset($this->orderby_field)) ? $this->orderby_field : $this->field_name;
  288. }
  289. }
  290. }
  291. if ($this->orderby)
  292. {
  293. $this->column_type = 'orderby';
  294. }
  295. }
  296. // --------------------------------------------------------------------
  297. function reset_pattern()
  298. {
  299. $this->rpattern = $this->pattern;
  300. }
  301. // --------------------------------------------------------------------
  302. protected function set_url($url, $img = '', $onclick='')
  303. {
  304. $uri = rpd::uri($url);
  305. $this->url = rpd::url($uri);
  306. //$this->url = $url;
  307. $this->img = $img;
  308. $this->onclick = $onclick;
  309. return $this;
  310. }
  311. // --------------------------------------------------------------------
  312. public function set_attributes($attributes)
  313. {
  314. $this->attributes = $attributes;
  315. return $this;
  316. }
  317. // --------------------------------------------------------------------
  318. public function set_callback($callback, $object = null)
  319. {
  320. $this->callback = $callback;
  321. $this->callback_object = $object;
  322. }
  323. // --------------------------------------------------------------------
  324. public function set_tr_attributes($attributes)
  325. {
  326. $this->tr_attributes = $attributes;
  327. return $this;
  328. }
  329. // --------------------------------------------------------------------
  330. public function set_row($data_row)
  331. {
  332. switch ($this->pattern_type)
  333. {
  334. case "field_object":
  335. if (isset($data_row[$this->field->name]))
  336. {
  337. $this->field->value = $data_row[$this->field->name];
  338. } else
  339. {
  340. $this->field->value = "";
  341. }
  342. break;
  343. case "pattern":
  344. $this->rpattern = $this->replace_pattern($this->rpattern, $data_row);
  345. break;
  346. case "field_name":
  347. if (isset($data_row[$this->field_name]))
  348. {
  349. $this->rpattern = $data_row[$this->field_name];
  350. } elseif (array_key_exists($this->field_name, $data_row))
  351. {
  352. $this->rpattern = "";
  353. }
  354. break;
  355. }
  356. if (isset($this->callback_object))
  357. {
  358. $this->rpattern = call_user_func(array($this->callback_object, $this->callback), $data_row);
  359. } elseif (isset($this->callback))
  360. {
  361. $this->rpattern = call_user_func($this->callback, $data_row);
  362. }
  363. if ($this->url)
  364. {
  365. if (!isset($this->attributes['style']))
  366. $this->attributes['style'] = 'width: 70px; text-align:center; padding-right:5px';
  367. $this->link = parent::replace_pattern($this->url, $data_row);
  368. }
  369. if ($this->checkbox != "")
  370. {
  371. $value = $data_row[$this->field_name];
  372. $attributes = array('name' => $this->field_name . '[]', 'id' => $this->field_name . (string) self::$checkbox_id++,);
  373. $this->check = rpd_form_helper::checkbox($attributes, $value);
  374. }
  375. $this->attributes = rpd_html_helper::attributes($this->attributes);
  376. if (count($this->tr_attributes))
  377. {
  378. $this->tr_attr = array();
  379. foreach($this->tr_attributes as $k=>$v)
  380. {
  381. $this->tr_attr[$k] = parent::replace_pattern($v, $data_row);
  382. }
  383. $this->tr_attr = rpd_html_helper::attributes($this->tr_attr);
  384. }
  385. }
  386. /**
  387. * a column value by default is a string: the field-name you want to show in the column
  388. * but it support also a "pattern" with html and placeholders like : {field1} <br /> {field2}
  389. * @return type
  390. */
  391. function get_value()
  392. {
  393. switch ($this->pattern_type)
  394. {
  395. case "field_object":
  396. $this->field->request_refill = false;
  397. $this->field->status = "show";
  398. $this->field->build();
  399. return $this->field->output;
  400. break;
  401. case "pattern":
  402. if ($this->rpattern == "")
  403. {
  404. $this->rpattern = "&nbsp;";
  405. }
  406. $this->rpattern = parent::replace_functions($this->rpattern);
  407. return $this->rpattern;
  408. break;
  409. case "field_name":
  410. $this->rpattern = nl2br(htmlspecialchars($this->rpattern));
  411. if ($this->rpattern == "")
  412. {
  413. $this->rpattern = "&nbsp;";
  414. }
  415. return $this->rpattern;
  416. break;
  417. }
  418. }
  419. /**
  420. * replace {field} with value
  421. * @return string
  422. */
  423. function orderby_link()
  424. {
  425. return str_replace('{field}', $column->orderby_field, $this->orderby_asc_url);
  426. }
  427. }