BxGridMy.php 12 KB

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