A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
10 KiB

13 years ago
  1. <?php
  2. /** Load in the QCodo framework
  3. * @package QCodo
  4. */
  5. require('../prepend.inc.php');
  6. /** Load in the QCodeGen Class
  7. * @package QCodo
  8. */
  9. require(__QCODO__ . '/codegen/QCodeGen.class.php');
  10. // Security check for ALLOW_REMOTE_ADMIN
  11. // To allow access REGARDLESS of ALLOW_REMOTE_ADMIN, simply remove the line below
  12. // QApplication::CheckRemoteAdmin();
  13. /**
  14. * Class CodeGenUI - this class provides a more flexible way to run the code generator. It is like codegen.php
  15. * but allows you to select settings and the list of tables for which to generate ORM classes.
  16. * Basically, this class loads the standard codegen_settings.xml, saves a temporary copy and gives
  17. * the filename to QCodeGen::Run.
  18. *
  19. *@author Erik Winn <erikwinnmail@yahoo.com>
  20. *
  21. * $Id: codegen_ui.php 522 2009-04-02 18:14:53Z erikwinn $
  22. *@version 0.1
  23. * @package QCodo
  24. */
  25. class CodeGenUI extends QForm
  26. {
  27. ///@var QPanel - the panel containing controls for selecting settings
  28. public $pnlSettings;
  29. ///@var QButton - triggers code generation
  30. public $btnRun;
  31. ///@var QListBox - a list of tables for which to generate code
  32. public $lstTables;
  33. ///@var QCheckBox - check to support old style manual queries
  34. public $chkManualQuerySupport;
  35. ///@var QTextBox - type table suffix
  36. public $txtTypeTableIdentifier;
  37. ///@var QTextBox - association table suffix
  38. public $txtAssociationTableIdentifier;
  39. ///@var QTextBox - optional table prefix to remove
  40. public $txtStripFromTableName;
  41. ///@var QCheckBox - check to preserve previously generated QQN file
  42. public $chkSaveORM;
  43. ///@var string - comma delimited list of tables to generate
  44. private $strIncludeList;
  45. ///@var DOMDocument - contains the codegen_settings.xml XML
  46. private $objSettingsDom;
  47. ///@var string - file name for the QCodo standard codegen_settings.xml; adjust to fit your set up.
  48. private $strSettingsTpl;
  49. ///@var string - file name for the temporary settings XML; adjust to fit your set up.
  50. private $strMySettingsFile = '/tmp/code_generator_settings.xml';
  51. ///@var boolean - whether to preserve the old QQN.class.php
  52. private $blnSaveORM;
  53. ///@var array - contains names of ORM files to save and replace
  54. private $aryORMFiles;
  55. ///Runs last .. replace old ORM if desired
  56. protected function Form_Exit()
  57. {
  58. if($this->blnSaveORM)
  59. {
  60. foreach($this->aryORMFiles as $strFileName => $strTempFileName)
  61. if(file_exists($strTempFileName) )
  62. copy($strTempFileName, $strFileName);
  63. }
  64. }
  65. ///Runs once, first time the page is accessed (in a session)
  66. protected function Form_Create()
  67. {
  68. $this->strSettingsTpl = __DOCROOT__ . __DEVTOOLS__ . '/codegen_settings.xml';
  69. // $this->strTemplate = 'codegen_ui.tpl.php';
  70. if( !is_file($this->strSettingsTpl) )
  71. throw new QCallerException('Settings template missing: ' . $this->strSettingsTpl);
  72. //Set up array of ORM classes to preserve ..
  73. $strBaseDir = __DATAGEN_CLASSES__;
  74. $this->aryORMFiles = array(
  75. $strBaseDir . '/_type_class_paths.inc.php' => $strBaseDir . '/_type_class_paths.inc.php-gentmp',
  76. $strBaseDir . '/_class_paths.inc.php' => $strBaseDir . '/_class_paths.inc.php-gentmp',
  77. $strBaseDir . '/QQN.class.php' => $strBaseDir . '/QQN.class.php-gentmp',
  78. $strBaseDir . '/QMetaDataBase.class.php' => $strBaseDir . '/QMetaDataBase.class.php-gentmp',
  79. );
  80. $this->objSettingsDom = new DOMDocument();
  81. $this->objSettingsDom->load( $this->strSettingsTpl );
  82. $this->pnlSettings = new QPanel($this);
  83. //Note that we are setting a template for the child panel here:
  84. $this->pnlSettings->Template = 'codegen_ui_settings.tpl.php';
  85. $this->initTableList();
  86. //get the other settings in the standard file ..
  87. $objTypeTableIdentifierNode = $this->objSettingsDom->getElementsByTagName('typeTableIdentifier')->item(0);
  88. $objAssociationTableIdentifierNode = $this->objSettingsDom->getElementsByTagName('associationTableIdentifier')->item(0);
  89. $objManualQueryNode = $this->objSettingsDom->getElementsByTagName('manualQuery')->item(0);
  90. $objStripFromTableName= $this->objSettingsDom->getElementsByTagName('stripFromTableName')->item(0);
  91. $this->chkManualQuerySupport = new QCheckBox($this->pnlSettings);
  92. $this->chkManualQuerySupport->Name = 'Support (old) Manual Queries';
  93. $this->chkManualQuerySupport->Checked = ( 'true' == $objManualQueryNode->getAttribute('support') );
  94. $this->txtAssociationTableIdentifier = new QTextBox($this->pnlSettings);
  95. $this->txtAssociationTableIdentifier->Name = 'Association table suffix';
  96. $this->txtAssociationTableIdentifier->Text = $objAssociationTableIdentifierNode->getAttribute('suffix');
  97. $this->txtTypeTableIdentifier = new QTextBox($this->pnlSettings);
  98. $this->txtTypeTableIdentifier->Name = 'Type table suffix';
  99. $this->txtTypeTableIdentifier->Text = $objTypeTableIdentifierNode->getAttribute('suffix');
  100. $this->txtStripFromTableName = new QTextBox($this->pnlSettings);
  101. $this->txtStripFromTableName->Name = 'Remove table prefix';
  102. $this->txtStripFromTableName->Text = $objStripFromTableName->getAttribute('prefix');
  103. $this->chkSaveORM = new QCheckBox($this->pnlSettings);
  104. $this->chkSaveORM->Name = 'Preserve ORM (Recommended)';
  105. $this->chkSaveORM->Checked = true;
  106. $this->btnRun = new QButton($this);
  107. $this->btnRun->Text = 'Run Generator';
  108. $this->btnRun->AddAction(new QClickEvent(), new QServerAction('btnRun_Click'));
  109. }
  110. /**
  111. * Creates a list box with a list of the possible tables for which to generate ORM code
  112. *
  113. * @todo - suppport multiple databases ..
  114. */
  115. private function initTableList()
  116. {
  117. $this->lstTables = new QListBox($this->pnlSettings);
  118. $this->lstTables->Name = 'Select tables';
  119. $this->lstTables->SelectionMode = QSelectionMode::Multiple;
  120. $objDbi = QApplication::$Database[1];
  121. $aryTables = $objDbi->GetTables();
  122. $intSize = 0;
  123. foreach($aryTables as $strTableName)
  124. {
  125. $intSize += 1;
  126. $this->lstTables->AddItem(new QListItem($strTableName, $strTableName));
  127. }
  128. $this->lstTables->Rows = $intSize > 10 ? 10: $intSize;
  129. }
  130. /**
  131. * This is called when the user clicks "Run Generator". It collects the values set in the upper
  132. * half of the page, writes a temporary XML doc for CodeGen settings and runs QCodeGen::Run
  133. * using the temporary settings.
  134. * Parameters are ignored.
  135. */
  136. protected function btnRun_Click($strFormId, $strControlId, $strParameter)
  137. {
  138. $this->objSettingsDom->load( $this->strSettingsTpl );
  139. $this->strIncludeList = '';
  140. $arySelectedItems = $this->lstTables->SelectedItems;
  141. if(!$arySelectedItems)
  142. return;
  143. foreach( $arySelectedItems as $objListItem)
  144. {
  145. if('' != $this->strIncludeList)
  146. $this->strIncludeList .= ',';
  147. $this->strIncludeList .= $objListItem->Value;
  148. }
  149. if('' == $this->strIncludeList)
  150. return;
  151. //first, set up the table selection ..
  152. $objExcludesNode = $this->objSettingsDom->getElementsByTagName('excludeTables')->item(0);
  153. $objIncludesNode = $this->objSettingsDom->getElementsByTagName('includeTables')->item(0);
  154. $objExcludesNode->setAttribute('pattern','[0-9a-zA-Z_]*');
  155. $objIncludesNode->setAttribute('list', $this->strIncludeList);
  156. //extra settings ..
  157. $objManualQueryNode = $this->objSettingsDom->getElementsByTagName('manualQuery')->item(0);
  158. $objTypeTableIdentifierNode = $this->objSettingsDom->getElementsByTagName('typeTableIdentifier')->item(0);
  159. $objAssociationTableIdentifierNode = $this->objSettingsDom->getElementsByTagName('associationTableIdentifier')->item(0);
  160. $objStripFromTableName= $this->objSettingsDom->getElementsByTagName('stripFromTableName')->item(0);
  161. $objManualQueryNode->setAttribute('support', $this->chkManualQuerySupport->Checked ? 'true' : 'false');
  162. $objAssociationTableIdentifierNode->setAttribute('suffix', trim($this->txtTypeTableIdentifier->Text));
  163. $objTypeTableIdentifierNode->setAttribute('suffix', trim($this->txtTypeTableIdentifier->Text));
  164. $objStripFromTableName->setAttribute('suffix', trim($this->txtStripFromTableName->Text));
  165. $this->objSettingsDom->save( $this->strMySettingsFile );
  166. //By default QCodeGen creates new ORM files - save these and replace if desired ..
  167. $this->blnSaveORM = $this->chkSaveORM->Checked;
  168. if($this->blnSaveORM)
  169. {
  170. foreach($this->aryORMFiles as $strFileName => $strTempFileName)
  171. if(file_exists($strFileName) )
  172. copy($strFileName, $strTempFileName);
  173. }
  174. QCodeGen::Run($this->strMySettingsFile);
  175. }
  176. /**
  177. * Convenience function for formatting output - pretty prints the given text
  178. *@param string strText - text to format and print to screen
  179. */
  180. protected function DisplayMonospacedText($strText)
  181. {
  182. $strText = QApplication::HtmlEntities($strText);
  183. $strText = str_replace(' ', ' ', $strText);
  184. $strText = str_replace(' ', '&nbsp;', $strText);
  185. $strText = str_replace("\r", '', $strText);
  186. $strText = str_replace("\n", '<br/>', $strText);
  187. _p($strText, false);
  188. }
  189. }
  190. CodeGenUI::Run('CodeGenUI');
  191. ?>