function index()
 {
     $response = array();
     if (Sanitize::getString($this->params, 'task') == 'upgrade') {
         // Where running the install script for upgrade we want a json object returned
         $this->autoLayout = false;
         $this->autoRender = false;
     } else {
         $this->autoLayout = true;
         $this->autoRender = true;
     }
     $this->name = 'install';
     # Remove views folder in J1.5
     if ($this->cmsVersion == CMS_JOOMLA15) {
         $Folder = ClassRegistry::getClass('Folder');
         $target = PATH_ROOT . 'components' . DS . 'com_jreviews' . DS . 'views';
         $Folder->rm($target);
     }
     # Create database tables
     // Start db upgrade logic
     $action = array();
     $action['db_install'] = true;
     $tables = $this->_db->getTableList();
     $dbprefix = cmsFramework::getConfig('dbprefix');
     $old_build = 0;
     // Get current version number
     $jreviewsxml = $this->cmsVersion == CMS_JOOMLA15 ? 'jreviews.xml' : 'jreviewg.xml';
     $xml = file(S2_CMS_ADMIN . $jreviewsxml);
     foreach ($xml as $xml_line) {
         if (strstr($xml_line, 'version')) {
             $new_version = trim(strip_tags($xml_line));
             continue;
         }
     }
     $version_parts = explode('.', $new_version);
     $new_build = array_pop($version_parts);
     if (is_array($tables) && in_array($dbprefix . 'jreviews_categories', array_values($tables))) {
         // Tables exist so we check the current build and upgrade accordingly, otherwise it's a clean install and no upgrade is necessary
         $query = "SELECT value FROM #__jreviews_config WHERE id = 'version'";
         $this->_db->setQuery($query);
         $old_version = trim(strip_tags($this->_db->loadResult()));
         if ($old_version != '') {
             $version_parts = explode('.', $old_version);
             $old_build = array_pop($version_parts);
         }
         if (Sanitize::getBool($this->params, 'sql')) {
             $old_build = 0;
         }
         //            prx($old_build . '<br/>' . $new_build) ;
         if ($new_build > $old_build) {
             $i = $old_build + 1;
             for ($i = $old_build + 1; $i <= $new_build; $i++) {
                 // Run sql updates
                 $sql_file = S2Paths::get('jreviews', 'S2_APP') . 'upgrades' . DS . 'upgrade_build' . $i . '.sql';
                 if (file_exists($sql_file)) {
                     $action['db_install'] = $this->__parseMysqlDump($sql_file, $dbprefix) && $action['db_install'];
                 }
                 // Run php updates
                 $php_file = S2Paths::get('jreviews', 'S2_APP') . 'upgrades' . DS . 'upgrade_build' . $i . '.php';
                 if (file_exists($php_file)) {
                     include $php_file;
                 }
             }
         }
     } else {
         // It's a clean install so we use the whole jReviews sql file
         $sql_file = S2Paths::get('jreviews', 'S2_APP') . 'upgrades' . DS . 'jreviews.sql';
         $action['db_install'] = $this->__parseMysqlDump($sql_file, $dbprefix);
     }
     # Update component id in pre-existing jReviews menus
     if ($this->cmsVersion == CMS_JOOMLA16) {
         $query = "\n                SELECT \n                    extension_id AS id\n                FROM \n                    #__extensions \n                WHERE \n                    element = '" . S2Paths::get('jreviews', 'S2_CMSCOMP') . "' AND type = 'component'\n            ";
     } else {
         $query = "\n                SELECT \n                    id \n                FROM \n                    #__components \n                WHERE \n                    admin_menu_link = 'option=" . S2Paths::get('jreviews', 'S2_CMSCOMP') . "'\n            ";
     }
     $this->_db->setQuery($query);
     if ($id = $this->_db->loadResult()) {
         if ($this->cmsVersion == CMS_JOOMLA16) {
             $query = "\n                    UPDATE \n                        `#__menu` \n                    SET \n                        component_id = {$id} \n                    WHERE \n                        type IN ('component','components') \n                            AND \n                        link LIKE 'index.php?option=" . S2Paths::get('jreviews', 'S2_CMSCOMP') . "%'\n                ";
         } else {
             $query = "\n                    UPDATE \n                        `#__menu` \n                    SET \n                        componentid = {$id} \n                    WHERE \n                        type IN ('component','components') \n                            AND \n                        link = 'index.php?option=" . S2Paths::get('jreviews', 'S2_CMSCOMP') . "'\n                ";
         }
         $this->_db->setQuery($query);
         $this->_db->query();
     }
     # Update version number in the database
     $this->Config->version = $new_version;
     $this->Config->store();
     $action['plugin_install'] = $this->_installPlugin();
     # Create image upload and thumbnail folders
     if (!is_dir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS)) {
         $Config = new JConfig();
         if (isset($Config->ftp_enable) && $Config->ftp_enable) {
             // set up basic connection
             $conn_id = ftp_connect($Config->ftp_host, $Config->ftp_port);
             // login with username and password
             $login_result = ftp_login($conn_id, $Config->ftp_user, $Config->ftp_pass);
             ftp_chdir($conn_id, $Config->ftp_root);
             ftp_mkdir($conn_id, _JR_PATH_IMAGES . 'jreviews');
             ftp_mkdir($conn_id, _JR_PATH_IMAGES . 'jreviews' . DS . 'tn');
             ftp_close($conn_id);
             @copy(PATH_ROOT . _JR_PATH_IMAGES . 'index.html', PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'index.html');
             @copy(PATH_ROOT . _JR_PATH_IMAGES . 'index.html', PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn' . DS . 'index.html');
         }
     }
     if (!is_dir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS)) {
         $result = mkdir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS, 0755);
         if (!$result) {
             $action['thumbnail_dir'] = false;
         } else {
             @copy(PATH_ROOT . _JR_PATH_IMAGES . 'index.html', PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'index.html');
             $result = mkdir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn', 0755);
             if (!$result) {
                 $action['thumbnail_dir'] = false;
             } else {
                 @copy(PATH_ROOT . _JR_PATH_IMAGES . 'index.html', PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn' . DS . 'index.html');
             }
         }
     }
     if (!is_dir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn' . DS)) {
         $result = mkdir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn', 0755);
         if (!$result) {
             $action['thumbnail_dir'] = false;
         } else {
             @copy(PATH_ROOT . _JR_PATH_IMAGES . 'index.html', PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn' . DS . 'index.html');
         }
     }
     if (is_dir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS) && is_dir(PATH_ROOT . _JR_PATH_IMAGES . 'jreviews' . DS . 'tn' . DS)) {
         $action['thumbnail_dir'] = true;
     }
     # Ensure that all field group names are slugs
     $query = "\n            SELECT \n                groupid, name\n            FROM\n                #__jreviews_groups\n        ";
     $this->_db->setQuery($query);
     $groups = $this->_db->loadAssocList();
     if (!empty($groups)) {
         foreach ($groups as $group) {
             if (strpos($group['name'], ' ') !== false) {
                 $name = cmsFramework::StringTransliterate($group['name']) . $group['groupid'];
                 $query = "\n                        UPDATE\n                            #__jreviews_groups\n                        SET \n                            name = " . $this->quote($name) . "\n                        WHERE\n                            groupid = " . $group['groupid'];
                 $this->_db->setQuery($query);
                 $this->_db->query();
             }
         }
     }
     # Clear data and core caches
     clearCache('', '__data');
     clearCache('', 'core');
     //var_dump($action);
     if (Sanitize::getString($this->params, 'task') == 'upgrade') {
         $response = array('error' => false, 'html' => '');
         // {"db_install":true,"plugin_install":true,"thumbnail_dir":true}
         if (!$action['db_install']) {
             $response['error'] = true;
             $response['html'] = '<div style="color:red>There was a problem upgrading the database</div>';
         }
         if (!$action['plugin_install']) {
             $response['error'] = true;
             $response['html'] .= '<div style="color:red>There was a problem upgrading the JReviews plugin</div>';
         }
         return json_encode($response);
     }
     $this->set(array('action' => $action));
 }