コード例 #1
0
ファイル: script.php プロジェクト: ranrolls/ras-full-portal
 function installAndUpdate()
 {
     require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'joomla_compat.php';
     $db = JFactory::getDBO();
     $tables = CBCompat::getTableFields(JFactory::getDBO()->getTableList());
     // articles updates
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_articles'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_articles']['type'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_articles` ADD `type` VARCHAR( 55 ) NOT NULL DEFAULT '' AFTER `article_id` , ADD `reference_id` VARCHAR( 100 ) NOT NULL DEFAULT '' AFTER `type` ");
             JFactory::getDBO()->query();
         }
     }
     // storages updates
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_storages'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_storages']['bytable'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_storages` ADD `bytable` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `title`  ");
             JFactory::getDBO()->query();
         }
     }
     // forms updates
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['initial_order_dir'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `initial_order_dir` VARCHAR( 4 ) NOT NULL DEFAULT 'desc' AFTER `initial_sort_order` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['list_rating'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `list_rating` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `theme_plugin` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['rating_slots'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `rating_slots` TINYINT( 1 ) NOT NULL DEFAULT '5' AFTER `list_rating`  ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['rand_date_update'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `rand_date_update` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `rating_slots` , ADD INDEX ( `rand_date_update` )   ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['rand_update'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `rand_update` INT NOT NULL DEFAULT '86400' AFTER `rand_date_update` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['article_record_impact_publish'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `article_record_impact_publish` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `rand_update` , ADD `article_record_impact_language` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `article_record_impact_publish` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['allow_external_filter'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `allow_external_filter` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `article_record_impact_language` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['show_filter'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `show_filter` TINYINT( 1 ) NOT NULL DEFAULT '1' AFTER `allow_external_filter`  ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['show_records_per_page'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `show_records_per_page` TINYINT( 1 ) NOT NULL DEFAULT '1' AFTER `show_filter`  ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['initial_list_limit'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `initial_list_limit` TINYINT NOT NULL DEFAULT '20' AFTER `show_records_per_page` ");
             JFactory::getDBO()->query();
             // exceptionally here
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_articles` ADD INDEX ( `type` )");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['tag'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `tag` VARCHAR( 255 ) NOT NULL DEFAULT '' AFTER `name`, ADD INDEX ( `tag` ) ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['save_button_title'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `save_button_title` VARCHAR( 255 ) NOT NULL DEFAULT '' AFTER `initial_list_limit` , ADD `apply_button_title` VARCHAR( 255 ) NOT NULL DEFAULT '' AFTER `save_button_title` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['filter_exact_match'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `filter_exact_match` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `save_button_title`");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_forms']['initial_sort_order2'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` ADD `initial_sort_order2` VARCHAR( 255 ) NOT NULL DEFAULT '-1' AFTER `initial_sort_order` , ADD `initial_sort_order3` VARCHAR( 255 ) NOT NULL DEFAULT '-1' AFTER `initial_sort_order2` ");
             JFactory::getDBO()->query();
         }
     }
     // elements updates
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_elements'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_elements']['order_type'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_elements` ADD `order_type` VARCHAR( 255 ) NOT NULL DEFAULT '' AFTER `published` ");
             JFactory::getDBO()->query();
         }
     }
     // records updates
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['rating_sum'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `rating_sum` INT( 10 ) NOT NULL DEFAULT '0' AFTER `is_future` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['rating_count'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `rating_count` INT( 10 ) NOT NULL DEFAULT '0' AFTER `rating_sum` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['lastip'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `lastip` VARCHAR( 50 ) NOT NULL DEFAULT '' AFTER `rating_count`  ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['type'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `type` VARCHAR( 255 ) NOT NULL DEFAULT '' AFTER `id` ");
             JFactory::getDBO()->query();
         }
         JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD INDEX ( `type` )");
         JFactory::getDBO()->query();
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['session_id'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `session_id` VARCHAR( 32 ) NOT NULL DEFAULT '' AFTER `lastip` ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['rand_date'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `rand_date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `published` , ADD INDEX ( `rand_date` ) ");
             JFactory::getDBO()->query();
         }
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_records']['metadesc'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` ADD `metakey` TEXT NOT NULL ,ADD `metadesc` TEXT NOT NULL ,ADD `robots` VARCHAR( 255 ) NOT NULL DEFAULT '',ADD `author` VARCHAR( 255 ) NOT NULL DEFAULT '',ADD `rights` VARCHAR( 255 ) NOT NULL DEFAULT '',ADD `xreference` VARCHAR( 255 ) NOT NULL DEFAULT ''");
             JFactory::getDBO()->query();
         }
     }
     // element access
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_resource_access'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_resource_access']['type'])) {
             // sorry but we have to truncate the table
             JFactory::getDBO()->setQuery("TRUNCATE TABLE `#__contentbuilder_resource_access`");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_resource_access` ADD `type` VARCHAR( 100 ) NOT NULL DEFAULT '' FIRST  ");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_resource_access` DROP INDEX `form_id` ");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_resource_access` ADD UNIQUE ( `type` , `element_id` , `resource_id` )");
             JFactory::getDBO()->query();
         }
     }
     // rating cache
     if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_rating_cache'])) {
         JFactory::getDBO()->setQuery("CREATE TABLE `#__contentbuilder_rating_cache` (\n                    `record_id` varchar(255) NOT NULL DEFAULT '',\n                    `form_id` int(11) NOT NULL DEFAULT '0',\n                    `ip` varchar(50) NOT NULL DEFAULT '',\n                    `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n                    KEY `record_id` (`record_id`,`form_id`,`ip`),\n                    KEY `date` (`date`)\n                    ) ;");
         JFactory::getDBO()->query();
     }
     // switching to ints for record_id and reference_id
     if (isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_verifications'])) {
         if (!isset($tables[JFactory::getDBO()->getPrefix() . 'contentbuilder_verifications']['create_invoice'])) {
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_verifications` ADD `create_invoice` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `verification_data` ");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_articles` CHANGE `reference_id` `reference_id` INT NOT NULL DEFAULT '0', CHANGE `record_id` `record_id` BIGINT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_elements` CHANGE `reference_id` `reference_id` INT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_forms` CHANGE `reference_id` `reference_id` INT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_list_records` CHANGE `record_id` `record_id` BIGINT NOT NULL DEFAULT '0', CHANGE `reference_id` `reference_id` INT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_rating_cache` CHANGE `record_id` `record_id` BIGINT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_registered_users` CHANGE `record_id` `record_id` BIGINT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_records` CHANGE `record_id` `record_id` BIGINT NOT NULL DEFAULT '0', CHANGE `reference_id` `reference_id` INT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
             JFactory::getDBO()->setQuery("ALTER TABLE `#__contentbuilder_resource_access` CHANGE `element_id` `element_id` INT NOT NULL DEFAULT '0'");
             JFactory::getDBO()->query();
         }
     }
     // trying to ease the 0.9 update pain
     $db->setQuery("Select `type`,`reference_id` From #__contentbuilder_forms");
     $typeref = $db->loadAssocList();
     foreach ($typeref as $tr) {
         $db->setQuery("Update #__contentbuilder_records Set `type` = " . $db->Quote($tr['type']) . " Where `type` = '' And reference_id = " . $db->Quote($tr['reference_id']));
         $db->query();
     }
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     jimport('joomla.version');
     // cleaning up leftovers (additional list view overrides, that didn't work with J! 1.7
     for ($a = 1; $a <= 5; $a++) {
         if (JFile::exists(JPATH_SITE . DS . 'components' . DS . 'com_contentbuilder' . DS . 'views' . DS . 'list' . DS . 'tmpl' . DS . 'custom' . $a . '.php')) {
             JFile::delete(JPATH_SITE . DS . 'components' . DS . 'com_contentbuilder' . DS . 'views' . DS . 'list' . DS . 'tmpl' . DS . 'custom' . $a . '.php');
         }
         if (JFile::exists(JPATH_SITE . DS . 'components' . DS . 'com_contentbuilder' . DS . 'views' . DS . 'list' . DS . 'tmpl' . DS . 'custom' . $a . '.xml')) {
             JFile::delete(JPATH_SITE . DS . 'components' . DS . 'com_contentbuilder' . DS . 'views' . DS . 'list' . DS . 'tmpl' . DS . 'custom' . $a . '.xml');
         }
     }
     $version = new JVersion();
     $plugins = $this->getPlugins();
     $base_path = JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'plugins';
     $folders = JFolder::folders($base_path);
     $installer = new JInstaller();
     foreach ($folders as $folder) {
         echo 'Installing plugin <b>' . $folder . '</b><br/>';
         $success = $installer->install($base_path . DS . $folder);
         if (!$success) {
             echo 'Install failed for plugin <b>' . $folder . '</b><br/>';
         }
         echo '<hr/>';
     }
     foreach ($plugins as $folder => $subplugs) {
         foreach ($subplugs as $plugin) {
             if (version_compare($version->getShortVersion(), '1.6', '>=')) {
                 $db->setQuery('Update #__extensions Set `enabled` = 1 WHERE `type` = "plugin" AND `element` = "' . $plugin . '" AND `folder` = "' . $folder . '"');
             } else {
                 $db->setQuery('Update #__plugins Set `published` = 1 WHERE `element` = "' . $plugin . '" AND `folder` = "' . $folder . '"');
             }
             $db->query();
             echo 'Published plugin ' . $plugin . '<hr/>';
         }
     }
 }
