BxGridMy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 `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, 'Dolphin User', 'user@dolphin.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/dolphin">BoonEx Dolphin</a> demo sites or some default Dolphin installations. This and other demo profiles are used to post sample media content, messages and friend requests.</p>\r\n<p> </p>\r\n<p>Dolphin is a free, open-source social networking and online dating platform by <a href="http://www.boonex.com">BoonEx Community Software Experts</a>.</p>\r\n<p> </p>\r\n<p>BoonEx mission is to Unite People and thus make the world a better place.</p>', 'AU', 'Castle Hill', '1981-03-31', 'boonex, dolphin, 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. bx_import('BxTemplGrid');
  73. class BxGridMy extends BxTemplGrid
  74. {
  75. public function __construct ($aOptions, $oTemplate = false)
  76. {
  77. parent::__construct ($aOptions, $oTemplate);
  78. }
  79. /**
  80. * add js file for AJAX form submission
  81. */
  82. protected function _addJsCss()
  83. {
  84. parent::_addJsCss();
  85. $this->_oTemplate->addJs('jquery.form.min.js');
  86. bx_import('BxTemplFormView');
  87. $oForm = new BxTemplFormView(array());
  88. $oForm->addCssJs();
  89. }
  90. /**
  91. * 'add' action handler
  92. */
  93. public function performActionAdd()
  94. {
  95. $sAction = 'add';
  96. $aForm = array(
  97. 'form_attrs' => array(
  98. 'id' => 'sample-add-form',
  99. '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
  100. 'method' => 'post',
  101. ),
  102. 'params' => array (
  103. 'db' => array(
  104. 'table' => 'sample_grid_data',
  105. 'key' => 'ID',
  106. 'submit_name' => 'do_submit',
  107. ),
  108. ),
  109. 'inputs' => array(
  110. 'NickName' => array(
  111. 'type' => 'text',
  112. 'name' => 'NickName',
  113. 'caption' => _t('Username'),
  114. 'required' => true,
  115. 'checker' => array(
  116. 'func' => 'length',
  117. 'params' => array(1, 150),
  118. 'error' => _t( 'Username is required' )
  119. ),
  120. 'db' => array (
  121. 'pass' => 'Xss',
  122. ),
  123. ),
  124. 'Email' => array(
  125. 'type' => 'text',
  126. 'name' => 'Email',
  127. 'caption' => _t('Email'),
  128. 'required' => true,
  129. 'checker' => array(
  130. 'func' => 'email',
  131. 'error' => _t( '_Incorrect Email' )
  132. ),
  133. 'db' => array (
  134. 'pass' => 'Xss',
  135. ),
  136. ),
  137. 'City' => array(
  138. 'type' => 'text',
  139. 'name' => 'City',
  140. 'caption' => _t('City'),
  141. 'required' => true,
  142. 'checker' => array(
  143. 'func' => 'length',
  144. 'params' => array(1, 150),
  145. 'error' => _t( 'City is required' )
  146. ),
  147. 'db' => array (
  148. 'pass' => 'Xss',
  149. ),
  150. ),
  151. 'submit' => array(
  152. 'type' => 'input_set',
  153. 0 => array (
  154. 'type' => 'submit',
  155. 'name' => 'do_submit',
  156. 'value' => _t('_Submit'),
  157. ),
  158. 1 => array (
  159. 'type' => 'reset',
  160. 'name' => 'close',
  161. 'value' => _t('Close'),
  162. 'attrs' => array(
  163. 'onclick' => "$('.dolPopup:visible').dolPopupHide()",
  164. 'class' => 'bx-def-margin-sec-left',
  165. ),
  166. ),
  167. ),
  168. ),
  169. );
  170. bx_import('BxTemplFormView');
  171. $oForm = new BxTemplFormView($aForm);
  172. $oForm->initChecker();
  173. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  174. $iNewId = $oForm->insert (array(), true); // insert record to database
  175. if ($iNewId)
  176. $aRes = array('grid' => $this->getCode(true), 'blink' => $iNewId); // if record is successfully added, reload grid and highlight added row
  177. else
  178. $aRes = array('msg' => "Error occured"); // if record adding failed, display error message
  179. $this->_echoResultJson($aRes, true);
  180. } else { // if form is not submitted or some fields are invalid, display popup with form
  181. bx_import('BxTemplFunctions');
  182. // we need to use 'transBox' function to properly display 'popup'
  183. $s = BxTemplFunctions::getInstance()->transBox('', '
  184. <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>
  185. <script>
  186. $(document).ready(function () {
  187. $("#sample-add-form").ajaxForm({
  188. dataType: "json",
  189. beforeSubmit: function (formData, jqForm, options) {
  190. bx_loading($("#' . $aForm['form_attrs']['id'] . '"), true);
  191. },
  192. success: function (data) {
  193. $(".dolPopup:visible").dolPopupHide();
  194. glGrids.' . $this->_sObject . '.processJson(data, "' . $sAction . '");
  195. }
  196. });
  197. });
  198. </script>');
  199. $this->_echoResultJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))), true);
  200. }
  201. }
  202. /**
  203. * 'approve' action handler
  204. */
  205. public function performActionApprove()
  206. {
  207. $iAffected = 0;
  208. $aIds = bx_get('ids');
  209. if (!$aIds || !is_array($aIds)) {
  210. $this->_echoResultJson(array());
  211. exit;
  212. }
  213. $aIdsAffected = array ();
  214. foreach ($aIds as $mixedId) {
  215. if (!$this->_approve($mixedId))
  216. continue;
  217. $aIdsAffected[] = $mixedId;
  218. $iAffected++;
  219. }
  220. $this->_echoResultJson(array_merge(
  221. array(
  222. 'grid' => $this->getCode(false),
  223. 'blink' => $aIdsAffected,
  224. 'eval' => 'alert(22)',
  225. ),
  226. $iAffected ? array () : array('msg' => "Profile(s) activation failed")
  227. )
  228. );
  229. }
  230. /**
  231. * helper funtion for 'approve' action handler
  232. */
  233. protected function _approve ($mixedId)
  234. {
  235. $oDb = BxDolDb::getInstance();
  236. $sTable = $this->_aOptions['table'];
  237. $sFieldId = $this->_aOptions['field_id'];
  238. $sQuery = $oDb->prepare("UPDATE `{$sTable}` SET `Status` = 'Active' WHERE `{$sFieldId}` = ?", $mixedId);
  239. return $oDb->query($sQuery);
  240. }
  241. /**
  242. * custom cell look for 'Status' field
  243. */
  244. protected function _getCellStatus ($mixedValue, $sKey, $aField, $aRow)
  245. {
  246. $sAttr = $this->_convertAttrs(
  247. $aField, 'attr_cell',
  248. false,
  249. isset($aField['width']) ? 'width:' . $aField['width'] : false // add default styles
  250. );
  251. return '<td ' . $sAttr . '><span style="background-color:' . ('Active' == $mixedValue ? '#cfc' : '#fcc') . '">' . $mixedValue . '</span></td>';
  252. }
  253. /**
  254. * custom column header look for 'Status' field
  255. */
  256. protected function _getCellHeaderStatus ($sKey, $aField)
  257. {
  258. $s = parent::_getCellHeaderDefault($sKey, $aField);
  259. return preg_replace ('/<th(.*?)>(.*?)<\/th>/', '<th$1><img src="' . BxDolTemplate::getInstance()->getIconUrl('sys_fl_kg.gif') . '"></th>', $s);
  260. }
  261. /**
  262. * custom behavior for 'custom1' action
  263. */
  264. protected function _getActionCustom1 ($sType, $sKey, $a, $isSmall = false)
  265. {
  266. $sAttr = $this->_convertAttrs(
  267. $a, 'attr',
  268. 'bx-btn bx-def-margin-sec-left' . ($isSmall ? ' bx-btn-small' : '') // add default classes
  269. );
  270. return '<button ' . $sAttr . ' onclick="$(this).off(); alert(\'default behaviour is overrided, so the action is not performed\');">' . $a['title'] . '</button>';
  271. }
  272. }
  273. /** @} */