function setup()
 {
     if (!empty($this->data)) {
         $data = $this->data;
         $install_files_path = CONFIGS . 'install' . DS;
         $connection = array();
         foreach (array('driver', 'host', 'login', 'password', 'database', 'prefix') as $k) {
             $connection[$k] = $data[$k];
         }
         $this->_writeDBConfig($connection);
         uses('model' . DS . 'connection_manager');
         $db = ConnectionManager::getInstance();
         $connection = $db->getDataSource('default');
         if ($connection->isConnected()) {
             App::import('vendor', 'migrations');
             $oMigrations = new Migrations();
             $oMigrations->load($install_files_path . 'schema.yml');
             $dbRes = $oMigrations->up();
             if (is_array($dbRes)) {
                 $error_string = '';
                 foreach ($dbRes as $error) {
                     $error_string .= $error['error'] . '<br />';
                 }
                 $this->Session->setFlash(__('There were some errors during the creation of your db tables', true) . ':<br />' . $error_string);
             } elseif ($dbRes == true) {
                 //add admin to the users table
                 App::import('model', array('User', 'Site'));
                 $User = new User();
                 $User->save(array('username' => $data['admin_username'], 'password' => sha1(Configure::read('Security.salt') . $data['admin_password']), 'group_id' => 1));
                 /*$Site = new Site();
                   $Site->save( array( 'user_id' => $User->getInsertID(), 'domain' => Configure::read( 'CMS.Site.Domain' ) ) );*/
                 App::import('vendor', 'fixtures');
                 $oFixtures = new Fixtures();
                 if ($oFixtures->import($install_files_path . 'fixtures.yml') === true) {
                     $this->flash('Congratulations, you have successfully installed Pagebakery!', '/');
                 } else {
                     $this->Session->setFlash(__('Sorry, there was an error adding initial data', true));
                 }
             }
         } else {
             $this->Session->setFlash('I could not connect to the DataBase. Please check the setup details again.');
         }
     }
     $this->set('DBDrivers', $this->_getDBDrivers());
 }
 public function rebuild_db($projectId = null)
 {
     if (!$projectId || !($project = $this->Project->find('first', array('conditions' => array('Project.id' => $projectId), 'contain' => array())))) {
         $this->Session->setFlash(__('Please do following links in the page'));
     } else {
         require_once VENDORS . 'migrations/migrations.php';
         $db = new ConnectionManager();
         $db->create('olc_baker-dev', array('datasource' => 'Database/Mysql', 'host' => $project['Project']['db_host'], 'login' => $project['Project']['db_login'], 'password' => $project['Project']['db_password'], 'database' => $project['Project']['db_name'], 'encoding' => 'utf8', 'persistent' => false));
         $dbn = $db->getDataSource('olc_baker-dev');
         $sqlPath = $project['Project']['app_path'] . DS . 'Config' . DS . 'schema';
         $dbn->execute('CREATE DATABASE IF NOT EXISTS `' . $project['Project']['db_name'] . '`
         DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
         $dbn->execute('USE `' . $project['Project']['db_name'] . '`;');
         $migrations = new Migrations('olc_baker-dev');
         $migrations->load($sqlPath . DS . 'schema.yaml');
         $migrations->down();
         $migrations->up();
         $this->Session->setFlash(__('Database was rebuilt'));
     }
     $this->redirect(array('action' => 'index'));
 }