BxGridMy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreSamples 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 `sample_grid_data` WHERE `Role` != 3 ', 'sample_grid_data', 'ID', 'Order', '', 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. -- SQL dump of sample data:
  38. CREATE TABLE IF NOT EXISTS `sample_grid_data` (
  39. `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  40. `NickName` varchar(255) NOT NULL DEFAULT '',
  41. `Email` varchar(255) NOT NULL DEFAULT '',
  42. `Status` enum('Unconfirmed','Approval','Active','Rejected','Suspended') NOT NULL DEFAULT 'Unconfirmed',
  43. `Role` tinyint(4) unsigned NOT NULL DEFAULT '1',
  44. `Sex` varchar(255) NOT NULL DEFAULT '',
  45. `Headline` varchar(255) NOT NULL,
  46. `DescriptionMe` text NOT NULL,
  47. `Country` varchar(255) NOT NULL DEFAULT '',
  48. `City` varchar(255) NOT NULL,
  49. `DateOfBirth` date NOT NULL,
  50. `Tags` varchar(255) NOT NULL DEFAULT '',
  51. `Order` int(11) NOT NULL,
  52. PRIMARY KEY (`ID`),
  53. UNIQUE KEY `NickName` (`NickName`),
  54. KEY `Country` (`Country`),
  55. KEY `DateOfBirth` (`DateOfBirth`),
  56. FULLTEXT KEY `NickName_2` (`NickName`,`City`,`Headline`,`DescriptionMe`,`Tags`)
  57. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=56 ;
  58. INSERT INTO `sample_grid_data` VALUES
  59. (1, 'admin', 'dev@boonex.com', 'Rejected', 3, '', '', '', '', '', '0000-00-00', '', 0),
  60. (43, 'Shmak', 'shamks@gmail.com', 'Active', 1, '', '', '', '', 'Yogurtown', '0000-00-00', '', 1),
  61. (45, 'Rio', 'parrot@blue.bz', 'Active', 1, '', '', '', '', 'Rio', '0000-00-00', '', 9),
  62. (36, 'Master Yoda', 'force@galaxy.sw', 'Active', 1, '', '', '', '', 'Sydney', '0000-00-00', '', 4),
  63. (28, 'petr', 'petr@petrovich.com', 'Active', 1, '', '', '', '', 'Petrovka', '0000-00-00', '', 10),
  64. (35, 'MrTwister', 'mr@twister.com', 'Active', 1, '', '', '', '', 'Mumbai', '0000-00-00', '', 6),
  65. (42, 'kokoko', 'okokok@ok.com', 'Unconfirmed', 1, '', '', '', '', 'Ok', '0000-00-00', '', 10),
  66. (34, 'uno', 'uno@boonex.com', 'Active', 1, '', '', '', '', 'Sydney', '0000-00-00', '', 9),
  67. (41, 'Super User', 'user@super.co', 'Active', 1, '', '', '', '', 'Seashore', '0000-00-00', '', 2),
  68. (10, 'Andrew5', 'uno5@boonex.com', 'Active', 1, 'male', 'Unite People!', '<p>This is a demo profile that you may find on <a href="http://www.boonex.com/">BoonEx</a>.<p>BoonEx mission is to Unite People and thus make the world a better place.</p>', 'AU', 'Castle Hill', '1981-03-31', 'boonex, community, unite, people', 10),
  69. (47, 'Bimbo', 'bambi@bumba.com', 'Unconfirmed', 1, '', '', '', '', 'Bomba', '0000-00-00', '', 3),
  70. (50, 'X-Man', 'x@man.me', 'Unconfirmed', 1, '', '', '', '', 'Movie City', '0000-00-00', '', 10);
  71. */
  72. class BxGridMy extends BxTemplGrid
  73. {
  74. public function __construct ($aOptions, $oTemplate = false)
  75. {
  76. parent::__construct ($aOptions, $oTemplate);
  77. }
  78. /**
  79. * add js file for AJAX form submission
  80. */
  81. protected function _addJsCss()
  82. {
  83. parent::_addJsCss();
  84. $this->_oTemplate->addJs('jquery.form.min.js');
  85. $oForm = new BxTemplFormView(array());
  86. $oForm->addCssJs();
  87. }
  88. /**
  89. * 'add' action handler
  90. */
  91. public function performActionAdd()
  92. {
  93. $sAction = 'add';
  94. $aForm = array(
  95. 'form_attrs' => array(
  96. 'id' => 'sample-add-form',
  97. 'action' => 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r')), // grid.php is usiversal actions handler file, we need to pass object and action names to it at least, or just all the params to preserve such states like filter and paginate
  98. 'method' => 'post',
  99. ),
  100. 'params' => array (
  101. 'db' => array(
  102. 'table' => 'sample_grid_data',
  103. 'key' => 'ID',
  104. 'submit_name' => 'do_submit',
  105. ),
  106. ),
  107. 'inputs' => array(
  108. 'NickName' => array(
  109. 'type' => 'text',
  110. 'name' => 'NickName',
  111. 'caption' => _t('Username'),
  112. 'required' => true,
  113. 'checker' => array(
  114. 'func' => 'length',
  115. 'params' => array(1, 150),
  116. 'error' => _t( 'Username is required' )
  117. ),
  118. 'db' => array (
  119. 'pass' => 'Xss',
  120. ),
  121. ),
  122. 'Email' => array(
  123. 'type' => 'text',
  124. 'name' => 'Email',
  125. 'caption' => _t('Email'),
  126. 'required' => true,
  127. 'checker' => array(
  128. 'func' => 'email',
  129. 'error' => _t( '_Incorrect Email' )
  130. ),
  131. 'db' => array (
  132. 'pass' => 'Xss',
  133. ),
  134. ),
  135. 'City' => array(
  136. 'type' => 'text',
  137. 'name' => 'City',
  138. 'caption' => _t('City'),
  139. 'required' => true,
  140. 'checker' => array(
  141. 'func' => 'length',
  142. 'params' => array(1, 150),
  143. 'error' => _t( 'City is required' )
  144. ),
  145. 'db' => array (
  146. 'pass' => 'Xss',
  147. ),
  148. ),
  149. 'submit' => array(
  150. 'type' => 'input_set',
  151. 0 => array (
  152. 'type' => 'submit',
  153. 'name' => 'do_submit',
  154. 'value' => _t('_Submit'),
  155. ),
  156. 1 => array (
  157. 'type' => 'reset',
  158. 'name' => 'close',
  159. 'value' => _t('Close'),
  160. 'attrs' => array(
  161. 'onclick' => "$('.bx-popup-active').dolPopupHide()",
  162. 'class' => 'bx-def-margin-sec-left',
  163. ),
  164. ),
  165. ),
  166. ),
  167. );
  168. $oForm = new BxTemplFormView($aForm);
  169. $oForm->initChecker();
  170. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  171. $iNewId = $oForm->insert (array(), true); // insert record to database
  172. if ($iNewId)
  173. $aRes = array('grid' => $this->getCode(false), 'blink' => $iNewId); // if record is successfully added, reload grid and highlight added row
  174. else
  175. $aRes = array('msg' => "Error occured"); // if record adding failed, display error message
  176. echoJson($aRes);
  177. } else { // if form is not submitted or some fields are invalid, display popup with form
  178. // we need to use 'transBox' function to properly display 'popup'
  179. $s = BxTemplFunctions::getInstance()->transBox('', '
  180. <div class="bx-def-padding bx-def-color-bg-block" style="width:300px;">' . $oForm->getCode() . '</div>
  181. <script>
  182. $(document).ready(function () {
  183. $("#sample-add-form").ajaxForm({
  184. dataType: "json",
  185. beforeSubmit: function (formData, jqForm, options) {
  186. bx_loading($("#' . $aForm['form_attrs']['id'] . '"), true);
  187. },
  188. success: function (data) {
  189. $(".bx-popup-active").dolPopupHide();
  190. glGrids.' . $this->_sObject . '.processJson(data, "' . $sAction . '");
  191. }
  192. });
  193. });
  194. </script>');
  195. echoJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))));
  196. }
  197. }
  198. /**
  199. * 'approve' action handler
  200. */
  201. public function performActionApprove()
  202. {
  203. $iAffected = 0;
  204. $aIds = bx_get('ids');
  205. if (!$aIds || !is_array($aIds)) {
  206. echoJson(array());
  207. exit;
  208. }
  209. $aIdsAffected = array ();
  210. foreach ($aIds as $mixedId) {
  211. if (!$this->_approve($mixedId))
  212. continue;
  213. $aIdsAffected[] = $mixedId;
  214. $iAffected++;
  215. }
  216. echoJson(array_merge(
  217. array(
  218. 'grid' => $this->getCode(false),
  219. 'blink' => $aIdsAffected,
  220. 'eval' => 'alert(22)',
  221. ),
  222. $iAffected ? array () : array('msg' => "Profile(s) activation failed")
  223. )
  224. );
  225. }
  226. /**
  227. * helper funtion for 'approve' action handler
  228. */
  229. protected function _approve ($mixedId)
  230. {
  231. $oDb = BxDolDb::getInstance();
  232. $sTable = $this->_aOptions['table'];
  233. $sFieldId = $this->_aOptions['field_id'];
  234. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `Status` = 'Active' WHERE `{$sFieldId}` = ?", $mixedId);
  235. return $oDb->query($sQuery);
  236. }
  237. /**
  238. * custom cell look for 'Status' field
  239. */
  240. protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow)
  241. {
  242. $sAttr = $this->_convertAttrs(
  243. $aField, 'attr_cell',
  244. false,
  245. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  246. );
  247. return '<td ' . $sAttr . '><span style="background-color:' . ('Active' == $mixedValue ? '#cfc' : '#fcc') . '">' . $mixedValue . '</span></td>';
  248. }
  249. /**
  250. * custom column header look for 'Status' field
  251. */
  252. protected function _getCellHeaderStatus ($sKey, $aField)
  253. {
  254. $s = parent::_getCellHeaderDefault($sKey, $aField);
  255. return preg_replace ('/<th(.*?)>(.*?)<\/th>/', '<th$1><img src="' . BxDolTemplate::getInstance()->getImageUrl('acl-standard.png') . '"></th>', $s);
  256. }
  257. /**
  258. * custom behavior for 'custom1' action
  259. */
  260. protected function _getActionCustom1 ($sType, $sKey, $a, $isSmall = false)
  261. {
  262. $sAttr = $this->_convertAttrs(
  263. $a, 'attr',
  264. 'bx-btn bx-def-margin-sec-left' . ($isSmall ? ' bx-btn-small' : '') // add default classes
  265. );
  266. return '<button ' . $sAttr . ' onclick="$(this).off(); alert(\'default behaviour is overrided, so the action is not performed\');">' . $a['title'] . '</button>';
  267. }
  268. }
  269. /** @} */