Merge branch '2013.10' into 2014.11
[tine20] / tine20 / Setup / Frontend / Cli.php
index 353e55e..4895d0b 100644 (file)
@@ -57,7 +57,7 @@ class Setup_Frontend_Cli
         } elseif(isset($_opts->uninstall)) {
             $this->_uninstall($_opts);
         } elseif(isset($_opts->list)) {
-            $this->_listInstalled();
+            $result = $this->_listInstalled();
         } elseif(isset($_opts->sync_accounts_from_ldap)) {
             $this->_importAccounts($_opts);
         } elseif(isset($_opts->sync_passwords_from_ldap)) {
@@ -72,6 +72,10 @@ class Setup_Frontend_Cli
             $this->_createAdminUser($_opts);
         } elseif(isset($_opts->getconfig)) {
             $this->_getConfig($_opts);
+        } elseif(isset($_opts->reset_demodata)) {
+            $this->_resetDemodata($_opts);
+        } elseif(isset($_opts->updateAllImportExportDefinitions)) {
+            $this->_updateAllImportExportDefinitions($_opts);
         }
         
         if ($exitAfterHandle) {
@@ -155,8 +159,8 @@ class Setup_Frontend_Cli
                 }
             }
             
-            // initial password
-            if (! isset($_options['adminPassword'])) {
+            // initial password / can be empty => will trigger password change dialogue
+            if (! array_key_exists('adminPassword', $_options)) {
                 $_options['adminPassword'] = $this->_promptPassword();
             }
         }
@@ -264,11 +268,122 @@ class Setup_Frontend_Cli
         }
         
         $controller->uninstallApplications($applications->name);
-
+        
         echo "Successfully uninstalled " . count($applications) . " applications.\n";
     }
+    
+    /**
+     * reinstall applications
+     * and reset Demodata
+     * php setup.php --reset_demodata USERNAME
+     * 
+     * @param Zend_Console_Getopt $_opts
+     */
+    protected function _resetDemodata(Zend_Console_Getopt $_opts)
+    {
+        $controller = Setup_Controller::getInstance();
+        $userController = Admin_Controller_User::getInstance();
+        $containerController = Tinebase_Container::getInstance();
+        $cli = new Tinebase_Frontend_Cli();
+        
+        //Don't reset this applications
+        $fixedApplications = array('Tinebase', 'Admin', 'Addressbook');
+        
+        //Log in
+        $opts = $_opts->getRemainingArgs();
+        $username = $opts[0];
+        if (empty($username)) {
+            echo "Username is missing!\n";
+            exit;
+        }
+        $user = Tinebase_User::getInstance()->getUserByLoginName($username);
+        Tinebase_Core::set(Tinebase_Core::USER, $user);
+        
+        //get all applications and remove some
+        $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
+        
+        foreach ($applications as $key => &$application) {
+            if (in_array($application, $fixedApplications)) {
+                unset($applications[$key]);
+            }
+        }
+        
+        //get set rights
+        $users = Tinebase_Acl_Roles::getInstance()->getRoleByName('user role');
+        $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($users->getId());
+        
+        //Uninstall Applications
+        try {
+            $controller->uninstallApplications($applications->name);
+            echo "Successfully uninstalled " . count($applications) . " applications.\n";
+        } catch (Tinebase_Exception_NotFound $e) {
+        }
+        //Install Applications
+        try {
+            $controller->installApplications($applications->name);
+            echo "Successfully installed " . count($applications) . " applications.\n";
+        } catch (Tinebase_Exception_NotFound $e) {
+        }
+        
+        //set rights
+        foreach ($applications as &$application) {
+            $newApplicationId = Tinebase_Application::getInstance()->getApplicationByName($application->name)->getId();
+            
+            foreach ($rights as &$right) {
+                if ($right['application_id'] == $application->id) {
+                    $right['application_id'] = $newApplicationId;
+                }
+            }
+            
+        }
+        
+        Tinebase_Acl_Roles::getInstance()->setRoleRights($users->getId(), $rights);
+        echo "Successfully restored user rights.\n";
+        
+        //Clean up addressbooks
+        $internalContacts = $userController->getDefaultInternalAddressbook();
+        $containers = $containerController->getAll();
+        foreach ($containers as $key => &$container) {
+            if ($container->id == $internalContacts) {
+                // Do nothing
+            } else {
+                try {
+                    $containerController->deleteContainer($container, true);
+                } catch (Tinebase_Exception_NotFound $e) {
+                }
+            }
+        }
+        echo "Successfully cleand up containers.\n";
+        
+        //remove state
+        $db = Tinebase_Core::getDb();
+        $statement = "TRUNCATE TABLE " . $db->quoteIdentifier(SQL_TABLE_PREFIX . 'state');
+        $db->query($statement);
+        echo "Successfully truncated state table.\n";
+        
+        //Get Demodata
+        $cli->createAllDemoData();
+        
+        //clear Cache
+        Tinebase_Core::getCache()->clean(Zend_Cache::CLEANING_MODE_ALL);
+        echo "Successfully cleared Cache.\n";
+        
+        echo "Every thing done!\n";
+    }
 
     /**
+     * Update Import Export Definitions for all applications
+     */
+    protected function _updateAllImportExportDefinitions(Zend_Console_Getopt $_opts){
+        //get all applications
+        $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
+        foreach ($applications as $application) {
+            Setup_Controller::getInstance()->createImportExportDefinitions($application);
+            echo "Update definitions for " . $application->name . "...\n";
+        }
+    }
+    
+    /**
      * list installed apps
      */
     protected function _listInstalled()
@@ -277,13 +392,15 @@ class Setup_Frontend_Cli
             $applications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
         } catch (Zend_Db_Statement_Exception $e) {
             echo "No applications installed\n";
-            return;
+            return 1;
         }
         
         echo "Currently installed applications:\n";
         foreach($applications as $application) {
             echo "* $application\n";
         }
+        
+        return 0;
     }
     
     /**
@@ -398,7 +515,7 @@ class Setup_Frontend_Cli
             $errors[] = 'Missing argument: configvalue';
         }
         $configKey = (string)$options['configkey'];
-        $configValue = (is_json($options['configvalue'])) ? Zend_Json::decode($options['configvalue']) : self::parseConfigValue($options['configvalue']);
+        $configValue = self::parseConfigValue($options['configvalue']);
         $applicationName = (isset($options['app'])) ? $options['app'] : 'Tinebase';
         
         if (empty($errors)) {
@@ -576,6 +693,11 @@ class Setup_Frontend_Cli
     {
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_value, TRUE));
         
+        // check value is json encoded
+        if (Tinebase_Helper::is_json($_value)) {
+            return Zend_Json::decode($_value); 
+        }
+        
         $result = array(
             'active' => 1
         );