public function getProjectParams()
 {
     $params = $this->projectConfTemplate['project'];
     $params['path'] = afStudioUtil::getRootDir();
     $params['display_url'] = '<a href="' . $params['url'] . '">' . $params['url'] . '</a>';
     return $params;
 }
 /**
  * Getting files
  *
  * @param string $plugin 
  * @param string $modulename 
  * @param string $pro_name 
  * @return Array
  */
 public static function getFiles($plugin, $modulename, $pro_name)
 {
     $root_dir = afStudioUtil::getRootDir();
     $dir = "{$root_dir}/plugins/{$plugin}/modules/{$modulename}/config/";
     $base_mod_dir = "{$root_dir}/plugins/{$plugin}/modules/{$modulename}";
     $securityPath = "{$base_mod_dir}/config/security.yml";
     $defaultActionPath = "{$base_mod_dir}/actions/actions.class.php";
     $files = array();
     if (is_dir($dir)) {
         $handler = opendir($dir);
         while (($f = readdir($handler)) !== false) {
             if (!is_dir($dir . $f) && strpos($f, $pro_name) > 0) {
                 $actionPath = $defaultActionPath;
                 $widgetName = pathinfo($f, PATHINFO_FILENAME);
                 $predictActions = "{$widgetName}Action.class.php";
                 $predictActionsPath = "{$base_mod_dir}/actions/{$predictActions}";
                 if (file_exists($predictActionsPath)) {
                     $actionPath = $predictActionsPath;
                 }
                 $actionName = pathinfo($actionPath, PATHINFO_BASENAME);
                 $files[] = array('text' => $f, 'type' => 'xml', 'widgetUri' => $modulename . '/' . str_replace('.xml', '', $f), 'securityPath' => $securityPath, 'actionPath' => $actionPath, 'actionName' => $actionName, 'widgetName' => $widgetName, 'leaf' => true);
             }
         }
     }
     return $files;
 }
 /**
  * Getting description when command empty
  *
  * @return string
  * @author Sergey Startsev
  */
 protected function getDescription()
 {
     $files = sfFinder::type('file')->name('*.*')->in(afStudioUtil::getRootDir() . '/batch/');
     foreach ($files as $file) {
         $baseFiles[] = basename($file);
     }
     $result = $this->render('../batch/<file>') . afsRenderConsoleCommand::render('<b>Usage:</b> batch "file"<br><b>Found batches:</b> ' . implode('; ', $baseFiles), array('class' => 'afStudio_result_command'));
     return $result;
 }
 /**
  * Rename action name inside module
  *
  * @param string $name 
  * @param string $renamed 
  * @param string $module
  * @param string $place 
  * @param string $type 
  * @return boolean
  * @author Sergey Startsev
  */
 public static function renameAction($name, $renamed, $module, $place, $type)
 {
     $root = afStudioUtil::getRootDir();
     $afConsole = afStudioConsole::getInstance();
     $console = '';
     $module_dir = "{$root}/{$type}s/{$place}/modules/{$module}";
     $action_dir = "{$module_dir}/actions";
     $predictActions = "{$name}Action.class.php";
     $predictActionsPath = "{$module_dir}/actions/{$predictActions}";
     $oldName = "{$name}Action.class.php";
     $newName = "{$renamed}Action.class.php";
     $oldPath = "{$action_dir}/{$oldName}";
     $newPath = "{$action_dir}/{$newName}";
     if (file_exists($oldPath)) {
         $action = file_get_contents($oldPath);
         $action = str_ireplace("{$name}Action", "{$renamed}Action", $action);
         afStudioUtil::writeFile($oldPath, $action);
         return afsFileSystem::create()->rename($oldPath, $newPath);
     }
     return true;
 }
 /**
  * Prepare command
  *
  * @return string
  * @author Sergey Startsev
  */
 protected function prepare()
 {
     $command = 'appflower:' . substr($this->getCommand(), 10);
     return sprintf('%s "%s" %s', $this->getCli(), afStudioUtil::getRootDir() . '/symfony', $command);
 }
 /**
  * Getting grouped module list 
  *
  * @param string $type 
  * @return array
  * @author Sergey Startsev
  */
 public static function getGroupedList($type)
 {
     $root = afStudioUtil::getRootDir();
     $deprecated = $type == self::TYPE_PLUGIN ? afStudioPluginCommandHelper::getDeprecatedList() : array();
     $data = array();
     foreach (afStudioUtil::getDirectories("{$root}/{$type}s/", true) as $place) {
         if (in_array($place, $deprecated)) {
             continue;
         }
         foreach (afStudioUtil::getDirectories("{$root}/{$type}s/{$place}/modules/", true) as $module) {
             $data[] = array('value' => $module, 'text' => $module, 'group' => $place, 'type' => $type);
         }
     }
     return $data;
 }
 /**
  * Getting full file path with replaces 'root' token 
  *
  * @param string $file 
  * @return string
  * @author Sergey Startsev
  */
 public static function getPath($file)
 {
     return str_replace(self::PATH_ROOT_IDENTIFICATOR, afStudioUtil::getRootDir(), $file);
 }
 /**
  * Rename module functionality
  * 
  * @return afResponse
  * @author Sergey Startsev
  */
 protected function processRename()
 {
     $type = $this->getParameter('type');
     $place = $this->getParameter('place');
     $name = $this->getParameter('name');
     $renamed = $this->getParameter('renamed');
     $permissions = new Permissions();
     $is_writable = $permissions->isWritable(sfConfig::get('sf_apps_dir') . '/' . $place . '/modules/');
     if ($is_writable !== true) {
         return $is_writable;
     }
     $response = afResponseHelper::create();
     $root = afStudioUtil::getRootDir();
     if (!afStudioModuleCommandHelper::isValidName($renamed)) {
         return afResponseHelper::create()->success(false)->message("Module name not valid, should contains only upper and lower case alphabet characters");
     }
     $oldDir = "{$root}/{$type}s/{$place}/modules/{$name}/";
     $newDir = "{$root}/{$type}s/{$place}/modules/{$renamed}/";
     if (file_exists($newDir)) {
         return $response->success(false)->message("Module <b>{$renamed}</b> already exists inside <b>{$place}</b> {$type}!");
     }
     afsFileSystem::create()->rename($oldDir, $newDir);
     // Rename in actions class
     afStudioModuleCommandHelper::renameAction($name, $renamed, $place, $type);
     if (!file_exists($oldDir) && file_exists($newDir)) {
         $console = afStudioConsole::getInstance()->execute(array('afs fix-perms', 'sf cc'));
         return $response->success(true)->message("Renamed module from <b>{$name}</b> to <b>{$renamed}</b> inside <b>{$place}</b> {$type}!")->console($console);
     }
     return $response->success(false)->message("Can't rename module from <b>{$name}</b> to <b>{$renamed}</b> inside <b>{$place}</b> {$type}!");
 }
 /**
  * Try to find action 
  *
  * @param string $action 
  * @param string $module 
  * @return mixed - array/boolean
  * @author Sergey Startsev
  */
 public static function find($action, $module)
 {
     $root_dir = afStudioUtil::getRootDir();
     foreach (array('app', 'plugin') as $type) {
         foreach (afStudioUtil::getDirectories("{$root_dir}/{$type}s/", true) as $place) {
             $modules_dir = "{$root_dir}/{$type}s/{$place}/modules";
             if (in_array($module, afStudioUtil::getDirectories($modules_dir, true))) {
                 if (in_array("{$action}.xml", afStudioUtil::getFiles("{$modules_dir}/{$module}/config/", true, "xml"))) {
                     return array('place' => $place, 'placeType' => $type);
                 }
             }
         }
     }
     return false;
 }
 /**
  * Getting schema file
  *
  * @return string
  * @author Sergey Startsev
  */
 public function getSchemaFile()
 {
     if (is_null($this->schemaFile)) {
         $this->schemaFile = afStudioUtil::getRootDir() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'schema.yml';
     }
     return $this->schemaFile;
 }
 /**
  * Extract archive functionality
  *
  * @return afResponse
  * @author Sergey Startsev
  */
 protected function processUnarchive()
 {
     $file = $this->getParameter('file');
     $file = substr($file, 0, 4) == 'root' ? str_replace('root', afStudioUtil::getRootDir(), substr($file, 0, 4)) . substr($file, 4) : $file;
     if (!file_exists($file)) {
         return afResponseHelper::create()->success(false)->message("File that you try to extract doesn't exists");
     }
     $arch = new Archive_Tar("{$file}", 'gz');
     $status = $arch->extract(dirname($file));
     return afResponseHelper::create()->success($status)->message($status ? "File has been successfully extracted" : "File can't be extracted");
 }
 /**
  * Getting pages list from applications 
  * 
  * @return array
  * @author Sergey Startsev
  */
 private function getPagesList()
 {
     $sRealRoot = afStudioUtil::getRootDir();
     $data = array();
     $apps = afStudioUtil::getDirectories("{$sRealRoot}/apps/", true);
     foreach ($apps as $app) {
         $xmlNames = afStudioUtil::getFiles($sRealRoot . "/apps/{$app}/config/pages/", true, 'xml');
         $xmlPaths = afStudioUtil::getFiles($sRealRoot . "/apps/{$app}/config/pages/", false, 'xml');
         if (count($xmlNames) > 0) {
             foreach ($xmlNames as $xk => $page) {
                 $data[$app][] = array('text' => $page, 'xmlPath' => $xmlPaths[$xk], 'widgetUri' => 'pages/' . str_replace('.xml', '', $page));
             }
         }
     }
     return $data;
 }
 /**
  * Delete plugin
  * 
  * @return afResponse
  */
 protected function processDelete()
 {
     $name = $this->getParameter('name');
     $pluginDir = afStudioUtil::getRootDir() . "/plugins/{$name}/";
     $response = afResponseHelper::create();
     $permissions = new Permissions();
     $are_writable = $permissions->areWritable(array(afStudioUtil::getRootDir() . "/plugins/", $pluginDir));
     if ($are_writable !== true) {
         return $are_writable;
     }
     afsFileSystem::create()->remove($pluginDir);
     if (!file_exists($pluginDir)) {
         $console_result = afStudioConsole::getInstance()->execute(array('afs fix-perms', 'sf cc'));
         return $response->success(true)->message("Deleted plugin <b>{$name}</b>")->console($console_result);
     }
     return $response->success(false)->message("Can't delete plugin <b>{$name}</b>!");
 }
 /**
  * Getting pages application path
  *
  * @return void
  * @author Sergey Startsev
  */
 private function getPagesApplicationPath()
 {
     return afStudioUtil::getRootDir() . DIRECTORY_SEPARATOR . "apps" . DIRECTORY_SEPARATOR . $this->getApplication();
 }
 /**
  * Export project functionality
  *
  * @return afResponse
  * @author Sergey Startsev
  */
 protected function processExport()
 {
     // ini set for windows
     ini_set("max_execution_time", "300");
     $response = afResponseHelper::create();
     $by_os = $this->getParameter('by_os', 'true');
     $type = $this->getParameter('type', 'project');
     $path = $this->getParameter('path', sys_get_temp_dir());
     if (substr($path, -1, 1) != DIRECTORY_SEPARATOR) {
         $path .= DIRECTORY_SEPARATOR;
     }
     $name = $this->getParameter('name', pathinfo(afStudioUtil::getRootDir(), PATHINFO_BASENAME));
     $console_result = afStudioConsole::getInstance()->execute("sf afs:export --type={$type} --by_os={$by_os} --path={$path} --project_name={$name}");
     $postfix = $type == 'db' ? 'sql' : 'tar.gz';
     if (!file_exists("{$path}{$name}.{$postfix}")) {
         return $response->success(false)->message('Please check permissions, and propel settings');
     }
     return $response->success(true)->data(array(), array('name' => $name, 'file' => "{$name}.{$postfix}", 'path' => $path), 0)->console($console_result);
 }
 /**
  * Generate place path 
  *
  * @return string
  * @author Sergey Startsev
  */
 protected function getPlacePath()
 {
     return afStudioUtil::getRootDir() . DIRECTORY_SEPARATOR . "{$this->place_type}s" . DIRECTORY_SEPARATOR . $this->place;
 }