Esempio n. 1
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 . 'mvctriple' . 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 . DS . 'templates' . DS . 'mvcbase', $model->getTempPath());
     //the text view to read the mvc triple templates
     $view =& $this->getView($this->_viewname, 'text');
     //initiate variables
     $sqlstring = "";
     $dropsqlstring = "";
     $mainlink = "";
     $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');
     $router_tmpl = file_get_contents($elements_dir . 'router.php');
     if (count($item->tables)) {
         $firstName = substr(strrchr($item->tables[0], '_'), 1);
         if ($uses_categories) {
             //need a submenu for categories
             //TODO: check if catid or category_id in table
             $addsubmenu = "\t\t  <menu  link=\"option=" . $com_component . "&amp;view=categories\">Categories</menu>\n";
             $options = array('firstname' => 'categories');
             $helpersubmenu = JaccHelper::_replace($menu_tmpl, $item, $options);
             $syslanguage .= "CATEGORIES=\"Categories\"\n";
         }
         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();
         $foreignkeys = $model->getTableFields('foreigns');
         $ftmpl = file_get_contents($mvcdyn_dir . 'default_modelfield.php');
         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');
             //First table as main triple
             if (!$mainlink) {
                 $mainlink = 'option=' . $com_component . '&amp;view=' . $name;
                 $defaultview = $name;
             }
             $options = array('firstname' => $name);
             $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=" . $name . "\">" . ucfirst($name) . "</menu>\n";
             }
             //Create an array for the MVC elements
             $mvcfiles = array('model' => array('folders' => array('admin' . DS . 'models'), 'ext' => 'php', 'name' => $name), 'modelfrontend' => array('folders' => array('site' . DS . 'models'), 'ext' => 'php', 'name' => $name), 'xmlmodel' => array('folders' => array('admin' . DS . 'models' . DS . 'forms'), 'ext' => 'xml', 'name' => $name), 'element' => array('folders' => array('admin' . DS . 'elements'), 'ext' => 'php', 'name' => $name), 'table' => array('folders' => array('admin' . DS . 'tables'), 'ext' => 'php', 'name' => $name), 'controller' => array('folders' => array('admin' . DS . 'controllers'), 'ext' => 'php', 'name' => $name), 'viewfrontend' => array('folders' => array('site' . DS . 'views' . DS . $name), 'ext' => 'php', 'name' => 'view.html'), 'view' => array('folders' => array('admin' . DS . 'views' . DS . $name), 'ext' => 'php', 'name' => 'view.html'), 'metadata' => array('folders' => array('site' . DS . 'views' . DS . $name), 'ext' => 'xml', 'name' => 'metadata'), 'tmplfrontend' => array('folders' => array('site' . DS . 'views' . DS . $name . DS . 'tmpl'), 'ext' => 'php', 'name' => 'default'), 'templ' => array('folders' => array('admin' . DS . 'views' . DS . $name . DS . 'tmpl'), 'ext' => 'php', 'name' => 'default'), 'defaultxml' => array('folders' => array('site' . DS . 'views' . DS . $name . DS . 'tmpl'), 'ext' => 'xml', 'name' => 'default'), 'form' => array('folders' => array('admin' . DS . 'views' . DS . $name . DS . 'tmpl'), 'ext' => 'php', 'name' => 'form'));
             //If there is no category_id present, we use a simple list view
             if (!$model->TableHas($table, 'category')) {
                 $mvcfiles['viewlist'] = array('folders' => array('site' . DS . 'views' . DS . $name . 'list'), 'ext' => 'php', 'name' => 'view.html');
                 $mvcfiles['modellist'] = array('folders' => array('site' . DS . 'models'), 'ext' => 'php', 'name' => $name . 'list');
                 $mvcfiles['listtmpl'] = array('folders' => array('site' . DS . 'views' . DS . $name . 'list' . DS . 'tmpl'), 'ext' => 'php', 'name' => 'default');
                 $mvcfiles['defaultlistxml'] = array('folders' => array('site' . DS . 'views' . DS . $name . 'list' . DS . 'tmpl'), 'ext' => 'xml', 'name' => 'default');
                 JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $name . 'list');
                 JaccHelper::createFolder($model->getTempPath(true) . 'site' . DS . 'views' . DS . $name . 'list' . DS . 'tmpl');
             } else {
                 $extensions[] = $name;
             }
             //Read the template for each mvc element
             foreach ($mvcfiles as $template => $options) {
                 JRequest::setVar('type', $template);
                 $model->setMvcTemplate($template);
                 $view->setModel($model, true);
                 $data = $view->display();
                 foreach ($options['folders'] as $folder) {
                     file_put_contents($model->getTempPath(true) . $folder . DS . $options['name'] . '.' . $options['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']);
             //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));
     //Get Install sql for categories if required
     if ($uses_categories) {
         $sqlstring .= JaccHelper::getCatsql($lcomponent);
     }
     if ($uses_categories) {
         $dropsqlstring .= 'DROP TABLE IF EXISTS #__' . $lcomponent . "_categories;\n";
     }
     //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');
     }
 }