コード例 #2
0
ファイル: storage.php プロジェクト: ranrolls/ras-full-portal
 function store($post_replace = null)
 {
     $isNew = false;
     $db = JFactory::getDBO();
     $row = $this->getTable();
     $storage = $this->getStorage();
     $storage_id = 0;
     if ($post_replace === null) {
         $data = JRequest::get('post');
     } else {
         $data = $post_replace;
     }
     $bytable = isset($data['bytable']) ? $data['bytable'] : '';
     if (isset($data['bytable'])) {
         unset($data['bytable']);
     }
     // forcing to lower as database exports may lead to tablename lowering
     $data['name'] = isset($data['name']) ? strtolower($data['name']) : '';
     if ($bytable) {
         $data['bytable'] = 1;
         $newname = $bytable;
         $data['name'] = $newname;
         if (!trim($data['title'])) {
             $newtitle = $newname;
         } else {
             $newtitle = trim($data['title']);
         }
         $data['title'] = $newtitle;
     } else {
         $data['bytable'] = 0;
         $newname = str_replace(array(' ', "\n", "\r", "\t"), array(''), preg_replace("/[^a-zA-Z0-9_\\s]/isU", "_", trim($data['name'])));
         $newname = preg_replace("/^([0-9\\s])/isU", "field\$1\$2", $newname);
         $newname = $newname == '' ? 'field' . mt_rand(0, mt_getrandmax()) : $newname;
         // required for csv
         $this->target_table = $newname;
         $data['name'] = $newname;
         if (!trim($data['title'])) {
             $newtitle = $newname;
         } else {
             $newtitle = trim($data['title']);
         }
         $data['title'] = $newtitle;
     }
     $listnames = isset($data['itemNames']) ? $data['itemNames'] : array();
     $listtitles = isset($data['itemTitles']) ? $data['itemTitles'] : array();
     $listisgroup = isset($data['itemIsGroup']) ? $data['itemIsGroup'] : array();
     $listgroupdefinitions = JRequest::getVar('itemGroupDefinitions', array(), 'POST', 'ARRAY', JREQUEST_ALLOWRAW);
     unset($data['itemIsGroup']);
     unset($data['itemGroupDefinitions']);
     unset($data['itemNames']);
     unset($data['itemTitles']);
     // case of new field
     $newfieldname = '';
     $newfieldtitle = '';
     $is_group = 0;
     $group_definition = '';
     $fieldexists = false;
     if (isset($data['fieldname']) && trim($data['fieldname'])) {
         $newfieldname = str_replace(array(' ', "\n", "\r", "\t"), array('_'), preg_replace("/[^a-zA-Z0-9_\\s]/isU", "_", trim($data['fieldname'])));
         $newfieldname = preg_replace("/^([0-9\\s])/isU", "field\$1\$2", $newfieldname);
         $newfieldname = $newfieldname == '' ? 'field' . mt_rand(0, mt_getrandmax()) : $newfieldname;
         if (!trim($data['fieldtitle'])) {
             $newfieldtitle = $newfieldname;
         } else {
             $newfieldtitle = trim($data['fieldtitle']);
         }
         $this->_db->setQuery("Select `name` From #__contentbuilder_storage_fields Where `name` = " . $this->_db->Quote($newfieldname) . " And storage_id = " . JRequest::getInt('id', 0));
         $fieldexists = $this->_db->loadResult();
         if ($fieldexists) {
             $newfieldname = $fieldexists;
         }
         $is_group = intval($data['is_group']);
         $group_definition = $data['group_definition'];
         unset($data['is_group']);
         unset($data['group_definition']);
         unset($data['fieldname']);
         unset($data['fieldtitle']);
     }
     if (!$row->bind($data)) {
         $this->setError($this->_db->getErrorMsg());
         return false;
     }
     if (!$row->check()) {
         $this->setError($this->_db->getErrorMsg());
         return false;
     }
     $storeRes = $row->store();
     if (!$storeRes) {
         $this->setError($this->_db->getErrorMsg());
         return false;
     } else {
         if (intval($data['id']) != 0) {
             $storage_id = intval($data['id']);
         } else {
             $isNew = true;
             $storage_id = $this->_db->insertid();
             $this->_id = $storage_id;
         }
         // required for csv
         $this->_id = $storage_id;
     }
     $row->reorder();
     $this->_db->setQuery("Select Max(ordering)+1 From #__contentbuilder_storage_fields Where storage_id = " . $this->_id . "");
     $max = intval($this->_db->loadResult());
     // we have a new field, so let's add it
     if (!$bytable && $this->_id && $newfieldname && !$fieldexists) {
         $this->_db->setQuery("Insert Into #__contentbuilder_storage_fields (ordering, storage_id,`name`,`title`,`is_group`,`group_definition`) Values ({$max}," . intval($this->_id) . "," . $this->_db->Quote($newfieldname) . "," . $this->_db->Quote($newfieldtitle) . "," . $is_group . "," . $this->_db->Quote($group_definition) . ")");
         $this->_db->query();
     }
     // table
     // create or update the corresponding table, field synch below
     $last_update = JFactory::getDate();
     $last_update = CBCompat::toSql($last_update);
     $tables = CBCompat::getTableFields(JFactory::getDBO()->getTableList());
     if (!$bytable && !isset($tables[JFactory::getDBO()->getPrefix() . $data['name']])) {
         if ($storage->name && isset($tables[JFactory::getDBO()->getPrefix() . $storage->name])) {
             $this->_db->setQuery("Rename Table #__" . $storage->name . " To #__" . $data['name']);
             $this->_db->query();
         } else {
             try {
                 $this->_db->setQuery('
                 CREATE TABLE `#__' . $data['name'] . '` (
                 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                 `storage_id` INT NOT NULL DEFAULT "' . $this->_id . '",
                 `user_id` INT NOT NULL DEFAULT "0",
                 `created` DATETIME NOT NULL DEFAULT "' . $last_update . '",
                 `created_by` VARCHAR( 255 ) NOT NULL DEFAULT "",
                 `modified_user_id` INT NOT NULL DEFAULT "0",
                 `modified` DATETIME NOT NULL DEFAULT "0000-00-00 00:00:00",
                 `modified_by` VARCHAR( 255 ) NOT NULL DEFAULT ""
                 ) ;
                 ');
                 $this->_db->query();
                 JFactory::getDBO()->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD INDEX ( `storage_id` )");
                 JFactory::getDBO()->query();
                 JFactory::getDBO()->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD INDEX ( `user_id` )");
                 JFactory::getDBO()->query();
                 JFactory::getDBO()->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD INDEX ( `created` )");
                 JFactory::getDBO()->query();
                 JFactory::getDBO()->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD INDEX ( `modified_user_id` )");
                 JFactory::getDBO()->query();
                 JFactory::getDBO()->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD INDEX ( `modified` )");
                 JFactory::getDBO()->query();
             } catch (Exception $e) {
             }
         }
     } else {
         if ($bytable) {
             // creating the storage fields in custom table if not existing already
             $system_fields = array('id', 'storage_id', 'user_id', 'created', 'created_by', 'modified_user_id', 'modified', 'modified_by');
             $allfields = array();
             $fieldin = '';
             $fields = $tables[$data['name']];
             foreach ($fields as $field => $type) {
                 $fieldin .= "'" . $field . "',";
             }
             $fieldin = rtrim($fieldin, ',');
             if ($fieldin) {
                 $this->_db->setQuery("Select `name` From #__contentbuilder_storage_fields Where `name` In (" . $fieldin . ") And storage_id = " . $this->_id);
                 jimport('joomla.version');
                 $version = new JVersion();
                 if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                     $fieldnames = $this->_db->loadColumn();
                 } else {
                     $fieldnames = $this->_db->loadResultArray();
                 }
                 foreach ($fields as $field => $type) {
                     if (!in_array($field, $fieldnames) && !in_array($field, $system_fields)) {
                         $this->_db->setQuery("Insert Into #__contentbuilder_storage_fields (ordering,storage_id,`name`,`title`,`is_group`,`group_definition`) Values ({$max}," . intval($this->_id) . "," . $this->_db->Quote($field) . "," . $this->_db->Quote($field) . ",0,'')");
                         $this->_db->query();
                     }
                     $allfields[] = $field;
                 }
                 // now we add missing system columns into the custom table
                 try {
                     foreach ($system_fields as $missing) {
                         if (!in_array($missing, $allfields)) {
                             switch ($missing) {
                                 case 'id':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ");
                                     $this->_db->query();
                                     break;
                                 case 'storage_id':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `storage_id` INT NOT NULL DEFAULT " . $this->_id . ", ADD INDEX ( `storage_id` )");
                                     $this->_db->query();
                                     break;
                                 case 'user_id':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `user_id` INT NOT NULL DEFAULT 0, ADD INDEX ( `user_id` ) ");
                                     $this->_db->query();
                                     break;
                                 case 'created':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `created` DATETIME NOT NULL DEFAULT '" . $last_update . "', ADD INDEX ( `created` ) ");
                                     $this->_db->query();
                                     break;
                                 case 'created_by':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `created_by` VARCHAR( 255 ) NOT NULL DEFAULT '' ");
                                     $this->_db->query();
                                     break;
                                 case 'modified_user_id':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `modified_user_id` INT NOT NULL DEFAULT 0, ADD INDEX ( `modified_user_id` ) ");
                                     $this->_db->query();
                                     break;
                                 case 'modified':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `modified` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', ADD INDEX ( `modified` ) ");
                                     $this->_db->query();
                                     break;
                                 case 'modified_by':
                                     $this->_db->setQuery("ALTER TABLE `" . $data['name'] . "` ADD `modified_by` VARCHAR( 255 ) NOT NULL DEFAULT '' ");
                                     $this->_db->query();
                                     break;
                             }
                         }
                     }
                 } catch (Exception $e) {
                 }
                 // importing records
                 if ($isNew) {
                     $this->_db->setQuery("Alter Table `" . $data['name'] . "` Alter Column `storage_id` Set Default '" . $this->_id . "'");
                     $this->_db->query();
                     $this->_db->setQuery("Update `" . $data['name'] . "` Set `storage_id` = '" . $this->_id . "'");
                     $this->_db->query();
                     $this->_db->setQuery("Select id From `" . $data['name'] . "`");
                     jimport('joomla.version');
                     $version = new JVersion();
                     if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                         $third_party_ids = $this->_db->loadColumn();
                     } else {
                         $third_party_ids = $this->_db->loadResultArray();
                     }
                     foreach ($third_party_ids as $third_party_id) {
                         $this->_db->setQuery("Insert Into #__contentbuilder_records (\n                            `type`,\n                            last_update,\n                            is_future,\n                            lang_code, \n                            sef, \n                            published, \n                            record_id, \n                            reference_id\n                        ) \n                        Values \n                        (\n                            'com_contentbuilder',\n                            " . $this->_db->Quote($last_update) . ",\n                            0,\n                            '*',\n                            '',\n                            1,\n                            " . $this->_db->Quote(intval($third_party_id)) . ",\n                            " . $this->_db->Quote($this->_id) . "\n                        )");
                         // ignore already imported records
                         $this->_db->query();
                     }
                 }
             }
         }
     }
     $tables = CBCompat::getTableFields(JFactory::getDBO()->getTableList());
     foreach ($listnames as $field_id => $name) {
         $name = str_replace(array(' ', "\n", "\r", "\t"), array(''), preg_replace("/[^a-zA-Z0-9_\\s]/isU", "_", trim($name)));
         $name = preg_replace("/^([0-9\\s])/isU", "field\$1\$2", $name);
         $name = $name == '' ? 'field' . mt_rand(0, mt_getrandmax()) : $name;
         if (!trim($listtitles[$field_id])) {
             $listtitles[$field_id] = $name;
         } else {
             $listtitles[$field_id] = trim($listtitles[$field_id]);
         }
         if (!$bytable) {
             $this->_db->setQuery("Select `name` From #__contentbuilder_storage_fields Where id = " . intval($field_id));
             $old_name = $this->_db->loadResult();
             $this->_db->setQuery("Update #__contentbuilder_storage_fields Set group_definition = " . $this->_db->Quote($listgroupdefinitions[$field_id]) . ", is_group = " . intval($listisgroup[$field_id]) . ",`name` = " . $this->_db->Quote($name) . ", `title` = " . $this->_db->Quote($listtitles[$field_id]) . " Where id = " . intval($field_id));
             $this->_db->query();
             if ($old_name != $name) {
                 $this->_db->setQuery("ALTER TABLE `#__" . $data['name'] . "` CHANGE `" . $old_name . "` `" . $name . "` TEXT ");
                 $this->_db->query();
             }
         } else {
             $this->_db->setQuery("Update #__contentbuilder_storage_fields Set group_definition = " . $this->_db->Quote($listgroupdefinitions[$field_id]) . ", is_group = " . intval($listisgroup[$field_id]) . ", `title` = " . $this->_db->Quote($listtitles[$field_id]) . " Where id = " . intval($field_id));
             $this->_db->query();
         }
     }
     $this->getTable('storage_fields')->reorder('storage_id = ' . $this->_id);
     if (!$bytable) {
         // fields
         // synch non-existing fields
         $fields = $this->getFields();
         foreach ($fields as $field) {
             if (!isset($field->name)) {
                 continue;
             }
             $fieldname = $field->name;
             if ($fieldname && !isset($tables[JFactory::getDBO()->getPrefix() . $data['name']][$fieldname])) {
                 try {
                     $this->_db->setQuery("ALTER TABLE `#__" . $data['name'] . "` ADD `" . $fieldname . "` TEXT NOT NULL ");
                     $this->_db->query();
                 } catch (Exception $e) {
                 }
             }
         }
     }
     if ($post_replace === null) {
         return $this->_id;
     } else {
         return $newfieldname;
     }
 }