1: <?php
2:
3: require(__QCUBED__ . '/codegen/QCodeGen.class.php');
4: include (__QCUBED_CORE__ . '/codegen/controls/_class_paths.inc.php');
5:
6:
7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: class QModelConnectorEditDlg extends QDialog {
23:
24: protected $objCurrentControl;
25:
26: protected $tabs;
27:
28: protected $txtName;
29: protected $txtControlId;
30: protected $txtControlClass;
31: protected $lstFormGen;
32:
33: protected $params;
34: protected $objModelConnectorOptions;
35:
36: protected $generalOptions;
37: protected $dtgGeneralOptions;
38:
39: protected $categories;
40: protected $datagrids;
41:
42: public function __construct($objParentObject, $strControlId) {
43: parent::__construct ($objParentObject, $strControlId);
44:
45: $this->AutoRenderChildren = true;
46: $this->Width = 700;
47:
48: $this->objModelConnectorOptions = new QModelConnectorOptions();
49:
50: $this->tabs = new QTabs ($this);
51: $this->tabs->HeightStyle = "auto";
52:
53: $this->AddButton ('Save', 'save');
54: $this->AddButton ('Save, Regenerate and Reload', 'saveRefresh');
55: $this->AddButton ('Cancel', 'cancel');
56:
57: $this->AddAction(new QDialog_ButtonEvent(), new QAjaxControlAction($this, 'ButtonClick'));
58: }
59:
60: 61: 62:
63: protected function SetupTabs() {
64: $strClassNames = $this->CreateClassNameArray();
65: $this->tabs->RemoveChildControls(true);
66: $this->categories = array();
67:
68: $this->dtgGeneralOptions = new QHtmlTable($this->tabs, 'definitionTab');
69: $this->dtgGeneralOptions->ShowHeader = false;
70: $this->dtgGeneralOptions->Name = "General";
71: $this->dtgGeneralOptions->CreatePropertyColumn('Attribute', 'Name');
72: $col = $this->dtgGeneralOptions->AddColumn (new QHtmlTableCallableColumn('Attribute', array ($this, 'dtg_ValueRender'), $this->dtgGeneralOptions));
73: $col->HtmlEntities = false;
74: $this->dtgGeneralOptions->SetDataBinder('dtgGeneralOptions_Bind', $this);
75:
76: 77: 78: 79: 80: 81:
82: if ($this->objCurrentControl->LinkedNode->_ParentNode) {
83:
84: $this->generalOptions = array (
85: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'ControlClass', 'Override of the PHP type for the control. If you change this, save the dialog and reopen to reload the tabs to show the control specific options.', QModelConnectorParam::SelectionList, $strClassNames),
86: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'FormGen',
87: 'Whether or not to generate this object, just a label for the object, just the control, or both the control and label',
88: QModelConnectorParam::SelectionList,
89: array (QFormGen::Both=>'Both', QFormGen::None=>'None', QFormGen::ControlOnly=>'Control', QFormGen::LabelOnly=>'Label')),
90: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'Name', 'Control\'s Name', QType::String),
91: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'NoColumn', 'True to prevent a column in the lister from being generated.', QType::Boolean)
92: );
93: }
94: else {
95:
96:
97: $this->generalOptions = array (
98: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'ControlClass', 'Override of the PHP type for the control. If you change this, save the dialog and reopen to reload the tabs to show the control specific options.', QModelConnectorParam::SelectionList, $strClassNames),
99: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'Name', 'The Control\'s Name. Generally leave this blank, or use a plural name.', QType::String),
100: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'ItemName', 'The public name of an item in the list. Its used by the title of the edit form, for example. Defaults to the name of the table in the database.', QType::String),
101: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'CreateFilter', 'Whether to generate a separate control to filter the data. If the data list control does its own filtering, set this to false. Default is true.', QType::Boolean),
102: new QModelConnectorParam (QModelConnectorParam::GeneralCategory, 'EditMode',
103: 'How to edit an item. 1) Options are: to go to a separate form, 2) popup a dialog, or 3) popup a dialog only if not on a mobile device since mobile devices struggle with showing dialogs that are bigger than the screen.',
104: QModelConnectorParam::SelectionList,
105: array ('form'=>'Edit with a QForm', 'dialog'=>'Edit with a QDialog', 'both'=>'Edit with a form on mobile devices, and a dialog on desktops.'))
106: );
107: }
108:
109:
110: foreach ($this->generalOptions as $objParam) {
111: $objControl = $objParam->GetControl ($this->dtgGeneralOptions);
112: $strName = $objControl->Name;
113:
114: if (isset($this->params[$strName])) {
115: $objControl->Value = $this->params[$strName];
116: if ($strName == 'ControlClass') {
117: $strControlClass = $this->params[$strName];
118: }
119: } else {
120: $objControl->Value = null;
121: }
122: }
123:
124: if (!isset ($strControlClass)) {
125: $strControlClass = get_class ($this->objCurrentControl);
126: }
127: $params = $strControlClass::GetModelConnectorParams();
128:
129:
130: foreach ($params as $param) {
131: $this->categories[$param->Category][] = $param;
132: }
133:
134:
135: if (isset ($this->categories[QModelConnectorParam::GeneralCategory])) {
136:
137: foreach ($this->categories[QModelConnectorParam::GeneralCategory] as $objParam) {
138: $objControl = $objParam->GetControl($this->dtgGeneralOptions);
139: $strName = $objControl->Name;
140:
141: if (isset($this->params[$strName])) {
142: $objControl->Value = $this->params[$strName];
143: } else {
144: $objControl->Value = null;
145: }
146: $this->generalOptions[] = $objParam;
147: }
148:
149: unset($this->categories[QModelConnectorParam::GeneralCategory]);
150: }
151:
152: foreach ($this->categories as $tabName=>$params) {
153: $panel = new QPanel ($this->tabs);
154: $panel->SetCustomStyle('overflow-y', 'scroll');
155: $panel->SetCustomStyle('max-height', '200');
156: $panel->AutoRenderChildren = true;
157: $panel->Name = $tabName;
158:
159: $dtg = new QHtmlTable($panel);
160: $dtg->ShowHeader = false;
161: $dtg->CreatePropertyColumn('Attribute', 'Name');
162: $col = $dtg->AddColumn (new QHtmlTableCallableColumn('Attribute', array ($this, 'dtg_ValueRender'), $dtg));
163: $col->HtmlEntities = false;
164: $dtg->SetDataBinder('dtgControlBind', $this);
165: $dtg->Name = $tabName;
166: $this->datagrids[$tabName] = $dtg;
167:
168:
169: foreach ($params as $objParam) {
170: $objControl = $objParam->GetControl ($this->datagrids[$tabName]);
171: if ($objControl) {
172: $strName = $objControl->Name;
173:
174: if (isset($this->params['Overrides'][$strName])) {
175: $objControl->Value = $this->params['Overrides'][$strName];
176: } else {
177: $objControl->Value = null;
178: }
179: }
180: }
181:
182: }
183: }
184:
185: 186: 187:
188: public function dtgGeneralOptions_Bind() {
189: $this->dtgGeneralOptions->DataSource = $this->generalOptions;
190: }
191:
192: 193: 194:
195: public function dtgControlBind($dtg) {
196: $dtg->DataSource = $this->categories[$dtg->Name];
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: public function dtg_ValueRender (QModelConnectorParam $objControlParam, QControl $objParent) {
207: $objControl = $objControlParam->GetControl ($objParent);
208: return $objControl->Render(false);
209: }
210:
211: 212: 213: 214: 215:
216: public function EditControl (QControl $objControl) {
217: $this->objCurrentControl = $objControl;
218:
219: $this->Title = $objControl->Name . ' Edit';
220:
221: $blnEditable = $this->ReadParams();
222: if ($blnEditable) {
223: $this->SetupTabs();
224: $this->Open();
225: $this->tabs->Refresh();
226: }
227: }
228:
229: 230: 231: 232: 233: 234: 235:
236: public function ButtonClick ($strFormId, $strControlId, $mixParam) {
237: if ($mixParam == 'save') {
238: $this->UpdateControlInfo();
239: $this->WriteParams();
240: } elseif ($mixParam == 'saveRefresh') {
241: $this->UpdateControlInfo();
242: $this->WriteParams();
243: QCodeGen::Run(__CONFIGURATION__ . '/codegen_settings.xml');
244: foreach (QCodeGen::$CodeGenArray as $objCodeGen) {
245: $objCodeGen->GenerateAll();
246: }
247: QApplication::Redirect($_SERVER['PHP_SELF']);
248: }
249:
250: $this->Close();
251: }
252:
253: 254: 255:
256: protected function UpdateControlInfo() {
257: $objParams = $this->generalOptions;
258: foreach ($objParams as $objParam) {
259: $objControl = $objParam->GetControl ($this->dtgGeneralOptions);
260: $strName = $objControl->Name;
261: $value = $objControl->Value;
262:
263: if (!is_null($value)) {
264: $this->params[$strName] = $value;
265: } else {
266: unset ($this->params[$strName]);
267: }
268: }
269:
270: foreach ($this->categories as $objParams) {
271: foreach ($objParams as $objParam) {
272: $objControl = $objParam->GetControl ();
273: if ($objControl) {
274: $strName = $objControl->Name;
275: $value = $objControl->Value;
276:
277: if (!is_null($value)) {
278: $this->params['Overrides'][$strName] = $value;
279: } else {
280: unset ($this->params['Overrides'][$strName]);
281: }
282: } else {
283: unset ($this->params['Overrides'][$strName]);
284: }
285: }
286: }
287:
288: if (empty($this->params['Overrides'])) {
289: unset ($this->params['Overrides']);
290: }
291: }
292:
293: 294: 295:
296: protected function WriteParams() {
297: $node = $this->objCurrentControl->LinkedNode;
298: if ($node) {
299: if ($node->_ParentNode) {
300: $strClassName = $node->_ParentNode->_ClassName;
301: $this->objModelConnectorOptions->SetOptions ($strClassName, $node->_PropertyName, $this->params);
302: $this->objModelConnectorOptions->Save();
303: } else {
304:
305: $this->objModelConnectorOptions->SetOptions ($node->_ClassName, QModelConnectorOptions::TableOptionsFieldName, $this->params);
306: $this->objModelConnectorOptions->Save();
307: }
308: }
309: }
310:
311: 312: 313: 314: 315:
316: protected function ReadParams() {
317: $node = $this->objCurrentControl->LinkedNode;
318: if ($node) {
319: if ($node->_ParentNode) {
320: $strClassName = $node->_ParentNode->_ClassName;
321: $this->params = $this->objModelConnectorOptions->GetOptions ($strClassName, $node->_PropertyName);
322: }
323: else {
324:
325: $this->params = $this->objModelConnectorOptions->GetOptions ($node->_ClassName, QModelConnectorOptions::TableOptionsFieldName);
326: }
327: return true;
328: }
329: return false;
330: }
331:
332: 333: 334: 335: 336:
337: protected function CreateClassNameArray() {
338:
339: $controls = array();
340: include (__QCUBED_CORE__ . '/control_registry.inc.php');
341:
342: if (file_exists(__APP_INCLUDES__ . '/control_registry.inc.php')) {
343: include (__APP_INCLUDES__ . '/control_registry.inc.php');
344: }
345:
346: if (defined ('__PLUGINS__') &&
347: is_dir(__PLUGINS__)) {
348: $plugins = scandir(__PLUGINS__);
349: foreach ($plugins as $dirName) {
350: if ($dirName != '.' && $dirName != '..') {
351: if (file_exists(__PLUGINS__ . '/' . $dirName . '/control_registry.inc.php')) {
352: include (__PLUGINS__ . '/' . $dirName . '/control_registry.inc.php');
353: }
354: }
355: }
356: }
357:
358:
359:
360:
361:
362: $node = $this->objCurrentControl->LinkedNode;
363: $type = $node->_Type;
364: if (($node->_Type == QType::ReverseReference && $node->IsUnique()) || $node->_Type == QType::ArrayType) {
365: $type = QControlCategoryType::SingleSelect;
366: }
367: elseif (($node->_Type == QType::ReverseReference && !$node->IsUnique()) || $node->_Type == QType::Association) {
368: $type = QControlCategoryType::MultiSelect;
369: }
370: elseif ($node->_TableName) {
371: if ($node->_ParentNode) {
372:
373: $type = QControlCategoryType::SingleSelect;
374: } else {
375:
376: $type = QControlCategoryType::Table;
377: }
378: }
379:
380: if (isset ($controls[$type])) {
381: foreach ($controls[$type] as $strClassName) {
382: $a[$strClassName] = $strClassName;
383: }
384:
385: return $a;
386: } else {
387: return null;
388: }
389:
390: }
391:
392: }
393:
394: