PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/webcore/samples/testbed.php

#
PHP | 247 lines | 171 code | 22 blank | 54 comment | 3 complexity | 88ff9d4af82cfde224789cf555499ba6 MD5 | raw file
  1. <?php
  2. /**
  3. * WebCore 3.0 Samples Browser
  4. * @version 1.0.0
  5. * @author Mario Di Vece <mario@unosquare.com>
  6. */
  7. require_once 'initialize.inc.php';
  8. HttpContext::getPermissionSet()->setDefaultResolve(Permission::PERMISSION_ALLOW);
  9. HttpContext::applySecurity();
  10. /**
  11. * AddressesGridWidget represents a Model-View pair to list 'Addresses' entities within a grid.
  12. * @package Application
  13. * @subpackage Grids
  14. * @version 2010.10.20.22.33.27
  15. * @author WebCore Scaffolder <webcore@localhost>
  16. */
  17. class AddressesGridWidget extends WidgetBase
  18. {
  19. // Protected control model declarations
  20. protected $ctlAddressesId;
  21. protected $ctlAddressesLine1;
  22. protected $ctlAddressesLine2;
  23. protected $ctlAddressesLine3;
  24. protected $ctlAddressesStateId;
  25. protected $ctlAddressesPostalCode;
  26. protected $ctlAddressesDirections;
  27. protected $ctlAddressesFisrtName;
  28. protected $ctlAddressesLastName;
  29. protected $ctlAddressesCompanyName;
  30. protected $ctlAddressesPhonePrimary;
  31. protected $ctlAddressesPhoneOffice;
  32. protected $ctlAddressesPhoneHome;
  33. protected $ctlStateIdStatesAbbreviation;
  34. protected $ctlStateIdStatesName;
  35. /**
  36. * Initializes control models and plugs them into the model.
  37. */
  38. protected function initializeComponent()
  39. {
  40. //Control instantiation
  41. $this->ctlAddressesId = new NumberBoundGridColumn(
  42. 'addresses_id', 'Id', 'addresses.id', 'addresses.id');
  43. $this->ctlAddressesLine1 = new TextBoundGridColumn(
  44. 'addresses_line1', 'Line 1', 100, 'addresses.line1', 'addresses.line1');
  45. $this->ctlAddressesLine2 = new TextBoundGridColumn(
  46. 'addresses_line2', 'Line 2', 100, 'addresses.line2', 'addresses.line2');
  47. $this->ctlAddressesLine3 = new TextBoundGridColumn(
  48. 'addresses_line3', 'Line 3', 100, 'addresses.line3', 'addresses.line3');
  49. $this->ctlAddressesStateId = new NumberBoundGridColumn(
  50. 'addresses_state_id', 'State Id', 'addresses.state_id', 'addresses.state_id');
  51. $this->ctlAddressesPostalCode = new TextBoundGridColumn(
  52. 'addresses_postal_code', 'Postal Code', 100, 'addresses.postal_code', 'addresses.postal_code');
  53. $this->ctlAddressesDirections = new TextBoundGridColumn(
  54. 'addresses_directions', 'Directions', 100, 'addresses.directions', 'addresses.directions');
  55. $this->ctlAddressesFisrtName = new TextBoundGridColumn(
  56. 'addresses_fisrt_name', 'Fisrt Name', 100, 'addresses.fisrt_name', 'addresses.fisrt_name');
  57. $this->ctlAddressesLastName = new TextBoundGridColumn(
  58. 'addresses_last_name', 'Last Name', 100, 'addresses.last_name', 'addresses.last_name');
  59. $this->ctlAddressesCompanyName = new TextBoundGridColumn(
  60. 'addresses_company_name', 'Company Name', 100, 'addresses.company_name', 'addresses.company_name');
  61. $this->ctlAddressesPhonePrimary = new TextBoundGridColumn(
  62. 'addresses_phone_primary', 'Phone Primary', 100, 'addresses.phone_primary', 'addresses.phone_primary');
  63. $this->ctlAddressesPhoneOffice = new TextBoundGridColumn(
  64. 'addresses_phone_office', 'Phone Office', 100, 'addresses.phone_office', 'addresses.phone_office');
  65. $this->ctlAddressesPhoneHome = new TextBoundGridColumn(
  66. 'addresses_phone_home', 'Phone Home', 100, 'addresses.phone_home', 'addresses.phone_home');
  67. $this->ctlStateIdStatesAbbreviation = new TextBoundGridColumn(
  68. 'state_id_states_abbreviation', 'Abbreviation', 100, 'states.abbreviation', 'states.abbreviation');
  69. $this->ctlStateIdStatesName = new TextBoundGridColumn(
  70. 'state_id_states_name', 'Name', 100, 'states.name', 'states.name');
  71. // Root model instantiation
  72. $this->model = new Grid('addressesGridWidgetModel', 'Addresses');
  73. $this->model->addColumn($this->ctlAddressesId);
  74. $this->model->addColumn($this->ctlAddressesLine1);
  75. $this->model->addColumn($this->ctlAddressesLine2);
  76. $this->model->addColumn($this->ctlAddressesLine3);
  77. $this->model->addColumn($this->ctlAddressesStateId);
  78. $this->model->addColumn($this->ctlAddressesPostalCode);
  79. $this->model->addColumn($this->ctlAddressesDirections);
  80. $this->model->addColumn($this->ctlAddressesFisrtName);
  81. $this->model->addColumn($this->ctlAddressesLastName);
  82. $this->model->addColumn($this->ctlAddressesCompanyName);
  83. $this->model->addColumn($this->ctlAddressesPhonePrimary);
  84. $this->model->addColumn($this->ctlAddressesPhoneOffice);
  85. $this->model->addColumn($this->ctlAddressesPhoneHome);
  86. $this->model->addColumn($this->ctlStateIdStatesAbbreviation);
  87. $this->model->addColumn($this->ctlStateIdStatesName);
  88. $this->model->addColumn(new DetailsCommandGridColumn('detailsCommandColumn', 'detailsCommandColumn_Click', 'addresses_id'));
  89. // Paging and Sorting options
  90. $this->model->setDefaultSort('addresses_id', GridState::GRID_SORT_DESCENDING);
  91. $this->model->setPageSize(20);
  92. // Grid Print Preview
  93. $gridPrinter = new GridPrintEventManager('gridPrinter');
  94. $this->model->getChildren()->addControl($gridPrinter);
  95. // CSV Data Exporter
  96. $gridCsvExporter = new GridCsvExporterEventManager('gridCsvExporter');
  97. $this->model->getChildren()->addControl($gridCsvExporter);
  98. // View instantiation
  99. $this->view = new HtmlGridView($this->model);
  100. $this->view->setIsAsynchronous(true);
  101. }
  102. /**
  103. * Creates a new instance of this class.
  104. */
  105. public function __construct($name = 'addressesGridWidget')
  106. {
  107. parent::__construct($name);
  108. $this->initializeComponent();
  109. $this->registerEventHandlers();
  110. }
  111. /**
  112. * Creates a default instance of this class.
  113. * @return AddressesGridWidget
  114. */
  115. public static function createInstance()
  116. {
  117. return new AddressesGridWidget();
  118. }
  119. /**
  120. * Registers event handlers for controls.
  121. */
  122. protected function registerEventHandlers()
  123. {
  124. Controller::registerEventHandler('detailsCommandColumn_Click', array(__CLASS__, 'detailsCommandColumn_Clicked'));
  125. }
  126. /**
  127. * Gets this widget's associated model object.
  128. * @return Grid
  129. */
  130. public function &getModel()
  131. {
  132. return $this->model;
  133. }
  134. /**
  135. * Gets this widget's associated view object.
  136. * @return HtmlGridView
  137. */
  138. public function &getView()
  139. {
  140. return $this->view;
  141. }
  142. /**
  143. * Gets this widget's associated data source as a data adapter.
  144. * @return DataTableAdapterBase
  145. */
  146. public static function &getDataSource()
  147. {
  148. $dataSource = DataContext::getInstance()->getAdapter('Addresses');
  149. $dataSource
  150. ->joinRelated('States')
  151. ->addField('addresses', 'id', 'addresses_id')
  152. ->addField('addresses', 'line1', 'addresses_line1')
  153. ->addField('addresses', 'line2', 'addresses_line2')
  154. ->addField('addresses', 'line3', 'addresses_line3')
  155. ->addField('addresses', 'state_id', 'addresses_state_id')
  156. ->addField('addresses', 'postal_code', 'addresses_postal_code')
  157. ->addField('addresses', 'directions', 'addresses_directions')
  158. ->addField('addresses', 'fisrt_name', 'addresses_fisrt_name')
  159. ->addField('addresses', 'last_name', 'addresses_last_name')
  160. ->addField('addresses', 'company_name', 'addresses_company_name')
  161. ->addField('addresses', 'phone_primary', 'addresses_phone_primary')
  162. ->addField('addresses', 'phone_office', 'addresses_phone_office')
  163. ->addField('addresses', 'phone_home', 'addresses_phone_home')
  164. ->addField('states', 'abbreviation', 'state_id_states_abbreviation')
  165. ->addField('states', 'name', 'state_id_states_name');
  166. return $dataSource;
  167. }
  168. /**
  169. * Handles the request data.
  170. * @return int The number of events triggered by the view.
  171. */
  172. public function handleRequest()
  173. {
  174. $dataSource = self::getDataSource();
  175. $handledEvents = 0;
  176. if (Controller::isPostBack($this->model) === false)
  177. {
  178. $this->model->dataBind($dataSource);
  179. }
  180. else
  181. {
  182. $handledEvents = Controller::handleEvents($this->model);
  183. $this->model->dataBind($dataSource);
  184. if ($this->view->getIsAsynchronous())
  185. {
  186. $this->view->render();
  187. HttpResponse::end();
  188. }
  189. }
  190. return $handledEvents;
  191. }
  192. /**
  193. * Handles the detailsCommandColumn_Click event.
  194. * @param Grid $sender
  195. * @param ControllerEvent $event
  196. */
  197. public static function detailsCommandColumn_Clicked(&$sender, &$event)
  198. {
  199. // @todo Add custom code below. (Typycally, Controller::transfer logic)
  200. $requestUrl = HttpContextInfo::getInstance()->getRequestScriptPath();
  201. if (StringHelper::endsWith($requestUrl, '.index.php'))
  202. {
  203. $targetUrl = StringHelper::replaceEnd($requestUrl, '.index.php', '.details.php');
  204. Controller::transfer($targetUrl . '?id=' . urlencode($event->getValue()));
  205. }
  206. $sender->setMessage('Details command for item ' . $event->getValue());
  207. }
  208. }
  209. $widget = new AddressesGridWidget();
  210. $widget->handleRequest();
  211. HttpResponse::write(MarkupWriter::DTD_XHTML_STRICT . "\r\n");
  212. ?>
  213. <html xmlns="http://www.w3.org/1999/xhtml">
  214. <head>
  215. <title>WebCore Testbed</title>
  216. <?php HtmlViewManager::render(); ?>
  217. </head>
  218. <body>
  219. <pre>
  220. <?php
  221. $widget->render();
  222. ?>
  223. </pre>
  224. </body>
  225. </html>