Exemplo n.º 1
0
 /**
  * Method to check relations
  * @access public
  */
 public function checkTables()
 {
     $item = $this->getItem();
     $lname = strtolower(str_replace('com_', '', $item->name));
     $this->_Fields['foreigns'] = array();
     foreach ($this->_Tables as $table) {
         $Name = substr(strrchr($table, '_'), 1);
         $singular = JaccHelper::getPluralization($Name, 'singular');
         $plural = JaccHelper::getPluralization($Name, 'plural');
         $fields = $this->_Fields[$table];
         $fieldlist = $fields->all;
         $this->_TablesHas[$table] = new JObject();
         foreach ($fieldlist as &$field) {
             $part = substr($field->get('key'), 0, strpos($field->get('key'), '_'));
             $pfx = substr(strrchr($field->get('key'), '_'), 1) == 'id';
             if ($field->get('key') == 'catid' || $field->get('key') == 'category_id') {
                 $this->_TablesHas[$table]->set('category', $field->get('key'));
                 $primary = $fieldlist['primary'];
                 $this->_extensionXml .= "\n            <extension>\n" . "                <name>" . $singular . "</name>\n" . "                <listview>" . $plural . "</listview>\n" . "                <display>" . $fieldlist['hident']->get('key') . "</display>\n" . "                <field>" . $field->get('key') . "</field>\n" . "                <statefield>" . $fieldlist['state']->get('key') . "</statefield>\n" . "                <primary>" . $fieldlist['primary']->get('key') . "</primary>\n" . "                <table>" . $table . "</table>\n" . "            </extension>\n";
             } else {
                 $this->_TablesHas[$table]->set($field->get('key'), $field->get('key'));
             }
             if ($part && $pfx && ($foreigntable = $this->_findTable($part))) {
                 $field->set('foreignkey', $part);
                 $field->set('reltable', $foreigntable);
                 $field->set('formfield', $lname . $part);
                 $field->set('size', '1');
                 $field->set('label', ucfirst($part));
                 $field->set('fieldtype', $lname . $part);
                 $field->set('required', 'true');
                 $ffieldslist = $this->_Fields[$foreigntable];
                 $ffields = $ffieldslist->get('all');
                 $ffield = $ffields['hident'];
                 $ffieldprim = $ffields['primary'];
                 $this->_Fields['foreigns'][$field->get('key')] = new JObject();
                 $this->_Fields['foreigns'][$field->get('key')]->set('key', $field->get('key'));
                 $this->_Fields['foreigns'][$field->get('key')]->set('name', $part);
                 $this->_Fields['foreigns'][$field->get('key')]->set('hident', $ffield->get('key'));
                 $this->_Fields['foreigns'][$field->get('key')]->set('primary', $ffieldprim->get('key'));
                 $this->_Fields['foreigns'][$field->get('key')]->set('reltable', $foreigntable);
             }
         }
     }
 }
