BxGridMy.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Samples
  7. * @{
  8. */
  9. /**
  10. * @page samples
  11. * @section grid Grid
  12. */
  13. /**
  14. -- SQL dump of grid object:
  15. INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_mode`, `sorting_fields`, `override_class_name`, `override_class_file`) VALUES
  16. ('sample', 'Sql', 'SELECT `ID`, `NickName`, `Email`, `City`, `Status` FROM `Profiles` WHERE `Role` != 3 ', 'Profiles', 'ID', 'Education', '', 5, NULL, 'start', '', 'NickName,City,Headline,DescriptionMe,Tags', 'auto', 'ID,NickName,Email,City', 'BxGridMy', 'samples/BxGridMy.php');
  17. -- SQL dump of grid object fields:
  18. INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `params`, `order`) VALUES
  19. ('sample', 'order', '', '1%', '', 1),
  20. ('sample', 'checkbox', 'Select', '2%', '', 2),
  21. ('sample', 'ID', 'id', '7%', '', 3),
  22. ('sample', 'NickName', 'Username', '20%', '', 4),
  23. ('sample', 'Email', 'Email', '20%', '', 5),
  24. ('sample', 'actions', 'Actions', '20%', '', 8),
  25. ('sample', 'City', 'City', '20%', '', 6),
  26. ('sample', 'Status', 'Status', '10%', '', 7);
  27. -- SQL dump of grid object actions:
  28. INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `confirm`, `order`) VALUES
  29. ('sample', 'bulk', 'delete', '_Delete', 1, 1),
  30. ('sample', 'bulk', 'approve', '_Approve', 0, 2),
  31. ('sample', 'bulk', 'custom1', '_Custom1', 0, 4),
  32. ('sample', 'bulk', 'custom2', '_Custom2', 0, 5),
  33. ('sample', 'single', 'delete', '_Delete', 1, 1),
  34. ('sample', 'single', 'edit', '_Edit', 0, 2),
  35. ('sample', 'independent', 'add', '_Add record', 0, 1),
  36. ('sample', 'independent', 'settings', '_Settings', 0, 2);
  37. */
  38. bx_import('BxTemplGrid');
  39. class BxGridMy extends BxTemplGrid {
  40. public function __construct ($aOptions, $oTemplate = false) {
  41. parent::__construct ($aOptions, $oTemplate);
  42. }
  43. /**
  44. * add js file for AJAX form submission
  45. */
  46. protected function _addJsCss() {
  47. parent::_addJsCss();
  48. $this->_oTemplate->addJs('jquery.form.js');
  49. bx_import('BxTemplFormView');
  50. $oForm = new BxTemplFormView(array());
  51. $oForm->addCssJs();
  52. }
  53. /**
  54. * 'add' action handler
  55. */
  56. public function performActionAdd() {
  57. $sAction = 'add';
  58. $aForm = array(
  59. 'form_attrs' => array(
  60. 'id' => 'sample-add-form',
  61. 'action' => 'grid.php?o=' . $this->_sObject . '&a=' . $sAction, // grid.php is usiversal actions handler file, we need to pass object and action names to it at least
  62. 'method' => 'post',
  63. ),
  64. 'params' => array (
  65. 'db' => array(
  66. 'table' => 'Profiles_!!!',
  67. 'key' => 'ID',
  68. 'submit_name' => 'do_submit',
  69. ),
  70. ),
  71. 'inputs' => array(
  72. 'NickName' => array(
  73. 'type' => 'text',
  74. 'name' => 'NickName',
  75. 'caption' => _t('Username'),
  76. 'required' => true,
  77. 'checker' => array(
  78. 'func' => 'length',
  79. 'params' => array(1, 150),
  80. 'error' => _t( 'Username is required' )
  81. ),
  82. 'db' => array (
  83. 'pass' => 'Xss',
  84. ),
  85. ),
  86. 'Email' => array(
  87. 'type' => 'text',
  88. 'name' => 'Email',
  89. 'caption' => _t('Email'),
  90. 'required' => true,
  91. 'checker' => array(
  92. 'func' => 'email',
  93. 'error' => _t( '_Incorrect Email' )
  94. ),
  95. 'db' => array (
  96. 'pass' => 'Xss',
  97. ),
  98. ),
  99. 'City' => array(
  100. 'type' => 'text',
  101. 'name' => 'City',
  102. 'caption' => _t('City'),
  103. 'required' => true,
  104. 'checker' => array(
  105. 'func' => 'length',
  106. 'params' => array(1, 150),
  107. 'error' => _t( 'City is required' )
  108. ),
  109. 'db' => array (
  110. 'pass' => 'Xss',
  111. ),
  112. ),
  113. 'submit' => array(
  114. 'type' => 'input_set',
  115. 0 => array (
  116. 'type' => 'submit',
  117. 'name' => 'do_submit',
  118. 'value' => _t('_Submit'),
  119. ),
  120. 1 => array (
  121. 'type' => 'reset',
  122. 'name' => 'close',
  123. 'value' => _t('Close'),
  124. 'attrs' => array(
  125. 'onclick' => "$('.dolPopup:visible').dolPopupHide()",
  126. 'class' => 'bx-def-margin-sec-left',
  127. ),
  128. ),
  129. ),
  130. ),
  131. );
  132. bx_import('BxTemplFormView');
  133. $oForm = new BxTemplFormView($aForm);
  134. $oForm->initChecker();
  135. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  136. $iNewId = $oForm->insert (array(), true); // insert record to database
  137. if ($iNewId)
  138. $aRes = array('grid' => $this->getCode(true), 'blink' => $iNewId); // if record is successfully added, reload grid and highlight added row
  139. else
  140. $aRes = array('msg' => "Error occured"); // if record adding failed, display error message
  141. $this->_echoResultJson($aRes, true);
  142. } else { // if form is not submitted or some fields are invalid, display popup with form
  143. bx_import('BxTemplFunctions');
  144. // we need to use 'transBox' function to properly display 'popup'
  145. $s = BxTemplFunctions::getInstance()->transBox('
  146. <div class="bx-def-padding-top bx-def-padding-left bx-def-padding-right bx-def-color-bg-block" style="width:300px;">' . $oForm->getCode() . '</div>
  147. <script>
  148. $(document).ready(function () {
  149. $("#sample-add-form").ajaxForm({
  150. dataType: "json",
  151. beforeSubmit: function (formData, jqForm, options) {
  152. bx_loading($("#' . $aForm['form_attrs']['id'] . '"), true);
  153. },
  154. success: function (data) {
  155. $(".dolPopup:visible").dolPopupHide();
  156. glGrids.' . $this->_sObject . '.processJson(data, "' . $sAction . '");
  157. }
  158. });
  159. });
  160. </script>');
  161. $this->_echoResultJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))), true);
  162. }
  163. }
  164. /**
  165. * 'approve' action handler
  166. */
  167. public function performActionApprove() {
  168. $iAffected = 0;
  169. $aIds = bx_get('ids');
  170. if (!$aIds || !is_array($aIds)) {
  171. $this->_echoResultJson(array());
  172. exit;
  173. }
  174. $aIdsAffected = array ();
  175. foreach ($aIds as $mixedId) {
  176. if (!$this->_approve($mixedId))
  177. continue;
  178. $aIdsAffected[] = $mixedId;
  179. $iAffected++;
  180. }
  181. $this->_echoResultJson(array_merge(
  182. array(
  183. 'grid' => $this->getCode(false),
  184. 'blink' => $aIdsAffected,
  185. 'eval' => 'alert(22)',
  186. ),
  187. $iAffected ? array () : array('msg' => "Profile(s) activation failed")
  188. )
  189. );
  190. }
  191. /**
  192. * helper funtion for 'approve' action handler
  193. */
  194. protected function _approve ($mixedId) {
  195. $oDb = BxDolDb::getInstance();
  196. $sTable = $this->_aOptions['table'];
  197. $sFieldId = $this->_aOptions['field_id'];
  198. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `Status` = 'Active' WHERE `{$sFieldId}` = ?", $mixedId);
  199. return $oDb->query($sQuery);
  200. }
  201. /**
  202. * custom cell look for 'Status' field
  203. */
  204. protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow) {
  205. $sAttr = $this->_convertAttrs(
  206. $aField, 'attr_cell',
  207. false,
  208. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  209. );
  210. return '<td ' . $sAttr . '><span style="background-color:' . ('Active' == $mixedValue ? '#cfc' : '#fcc') . '">' . $mixedValue . '</span></td>';
  211. }
  212. /**
  213. * custom column header look for 'Status' field
  214. */
  215. protected function _getCellHeaderStatus ($sKey, $aField) {
  216. $s = parent::_getCellHeaderDefault($sKey, $aField);
  217. return preg_replace ('/<th(.*?)>(.*?)<\/th>/', '<th$1><img src="' . BxDolTemplate::getInstance()->getIconUrl('cmt-female.gif') . '"></th>', $s);
  218. }
  219. /**
  220. * custom behavior for 'custom1' action
  221. */
  222. protected function _getActionCustom1 ($sType, $sKey, $a, $isSmall = false) {
  223. $sAttr = $this->_convertAttrs(
  224. $a, 'attr',
  225. 'bx-btn bx-def-margin-sec-left' . ($isSmall ? ' bx-btn-small' : '') // add default classes
  226. );
  227. return '<button ' . $sAttr . ' onclick="$(this).off(); alert(\'default behaviour is overrided, so the action is not performed\');">' . $a['title'] . '</button>';
  228. }
  229. }
  230. /** @} */