/** * 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; }
/** * 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); }
/** * 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; }