Exemplo n.º 1
0
 /**
  * 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;
 }