1
0

BxGridMy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 TridentCore 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. 'class' => 'bx-def-margin-bottom',
  100. ),
  101. 'params' => array (
  102. 'db' => array(
  103. 'table' => 'sample_grid_data',
  104. 'key' => 'ID',
  105. 'submit_name' => 'do_submit',
  106. ),
  107. ),
  108. 'inputs' => array(
  109. 'NickName' => array(
  110. 'type' => 'text',
  111. 'name' => 'NickName',
  112. 'caption' => _t('Username'),
  113. 'required' => true,
  114. 'checker' => array(
  115. 'func' => 'length',
  116. 'params' => array(1, 150),
  117. 'error' => _t( 'Username is required' )
  118. ),
  119. 'db' => array (
  120. 'pass' => 'Xss',
  121. ),
  122. ),
  123. 'Email' => array(
  124. 'type' => 'text',
  125. 'name' => 'Email',
  126. 'caption' => _t('Email'),
  127. 'required' => true,
  128. 'checker' => array(
  129. 'func' => 'email',
  130. 'error' => _t( '_Incorrect Email' )
  131. ),
  132. 'db' => array (
  133. 'pass' => 'Xss',
  134. ),
  135. ),
  136. 'City' => array(
  137. 'type' => 'text',
  138. 'name' => 'City',
  139. 'caption' => _t('City'),
  140. 'required' => true,
  141. 'checker' => array(
  142. 'func' => 'length',
  143. 'params' => array(1, 150),
  144. 'error' => _t( 'City is required' )
  145. ),
  146. 'db' => array (
  147. 'pass' => 'Xss',
  148. ),
  149. ),
  150. 'submit' => array(
  151. 'type' => 'input_set',
  152. 0 => array (
  153. 'type' => 'submit',
  154. 'name' => 'do_submit',
  155. 'value' => _t('_Submit'),
  156. ),
  157. 1 => array (
  158. 'type' => 'reset',
  159. 'name' => 'close',
  160. 'value' => _t('Close'),
  161. 'attrs' => array(
  162. 'onclick' => "$('.bx-popup-active').dolPopupHide()",
  163. 'class' => 'bx-def-margin-sec-left',
  164. ),
  165. ),
  166. ),
  167. ),
  168. );
  169. $oForm = new BxTemplFormView($aForm);
  170. $oForm->initChecker();
  171. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  172. $iNewId = $oForm->insert (array(), true); // insert record to database
  173. if ($iNewId)
  174. $aRes = array('grid' => $this->getCode(false), 'blink' => $iNewId); // if record is successfully added, reload grid and highlight added row
  175. else
  176. $aRes = array('msg' => "Error occured"); // if record adding failed, display error message
  177. $this->_echoResultJson($aRes, true);
  178. } else { // if form is not submitted or some fields are invalid, display popup with form
  179. // we need to use 'transBox' function to properly display 'popup'
  180. $s = BxTemplFunctions::getInstance()->transBox('', '
  181. <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>
  182. <script>
  183. $(document).ready(function () {
  184. $("#sample-add-form").ajaxForm({
  185. dataType: "json",
  186. beforeSubmit: function (formData, jqForm, options) {
  187. bx_loading($("#' . $aForm['form_attrs']['id'] . '"), true);
  188. },
  189. success: function (data) {
  190. $(".bx-popup-active").dolPopupHide();
  191. glGrids.' . $this->_sObject . '.processJson(data, "' . $sAction . '");
  192. }
  193. });
  194. });
  195. </script>');
  196. $this->_echoResultJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))), true);
  197. }
  198. }
  199. /**
  200. * 'approve' action handler
  201. */
  202. public function performActionApprove()
  203. {
  204. $iAffected = 0;
  205. $aIds = bx_get('ids');
  206. if (!$aIds || !is_array($aIds)) {
  207. $this->_echoResultJson(array());
  208. exit;
  209. }
  210. $aIdsAffected = array ();
  211. foreach ($aIds as $mixedId) {
  212. if (!$this->_approve($mixedId))
  213. continue;
  214. $aIdsAffected[] = $mixedId;
  215. $iAffected++;
  216. }
  217. $this->_echoResultJson(array_merge(
  218. array(
  219. 'grid' => $this->getCode(false),
  220. 'blink' => $aIdsAffected,
  221. 'eval' => 'alert(22)',
  222. ),
  223. $iAffected ? array () : array('msg' => "Profile(s) activation failed")
  224. )
  225. );
  226. }
  227. /**
  228. * helper funtion for 'approve' action handler
  229. */
  230. protected function _approve ($mixedId)
  231. {
  232. $oDb = BxDolDb::getInstance();
  233. $sTable = $this->_aOptions['table'];
  234. $sFieldId = $this->_aOptions['field_id'];
  235. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `Status` = 'Active' WHERE `{$sFieldId}` = ?", $mixedId);
  236. return $oDb->query($sQuery);
  237. }
  238. /**
  239. * custom cell look for 'Status' field
  240. */
  241. protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow)
  242. {
  243. $sAttr = $this->_convertAttrs(
  244. $aField, 'attr_cell',
  245. false,
  246. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  247. );
  248. return '<td ' . $sAttr . '><span style="background-color:' . ('Active' == $mixedValue ? '#cfc' : '#fcc') . '">' . $mixedValue . '</span></td>';
  249. }
  250. /**
  251. * custom column header look for 'Status' field
  252. */
  253. protected function _getCellHeaderStatus ($sKey, $aField)
  254. {
  255. $s = parent::_getCellHeaderDefault($sKey, $aField);
  256. return preg_replace ('/<th(.*?)>(.*?)<\/th>/', '<th$1><img src="' . BxDolTemplate::getInstance()->getImageUrl('acl-standard.png') . '"></th>', $s);
  257. }
  258. /**
  259. * custom behavior for 'custom1' action
  260. */
  261. protected function _getActionCustom1 ($sType, $sKey, $a, $isSmall = false)
  262. {
  263. $sAttr = $this->_convertAttrs(
  264. $a, 'attr',
  265. 'bx-btn bx-def-margin-sec-left' . ($isSmall ? ' bx-btn-small' : '') // add default classes
  266. );
  267. return '<button ' . $sAttr . ' onclick="$(this).off(); alert(\'default behaviour is overrided, so the action is not performed\');">' . $a['title'] . '</button>';
  268. }
  269. }
  270. /** @} */