PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sgl/assets/_core/php/examples/dynamic/inline_editing.php

http://logisticsouth.googlecode.com/
PHP | 233 lines | 139 code | 36 blank | 58 comment | 20 complexity | 8111073885dda67f80f48bf2a4e6bfbf MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. require_once('../qcubed.inc.php');
  3. class ExampleForm extends QForm {
  4. // Declare the DataGrid, and the buttons and textboxes for inline editing
  5. protected $dtgPersons;
  6. protected $txtFirstName;
  7. protected $txtLastName;
  8. protected $btnSave;
  9. protected $btnCancel;
  10. protected $btnNew;
  11. // This value is either a Person->Id, "null" (if nothing is being edited), or "-1" (if creating a new Person)
  12. protected $intEditPersonId = null;
  13. protected function Form_Create() {
  14. // Define the DataGrid
  15. $this->dtgPersons = new QDataGrid($this);
  16. $this->dtgPersons->CellPadding = 5;
  17. $this->dtgPersons->CellSpacing = 0;
  18. // Define Columns -- we will define render helper methods to help with the rendering
  19. // of the HTML for most of these columns
  20. $this->dtgPersons->AddColumn(new QDataGridColumn('Person ID', '<?= $_ITEM->Id ?>', 'Width=100',
  21. array('OrderByClause' => QQ::OrderBy(QQN::Person()->Id), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Person()->Id, false))));
  22. // Setup the First and Last name columns with HtmlEntities set to false (because they may be rendering textbox controls)
  23. $this->dtgPersons->AddColumn(new QDataGridColumn('First Name', '<?= $_FORM->FirstNameColumn_Render($_ITEM) ?>', 'Width=200', 'HtmlEntities=false',
  24. array('OrderByClause' => QQ::OrderBy(QQN::Person()->FirstName), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Person()->FirstName, false))));
  25. $this->dtgPersons->AddColumn(new QDataGridColumn('Last Name', '<?= $_FORM->LastNameColumn_Render($_ITEM) ?>', 'Width=200', 'HtmlEntities=false',
  26. array('OrderByClause' => QQ::OrderBy(QQN::Person()->LastName), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Person()->LastName, false))));
  27. // Again, we setup the "Edit" column and ensure that the column's HtmlEntities is set to false
  28. $this->dtgPersons->AddColumn(new QDataGridColumn('Edit', '<?= $_FORM->EditColumn_Render($_ITEM) ?>', 'Width=120', 'HtmlEntities=false'));
  29. // Let's pre-default the sorting by id (column index #0) and use AJAX
  30. $this->dtgPersons->SortColumnIndex = 0;
  31. $this->dtgPersons->UseAjax = true;
  32. // Specify the DataBinder method for the DataGrid
  33. $this->dtgPersons->SetDataBinder('dtgPersons_Bind');
  34. // Make the DataGrid look nice
  35. $objStyle = $this->dtgPersons->RowStyle;
  36. $objStyle->FontSize = 12;
  37. $objStyle = $this->dtgPersons->AlternateRowStyle;
  38. $objStyle->BackColor = '#eaeaea';
  39. $objStyle = $this->dtgPersons->HeaderRowStyle;
  40. $objStyle->ForeColor = 'white';
  41. $objStyle->BackColor = '#000066';
  42. // Because browsers will apply different styles/colors for LINKs
  43. // We must explicitly define the ForeColor for the HeaderLink.
  44. // The header row turns into links when the column can be sorted.
  45. $objStyle = $this->dtgPersons->HeaderLinkStyle;
  46. $objStyle->ForeColor = 'white';
  47. // Create the other textboxes and buttons -- make sure we specify
  48. // the datagrid as the parent. If they hit the escape key, let's perform a Cancel.
  49. // Note that we need to terminate the action on the escape key event, too, b/c
  50. // many browsers will perform additional processing that we won't not want.
  51. $this->txtFirstName = new QTextBox($this->dtgPersons);
  52. $this->txtFirstName->Required = true;
  53. $this->txtFirstName->MaxLength = 50;
  54. $this->txtFirstName->Width = 200;
  55. $this->txtFirstName->AddAction(new QEscapeKeyEvent(), new QAjaxAction('btnCancel_Click'));
  56. $this->txtFirstName->AddAction(new QEscapeKeyEvent(), new QTerminateAction());
  57. $this->txtLastName = new QTextBox($this->dtgPersons);
  58. $this->txtLastName->Required = true;
  59. $this->txtLastName->MaxLength = 50;
  60. $this->txtLastName->Width = 200;
  61. $this->txtLastName->AddAction(new QEscapeKeyEvent(), new QAjaxAction('btnCancel_Click'));
  62. $this->txtLastName->AddAction(new QEscapeKeyEvent(), new QTerminateAction());
  63. // We want the Save button to be Primary, so that the save will perform if the
  64. // user hits the enter key in either of the textboxes.
  65. $this->btnSave = new QButton($this->dtgPersons);
  66. $this->btnSave->Text = 'Save';
  67. $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
  68. $this->btnSave->PrimaryButton = true;
  69. $this->btnSave->CausesValidation = true;
  70. // Make sure we turn off validation on the Cancel button
  71. $this->btnCancel = new QButton($this->dtgPersons);
  72. $this->btnCancel->Text = 'Cancel';
  73. $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));
  74. $this->btnCancel->CausesValidation = false;
  75. // Finally, let's add a "New" button
  76. $this->btnNew = new QButton($this);
  77. $this->btnNew->Text = 'New';
  78. $this->btnNew->AddAction(new QClickEvent(), new QAjaxAction('btnNew_Click'));
  79. $this->btnNew->CausesValidation = false;
  80. }
  81. protected function dtgPersons_Bind() {
  82. $objPersonArray = $this->dtgPersons->DataSource = Person::LoadAll(QQ::Clause(
  83. $this->dtgPersons->OrderByClause,
  84. $this->dtgPersons->LimitClause
  85. ));
  86. // If we are editing someone new, we need to add a new (blank) person to the data source
  87. if ($this->intEditPersonId == -1)
  88. array_push($objPersonArray, new Person());
  89. // Bind the datasource to the datagrid
  90. $this->dtgPersons->DataSource = $objPersonArray;
  91. }
  92. // When we Render, we need to see if we are currently editing someone
  93. protected function Form_PreRender() {
  94. // We want to force the datagrid to refresh on EVERY button click
  95. // Normally, the datagrid won't re-render on the ajaxactions because nothing
  96. // in the datagrid, itself, is being modified. But considering that every ajax action
  97. // on the page (e.g. every button click) makes changes to things that AFFECT the datagrid,
  98. // we need to explicitly force the datagrid to "refresh" on every event/action. Therefore,
  99. // we make the call to Refresh() in Form_PreRender
  100. $this->dtgPersons->Refresh();
  101. // If we are adding or editing a person, then we should disable the edit button
  102. if ($this->intEditPersonId)
  103. $this->btnNew->Enabled = false;
  104. else
  105. $this->btnNew->Enabled = true;
  106. }
  107. // If the person for the row we are rendering is currently being edited,
  108. // show the textbox. Otherwise, display the contents as is.
  109. public function FirstNameColumn_Render(Person $objPerson) {
  110. if (($objPerson->Id == $this->intEditPersonId) ||
  111. (($this->intEditPersonId == -1) && (!$objPerson->Id)))
  112. return $this->txtFirstName->RenderWithError(false);
  113. else
  114. // Because we are rendering with HtmlEntities set to false on this column
  115. // we need to make sure to escape the value
  116. return QApplication::HtmlEntities($objPerson->FirstName);
  117. }
  118. // If the person for the row we are rendering is currently being edited,
  119. // show the textbox. Otherwise, display the contents as is.
  120. public function LastNameColumn_Render(Person $objPerson) {
  121. if (($objPerson->Id == $this->intEditPersonId) ||
  122. (($this->intEditPersonId == -1) && (!$objPerson->Id)))
  123. return $this->txtLastName->RenderWithError(false);
  124. else
  125. // Because we are rendering with HtmlEntities set to false on this column
  126. // we need to make sure to escape the value
  127. return QApplication::HtmlEntities($objPerson->LastName);
  128. }
  129. // If the person for the row we are rendering is currently being edited,
  130. // show the Save & Cancel buttons. And the rest of the rows edit buttons
  131. // should be disabled. Otherwise, show the edit button normally.
  132. public function EditColumn_Render(Person $objPerson) {
  133. if (($objPerson->Id == $this->intEditPersonId) ||
  134. (($this->intEditPersonId == -1) && (!$objPerson->Id)))
  135. // We are rendering the row of the person we are editing OR we are rending the row
  136. // of the NEW (blank) person. Go ahead and render the Save and Cancel buttons.
  137. return $this->btnSave->Render(false) . '&nbsp;' . $this->btnCancel->Render(false);
  138. else {
  139. // Get the Edit button for this row (we will create it if it doesn't yet exist)
  140. $strControlId = 'btnEdit' . $objPerson->Id;
  141. $btnEdit = $this->GetControl($strControlId);
  142. if (!$btnEdit) {
  143. // Create the Edit button for this row in the DataGrid
  144. // Use ActionParameter to specify the ID of the person
  145. $btnEdit = new QButton($this->dtgPersons, $strControlId);
  146. $btnEdit->Text = 'Edit This Person';
  147. $btnEdit->ActionParameter = $objPerson->Id;
  148. $btnEdit->AddAction(new QClickEvent(), new QAjaxAction('btnEdit_Click'));
  149. $btnEdit->CausesValidation = false;
  150. }
  151. // If we are currently editing a person, then set this Edit button to be disabled
  152. if ($this->intEditPersonId)
  153. $btnEdit->Enabled = false;
  154. else
  155. $btnEdit->Enabled = true;
  156. // Return the rendered Edit button
  157. return $btnEdit->Render(false);
  158. }
  159. }
  160. // Handle the action for the Edit button being clicked. We must
  161. // setup the FirstName and LastName textboxes to contain the name of the person
  162. // we are editing.
  163. protected function btnEdit_Click($strFormId, $strControlId, $strParameter) {
  164. $this->intEditPersonId = $strParameter;
  165. $objPerson = Person::Load($strParameter);
  166. $this->txtFirstName->Text = $objPerson->FirstName;
  167. $this->txtLastName->Text = $objPerson->LastName;
  168. // Let's put the focus on the FirstName Textbox
  169. QApplication::ExecuteJavaScript(sprintf('qcodo.getControl("%s").focus()', $this->txtFirstName->ControlId));
  170. }
  171. // Handle the action for the Save button being clicked.
  172. protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
  173. if ($this->intEditPersonId == -1)
  174. $objPerson = new Person();
  175. else
  176. $objPerson = Person::Load($this->intEditPersonId);
  177. $objPerson->FirstName = trim($this->txtFirstName->Text);
  178. $objPerson->LastName = trim($this->txtLastName->Text);
  179. $objPerson->Save();
  180. $this->intEditPersonId = null;
  181. }
  182. // Handle the action for the Cancel button being clicked.
  183. protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
  184. $this->intEditPersonId = null;
  185. }
  186. // Handle the action for the New button being clicked. Clear the
  187. // contents of the Firstname and LastName textboxes.
  188. protected function btnNew_Click($strFormId, $strControlId, $strParameter) {
  189. $this->intEditPersonId = -1;
  190. $this->txtFirstName->Text = '';
  191. $this->txtLastName->Text = '';
  192. // Let's put the focus on the FirstName Textbox
  193. QApplication::ExecuteJavaScript(sprintf('qcodo.getControl("%s").focus()', $this->txtFirstName->ControlId));
  194. }
  195. }
  196. ExampleForm::Run('ExampleForm');
  197. ?>