/** * Gets the HTML code. * * @param EcrTable $table A EcrTable object * * @return string HTML */ public function getCode(EcrTable $table) { $ret = ''; foreach ($table->getFields() as $field) { $ret .= EcrTableHelper::formatTableVar($field->name, $field->type, array($field->label)); $ret .= NL; } //foreach return $ret; }
/** * Gets the HTML code. * * @param EcrTable $table A EcrTable object * @param string $indent Indentation string * * @return string HTML */ public function getCode(EcrTable $table, $indent = '') { $ret = ''; $started = false; foreach ($table->getFields() as $field) { $ret .= $started ? $indent . ', ' : $indent . ' '; $started = true; $ret .= EcrTableHelper::formatSqlField($field); $ret .= NL; } //foreach return $ret; }
/** * Displays available options with input fields. * * @param EcrProjectBase $project The project * * @return string HTML */ public function displayOptions(EcrProjectBase $project) { ecrScript('dbtables'); ecrStylesheet('dbtables'); if (false == in_array('mysql', $project->dbTypes)) { $project->dbTypes = array_merge(array('mysql'), $project->dbTypes); } $fields = array(); $field = new EcrTableField(); $field->name = 'catid'; $field->label = 'Category id'; $field->type = 'INT'; $field->length = '11'; $field->attributes = 'UNSIGNED'; $field->null = 'NOT_NULL'; $field->comment = 'Category ID'; $fields[] = $field; $field = new EcrTableField(); $field->name = 'checked_out'; $field->label = 'Checked out'; $field->type = 'INT'; $field->length = '11'; $field->attributes = 'UNSIGNED'; $field->default = '0'; $field->null = 'NOT_NULL'; $fields[] = $field; $html = array(); $html[] = '<script type="text/javascript">'; $html[] = '//--Set object count to 3 - 0 is the standard field "id"'; $html[] = '//-- 1 is "catid" and 2 is "checked_out"'; $html[] = 'var obCount = 3;'; $html[] = 'var obCountOrig = 3;'; $html[] = '</script>'; $html[] = '<h3>' . jgettext('Database support') . '</h3>'; $html[] = EcrHtmlOptions::database($project); $html[] = '<h3>' . jgettext('User defined table fields') . '</h3>'; $html[] = '<strong>' . sprintf(jgettext('Please define the fields for the table %s that will be created for your component.'), '"' . strtolower($project->name) . '"') . '</strong>'; $html[] = EcrTableHelper::startDbEditor(); foreach ($fields as $count => $field) { $html[] = EcrTableHelper::drawPredefinedRow($field, $count + 1); } $html[] = EcrTableHelper::endDbEditor(); return implode(NL, $html); }
/** * Inserts the AutoCode into the project. * * @param EcrProjectBase $project The project. * @param array $options Insert options. * @param EcrLogger $logger EcrLogger. * * @return boolean */ public function insert(EcrProjectBase $project, $options, EcrLogger $logger) { $input = JFactory::getApplication()->input; $table_name = $input->get('element'); $var_scope = $input->get('var_scope'); $element_scope = $input->get('element_scope'); if (!$table_name) { JFactory::getApplication()->enqueueMessage(jgettext('No table given'), 'error'); return false; } $db = JFactory::getDBO(); $prefix = $db->getPrefix(); $fields = $db->getTableFields($prefix . $table_name); $table_vars = ''; $autoCodeFields = array(); $reqFields = $input->get('field', array(), 'array'); if (count($fields)) { foreach ($fields[$prefix . $table_name] as $name => $type) { $reqField = $reqFields[$name]; if (isset($project->autoCodes[$this->key]->fields[$this->key . '.var'][$name])) { $field = $project->autoCodes[$this->key]->fields[$this->key . '.var'][$name]; $field->label = $reqField['label']; } else { $field = new EcrTableField(); $field->name = $name; $field->label = $name; $field->type = $type; } $autoCodeFields[] = $field; $adds = array($field->label); $table_vars .= EcrTableHelper::formatTableVar($name, $type, $adds, $var_scope); } //foreach } $AutoCode = new EcrProjectAutocode($this->group, $this->name, $table_name, $element_scope); $AutoCode->options = array(); $AutoCode->options['varscope'] = $var_scope; $AutoCode->fields[$AutoCode->getKey() . '.var'] = $autoCodeFields; $AutoCode->codes[$AutoCode->getKey() . '.var'] = $AutoCode->enclose($table_vars, $AutoCode->getKey() . '.var'); $project->addAutoCode($AutoCode); /* * Add substitutes * * Define keys that will be substitutes in the code */ $project->addSubstitute('ECR_SUBPACKAGE', 'Tables'); $project->addSubstitute('_ECR_TABLE_NAME_', $table_name); foreach ($AutoCode->codes as $key => $code) { $project->addSubstitute($key, $code); } //foreach /* Insert the part to your project and return the results */ return $project->insertPart($options, $logger); }
<?php defined('_JEXEC') || die('=;)'); /** * @package EasyCreator * @subpackage Views * @author Nikolai Plath * @author Created on 10-Aug-2009 * @license GNU/GPL, see JROOT/LICENSE.php */ ecrScript('dbtables', 'parts', 'autocode', 'util'); ecrStylesheet('stuffer'); echo '<h2 style="color: red; float: right;">W I P !'; echo '</h2>'; $tableHelper = new EcrTableHelper(); $db = JFactory::getDBO(); $dbTables = $db->getTableList(); $dbPrefix = $db->getPrefix(); $dbName = JFactory::getConfig()->get('db'); $scopes = array('admin' => jgettext('Admin'), 'site' => jgettext('Site')); $allTables = array(); $allTables = $this->project->tables; $discoveredTables = $tableHelper->discoverTables($this->project); foreach ($discoveredTables as $table) { if (!array_key_exists($table->name, $allTables)) { $allTables[$table->name] = $table; } } //foreach $tables = array(); $infoQuery = "SHOW TABLE STATUS FROM `%s` LIKE '%s';";
/** * Get the CREATE string for a table. * * @param EcrTable $table The table * * @return string */ public static function getTableCreate(EcrTable $table) { $db = JFactory::getDBO(); $dbName = JFactory::getApplication()->getCfg('db'); $tName = $db->getPrefix() . $table->name; $s = ''; $s .= 'CREATE TABLE IF NOT EXISTS `#__' . $table->name . '` (' . NL; $pri = ''; $started = false; $indent = ''; $db->setQuery('SHOW CREATE TABLE ' . $tName); $engineQuery = 'SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = \'' . $dbName . '\' AND TABLE_NAME = \'' . $tName . '\''; $db->setQuery($engineQuery); $engine = $db->loadResult(); echo 'A' . $engine; /* // $engineQuery = ' SHOW TABLE STATUS LIKE `'.$tName.'` // FROM information_schema.TABLES // WHERE TABLE_SCHEMA = \''.$dbName.'\' // AND TABLE_NAME = \''.$tName.'\''; // $db->setQuery($engineQuery); // // $engine = $db->loadResult(); // echo 'B'.$engine; */ foreach ($table->getFields() as $field) { if ($field->key == 'PRI') { $pri = $field->name; } $s .= $started ? $indent . ', ' : $indent . ' '; $started = true; $s .= EcrTableHelper::formatSqlField($field); $s .= NL; } //foreach if ($pri) { $s .= ', PRIMARY KEY (`' . $pri . '`)'; } //-- # $s .= ') ENGINE='.$engine.' DEFAULT CHARSET='. return $s; }
/** * Inserts the AutoCode into the project. * * @param EcrProjectBase $project The project. * @param array $options Insert options. * @param EcrLogger $logger The EcrLogger. * * @return boolean */ public function insert(EcrProjectBase $project, $options, EcrLogger $logger) { JFactory::getApplication()->enqueueMessage(__METHOD__ . ' not finished', 'error'); $input = JFactory::getApplication()->input; $table_name = $input->get('element'); $element_scope = $input->get('element_scope'); if (!$table_name) { JFactory::getApplication()->enqueueMessage(jgettext('No table given'), 'error'); return false; } $fields = EcrTableHelper::getTableColumns($table_name); $reqFields = $input->get('field', array(), 'array'); $headers = ''; $cells = ''; $tags = array('<!--', '-->'); $indent = ' '; if (!count($fields)) { JFactory::getApplication()->enqueueMessage('No table fields found', 'error'); return false; } foreach ($fields as $name => $field) { $reqFieldHeader = $reqFields['header'][$name]; $reqFieldCell = $reqFields['cell'][$name]; if (isset($project->autoCodes[$this->key]->fields[$this->key . '.header'][$name])) { $fieldHeader = $project->autoCodes[$this->key]->fields[$this->key . '.header'][$name]; $fieldHeader->label = $reqFieldHeader['label']; } else { $fieldHeader = new EcrTableField(); $fieldHeader->name = $name; $fieldHeader->label = $name; } if (isset($project->autoCodes[$this->key]->fields[$this->key . '.cell'][$name])) { $fieldCell = $project->autoCodes[$this->key]->fields[$this->key . '.cell'][$name]; } else { $fieldCell = new EcrTableField(); $fieldCell->name = $name; } $fieldHeader->display = $reqFieldHeader['display']; $fieldHeader->width = $reqFieldHeader['width']; //-- Display value for cells is the same as for headers $fieldCell->display = $reqFieldHeader['display']; $autoCodeFieldsHeader[] = $fieldHeader; $autoCodeFieldsCell[] = $fieldCell; $headers .= $this->getCode('header', $fieldHeader, $indent); $cells .= $this->getCode('cell', $fieldCell, $indent); } //foreach $this->fields[$this->key . '.header'] = $autoCodeFieldsHeader; $this->codes[$this->key . '.header'] = $this->enclose($headers, $this->key . '.header', true); $this->fields[$this->key . '.cell'] = $autoCodeFieldsCell; $this->codes[$this->key . '.cell'] = $this->enclose($cells, $this->key . '.cell', true); $project->addAutoCode($this); /* * Add substitutes * * Define keys that will be substitutes in the code */ $project->addSubstitute('ECR_SUBPACKAGE', 'Views'); $project->addSubstitute('_ECR_TABLE_NAME_', $table_name); foreach ($this->codes as $key => $code) { $project->addSubstitute($tags[0] . $key . $tags[1], $code); } //foreach /* Insert the part to your project and return the results */ return $project->insertPart($options, $logger); }
/** * Inserts the AutoCode into the project. * * @param EcrProjectBase $project The project. * @param array $options Insert options. * @param EcrLogger $logger EcrLogger. * * @return boolean */ public function insert(EcrProjectBase $project, $options, EcrLogger $logger) { $input = JFactory::getApplication()->input; $table_name = $input->get('element'); $element_scope = $input->get('element_scope'); $element = 'row'; if (!$table_name) { JFactory::getApplication()->enqueueMessage(jgettext('No table given'), 'error'); return false; } $table = new EcrTable($table_name); $fields = EcrTableHelper::getTableColumns($table_name); $reqFields = $input->get('field', array(), 'array'); $rows = ''; $tags = array('<!--', '-->'); $indent = ' '; if (!count($fields)) { JFactory::getApplication()->enqueueMessage('No table fields found', 'error'); return false; } foreach ($fields as $name => $field) { $reqFieldHeader = $reqFields[$name]; if (isset($project->autoCodes[$this->key]->fields[$this->key . '.' . $element][$name])) { $fieldHeader = $project->autoCodes[$this->key]->fields[$this->key . '.' . $element][$name]; } else { $fieldHeader = new EcrTableField(); $fieldHeader->name = $name; } $fieldHeader->label = $reqFieldHeader['label']; $fieldHeader->inputType = $reqFieldHeader['input_type']; $autoCodeFieldsHeader[] = $fieldHeader; $table->addField($fieldHeader); } //foreach $rows .= $this->getCode($element, $table, $indent); $this->fields[$this->key . '.' . $element] = $autoCodeFieldsHeader; $this->codes[$this->key . '.' . $element] = $this->enclose($rows, $this->key . '.' . $element); $project->addAutoCode($this); /* * Add substitutes * * Define keys that will be substitutes in the code */ $project->addSubstitute('ECR_SUBPACKAGE', 'Views'); $project->addSubstitute('_ECR_TABLE_NAME_', $table_name); foreach ($this->codes as $key => $code) { $project->addSubstitute($tags[0] . $key . $tags[1], $code); } //foreach /* Insert the part to your project and return the results */ return $project->insertPart($options, $logger); }
/** * Inserts the part into the project. * * @param EcrProjectBase $project The project. * @param array $options Insert options. * @param EcrLogger $logger The EcrLogger. * * @return boolean */ public function insert(EcrProjectBase $project, $options, EcrLogger $logger) { $input = JFactory::getApplication()->input; $element_name = $input->get('element_name'); $table_name = $input->get('table_name'); $req_table_fields = $input->get('table_fields', array(), 'array'); $req_table_fields_edits = $input->get('table_fields_edits', array(), 'array'); $req_table_fields_types = $input->get('table_fields_types', array(), 'array'); if (!$table_name) { JFactory::getApplication()->enqueueMessage(jgettext('No table given'), 'error'); return false; } $db = JFactory::getDBO(); $prefix = $db->getPrefix(); $fields = $db->getTableFields($prefix . $table_name); $tableFields = array(); $table_vars = ''; if (count($fields)) { $tableFields = $fields[$prefix . $table_name]; foreach ($tableFields as $key => $value) { if (!in_array($key, $req_table_fields)) { continue; } $table_vars .= EcrTableHelper::formatTableVar($key, $value); } //foreach } /* * Add substitutes */ $project->addSubstitute('ECR_ELEMENT_NAME', $element_name); $project->addSubstitute('_ECR_LOWER_ELEMENT_NAME_', strtolower($element_name)); $project->addSubstitute('_ECR_TABLE_NAME_', $table_name); $project->addSubstitute('##ECR_TABLE_VARS##', $table_vars); /* * Read part options files */ $files = JFolder::files($options->pathSource . DS . 'options', '.', true, true); foreach ($files as $file) { $fileContents = JFile::read($file); if (strpos($fileContents, '<?php') === 0) { $fileContents = substr($fileContents, 6); } $project->substitute($fileContents); $project->addSubstitute('##' . strtoupper(JFile::stripExt(JFile::getName($file))) . '##', $fileContents); } //foreach /* * Add manual substitutes */ $substitutes['##ECR_VIEW1_TMPL1_THS##'] = '?>'; $substitutes['##ECR_VIEW1_TMPL1_TDS##'] = '?>'; $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] = '?>'; $i = 0; foreach ($tableFields as $key => $value) { if (!in_array($key, $req_table_fields)) { continue; } $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <th>' . NL; $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= " <?php echo JHTML::_('grid.sort', '" . $key . "', '" . $key . "', \$this->lists['order_Dir'], \$this->lists['order']);?>" . NL; $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' </th>' . NL; $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <td>' . NL; $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <?php echo $row->' . $key . '; ?>' . NL; $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' </td>' . NL; if (in_array($key, $req_table_fields_edits)) { $s = $project->getSubstitute('##ECR_VIEW2_TMPL1_OPTION2VAL##'); $s = str_replace('_ECR_FIELDVALUE_1_', $key, $s); $s = str_replace('_ECR_FIELDVALUE_2_', $req_table_fields_types[$key], $s); $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] .= $s; } $i++; } //foreach $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <?php $coloumnCount += ' . $i . '; ?>' . NL; $substitutes['##ECR_VIEW1_TMPL1_THS##'] .= ' <?php ' . NL; $substitutes['##ECR_VIEW1_TMPL1_TDS##'] .= ' <?php ' . NL; $substitutes['##ECR_VIEW2_TMPL1_OPTION2##'] .= ' <?php ' . NL; foreach ($substitutes as $key => $value) { $project->addSubstitute($key, $value); } //foreach /* * Remove options */ if (!in_array('ordering', $req_table_fields)) { $project->addSubstitute('##ECR_CONTROLLER1_OPTION1##', ''); } if (!in_array('published', $req_table_fields)) { } $project->addSubstitute('ECR_SUBPACKAGE', 'Tables'); $input->set('element_scope', 'admin'); if (!$project->insertPart($options, $logger)) { return false; } /* * Create menu link */ if ($input->get('create_menu_link', false)) { $link = 'option=' . $options->ecr_project . '&view=' . strtolower($element_name) . $project->listPostfix . '&controller=' . strtolower($element_name) . $project->listPostfix; if (!$project->addSubmenuEntry($element_name, $link)) { JFactory::getApplication()->enqueueMessage(jgettext('Unable to create menu link'), 'error'); return false; } } return true; }