Exemplo n.º 2
0
 public function zip()
 {
     require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'jacc.php';
     jimport('joomla.filesystem.archive');
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     jimport('joomla.filtere.filteroutput');
     $db = JFactory::getDBO();
     $zipdir = JPATH_COMPONENT . DS . 'archives';
     $model = $this->getModel($this->_mainmodel);
     //Get Model
     $item = $model->getItem();
     $archive = $item->name . '-' . $item->version . '.zip';
     $model->addPath('archive', $zipdir . DS . $archive);
     $mvcdyn_dir = JPATH_COMPONENT . DS . 'templates' . DS . 'mvccomponent' . DS;
     $elements_dir = JPATH_COMPONENT . DS . 'templates' . DS . 'elements' . DS;
     //some basic variables to replace the template patterns
     //now
     $date = JFactory::getDate();
     $com_component = $item->name;
     JRequest::setVar('component', $com_component);
     //Component name for camel case functions
     $component = ucfirst(strtolower(str_replace('com_', '', $com_component)));
     //lower Component name
     $lcomponent = strtolower(str_replace('com_', '', $com_component));
     $model->addRenaming($lcomponent, '#component#');
     //User wants to create archive from installed component
     if ($item->use > 1) {
         return $this->zipLiveComponent($model);
     }
     //get db prefix
     $config = JFactory::getConfig();
     $dbprefix = version_compare(JVERSION, '3.0', 'lt') ? $config->getValue('config.dbprefix') : $config->get('dbprefix');
     //categories included?
     $uses_categories = $item->params->get('uses_categories');
     if (!$uses_categories) {
         $model->addExcludes(array('.svn', 'CVS', 'category', 'categories', 'category.php', 'categories.php', 'tablenested.php'));
     }
     //Create temp folder
     if (JFolder::exists($model->getTempPath())) {
         JFolder::delete($model->getTempPath());
     }
     // copy Basic MVC to tmp/jctmp
     JFolder::copy(JPATH_COMPONENT . '/templates/mvcbase', $model->getTempPath());
     //the text view to read the mvc triple templates
     $view = $this->getView($this->_viewname, 'text');
     //initiate variables
     $sqlstring = "";
     $dropsqlstring = "";
     $submenu = "";
     $addsubmenu = "";
     $helpersubmenu = "";
     $menuhelper = "";
     $defaultview = "";
     $extensions = "";
     $routerswitch = "";
     $filesCopy = array();
     //the view/model/controller name of the first table
     $firstName = "";
     $syslanguage = strtoupper(str_replace('com_', '', $com_component)) . "=\"" . ucfirst($component) . "\"\n";
     $menu_tmpl = file_get_contents($elements_dir . 'menu.php');
     $catmenu_tmpl = file_get_contents($elements_dir . 'catmenu.php');
     $router_tmpl = file_get_contents($elements_dir . 'router.php');
     if (count($item->tables)) {
         $firstName = substr(strrchr($item->tables[0], '_'), 1);
         foreach ($item->tables as $table) {
             //Get the fields
             $fields = version_compare(JVERSION, '3.0', 'lt') ? $db->getTableFields($table, false) : array($table => $db->getTableColumns($table, false));
             $model->setTableFields($table, JaccHelper::getSpecialFields($fields[$table]));
         }
         //Check tables for relations - foreign keys etc
         $model->checkTables();
         $ftmpl = file_get_contents($mvcdyn_dir . 'admin/models/fields/field.php');
         $foreignkeys = $model->getTableFields('foreigns');
         foreach ($foreignkeys as $field) {
             // create formfield
             $data = JaccHelper::_replace($ftmpl, $item, (array) $field);
             file_put_contents($model->getTempPath(true) . 'admin' . DS . 'models' . DS . 'fields' . DS . $lcomponent . $field->get('name') . '.php', $data);
         }
         $hasnoImages = true;
         foreach ($item->tables as $table) {
             if ($model->TableHas($table, 'image')) {
                 $hasnoImages = false;
             }
             //last part of table name as class name
             $name = substr(strrchr($table, '_'), 1);
             //the table name used as last part of camel case class names
             $model->setMvcTable($table);
             //the install sql
             $sqlstring .= JaccHelper::export_table_structure($table);
             //uninstall sql
             $dropsqlstring .= 'DROP TABLE IF EXISTS ' . str_replace($dbprefix, '#_', $table) . ";\n";
             //create temp folders
             JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $name);
             JaccHelper::createFolder($model->getTempPath(true) . 'admin' . DS . 'views' . DS . $name);
             JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $name . DS . 'tmpl');
             JaccHelper::createFolder($model->getTempPath(true) . 'admin' . DS . 'views' . DS . $name . DS . 'tmpl');
             JaccHelper::createFolder($model->getTempPath(true) . 'admin' . DS . 'sql');
             $plural = JaccHelper::getPluralization($name, 'plural');
             $name = JaccHelper::getPluralization($name, 'singular');
             //First table as main triple
             if (!$defaultview) {
                 $defaultview = $plural;
             }
             $options = array('firstname' => $name, 'plural' => $plural);
             $menuhelper .= JaccHelper::_replace($menu_tmpl, $item, $options);
             $routerswitch .= JaccHelper::_replace($router_tmpl, $item, $options);
             $syslanguage .= strtoupper($name) . "=\"" . ucfirst($name) . "\"\n";
             //Submenu for admin, if more than one table selected
             if (count($item->tables) > 1) {
                 $submenu .= "\t\t  <menu link=\"option=" . $com_component . "&amp;view=" . $plural . "\">" . ucfirst($plural) . "</menu>\n";
             }
             $sourcexml = file_get_contents(JPATH_COMPONENT_ADMINISTRATOR . '/models/sources.xml');
             $sources = JFactory::getXML(str_replace(array('##name##', '##plural##'), array($name, $plural), $sourcexml), false);
             $mvcelements = $sources->xpath('default/item');
             $lang_str = strtoupper($com_component) . "_" . strtoupper($name);
             //If there is no category_id present, we use a simple list view
             if (!$model->TableHas($table, 'category')) {
                 JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $plural);
                 JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $plural . DS . 'tmpl');
             } elseif ($uses_categories) {
                 $submenu .= "\t\t  <menu link=\"option=com_categories&amp;extension=" . $com_component . "." . $name . "\">" . $lang_str . "_CATEGORIES_TITLE</menu>\n";
                 $syslanguage .= $lang_str . "_CATEGORIES_TITLE=\"" . ucfirst($name) . " Categories\"\n";
                 $options = array('firstname' => $lang_str . "_CATEGORIES_TITLE", 'extension' => $name);
                 $menuhelper .= JaccHelper::_replace($catmenu_tmpl, $item, $options);
                 $extensions[] = $name;
             }
             //Read the template for each mvc element
             foreach ($mvcelements as $template) {
                 JRequest::setVar('type', $template->id);
                 $model->setMvcTemplate($template->source);
                 $model->setMvcTemplate($template->source);
                 $model->setMvcElementtype($template->id);
                 $view->setModel($model, true);
                 $view->set('ItemSingular', $name);
                 $view->set('ItemPlural', $plural);
                 $data = $view->display();
                 $path = $model->getTempPath(true) . $template->folder;
                 if (!JFolder::exists($path)) {
                     JaccHelper::createFolder($path);
                 }
                 file_put_contents($path . '/' . $template->name . '.' . $template->ext, $data);
             }
         }
     }
     if ($hasnoImages) {
         $model->addExcludes(array('imageup'));
     }
     //additional user defined Controller/View Pairs, that do not relate to a table
     $vcpairs = $item->params->get('views');
     if (is_array($vcpairs) && count($vcpairs)) {
         foreach ($vcpairs as $options) {
             if (!trim($options['name'])) {
                 continue;
             }
             $options['name'] = JFilterOutput::stringURLSafe($options['name']);
             if (!isset($options['option'])) {
                 $options['option'] = 'both';
             }
             $replaceoptions = array('firstname' => $options['name'], 'plural' => false);
             //create the files
             $this->copyCVPair($options);
             //Make a submenu, if the pair is created for backend
             if ($options['option'] == 'backend' || $options['option'] == 'both') {
                 $menuhelper .= JaccHelper::_replace($menu_tmpl, $item, $replaceoptions);
                 $addsubmenu .= "\t\t  <menu  link=\"option=" . $com_component . "&amp;view=" . $options['name'] . "\">" . ucfirst($options['name']) . "</menu>\n";
                 $routerswitch .= JaccHelper::_replace($router_tmpl, $item, $replaceoptions);
                 $syslanguage .= strtoupper($options['name']) . "=\"" . ucfirst($options['name']) . "\"\n";
             }
         }
     }
     //write the extension.xml
     $extensionxmlfile = file_get_contents($model->getTempPath(true) . 'admin' . DS . 'elements' . DS . 'extensions.xml');
     file_put_contents($model->getTempPath(true) . 'admin' . DS . 'elements' . DS . 'extensions.xml', str_replace('##extensionsxml##', $model->getExtensionXml(), $extensionxmlfile));
     //write install.mysql.sql and uninstall.mysql.sql'
     file_put_contents($model->getTempPath(true) . 'admin' . DS . 'sql' . DS . 'install.mysql.sql', $sqlstring);
     file_put_contents($model->getTempPath(true) . 'admin' . DS . 'sql' . DS . 'uninstall.mysql.sql', $dropsqlstring);
     $submenu .= $addsubmenu;
     $options = array('menuhelper' => $menuhelper . $helpersubmenu, 'routerswitch' => $routerswitch, 'syslanguage' => $syslanguage, 'defaultview' => $defaultview, 'submenu' => $submenu, 'firstname' => $firstName);
     $model->readinFiles();
     $model->customizeFiles($options);
     //Delete old archive if exists
     if (JFile::exists($zipdir . DS . $archive)) {
         JFile::delete($zipdir . DS . $archive);
     }
     $model->readinFiles();
     $model->buildArchiveContent();
     //create archive
     $model->createArchive();
     //delete tmp folder
     if (JFolder::exists(JPATH_SITE . DS . 'tmp' . DS . 'jctmp')) {
         JFolder::delete(JPATH_SITE . DS . 'tmp' . DS . 'jctmp');
     }
 }