/**
  * Check fields in all views
  *
  * @param array $modules List of modules to check
  * @return int Count of errors found
  */
 protected function _checkViewsFields(array $modules = [])
 {
     $errors = [];
     $views = Configure::read('CsvMigrations.actions');
     $this->out('Checking views fields:', 2);
     foreach ($modules as $module => $path) {
         $moduleErrors = [];
         $viewCounter = 0;
         $this->out(' - ' . $module . ' ... ', 0);
         foreach ($views as $view) {
             $fields = null;
             try {
                 $pathFinder = new ViewPathFinder();
                 $path = $pathFinder->find($module, $view);
                 $parser = new ViewParser();
                 $fields = $parser->parseFromPath($path);
             } catch (\Exception $e) {
                 // It's OK for view files to be missing.
                 // We already handle this in _checkViewsPresence()
             }
             // If the view file does exist, it has to be parseable.
             if ($fields) {
                 $viewCounter++;
                 foreach ($fields as $field) {
                     if (count($field) > 3) {
                         $moduleErrors[] = $module . " module [{$view}] view has more than 2 columns";
                     } elseif (count($field) == 3) {
                         // Get rid of the first column, which is the panel name
                         array_shift($field);
                         $isEmbedded = false;
                         foreach ($field as $column) {
                             if ($column == 'EMBEDDED') {
                                 $isEmbedded = true;
                                 continue;
                             } else {
                                 if ($isEmbedded) {
                                     list($embeddedModule, $embeddedModuleField) = explode('.', $column);
                                     if (empty($embeddedModule)) {
                                         $moduleErrors[] = $module . " module [{$view}] view reference EMBEDDED column without a module";
                                     } else {
                                         if (!$this->_isValidModule($embeddedModule, array_keys($modules))) {
                                             $moduleErrors[] = $module . " module [{$view}] view reference EMBEDDED column with unknown module '{$embeddedModule}'";
                                         }
                                     }
                                     if (empty($embeddedModuleField)) {
                                         $moduleErrors[] = $module . " module [{$view}] view reference EMBEDDED column without a module field";
                                     } else {
                                         if (!$this->_isValidModuleField($module, $embeddedModuleField)) {
                                             $moduleErrors[] = $module . " module [{$view}] view reference EMBEDDED column with unknown field '{$embeddedModuleField}' of module '{$embeddedModule}'";
                                         }
                                     }
                                     $isEmbedded = false;
                                 } else {
                                     if ($column && !$this->_isValidModuleField($module, $column)) {
                                         $moduleErrors[] = $module . " module [{$view}] view references unknown field '{$column}'";
                                     }
                                 }
                             }
                         }
                         if ($isEmbedded) {
                             $moduleErrors[] = $module . " module [{$view}] view incorrectly uses EMBEDDED in the last column";
                         }
                     } elseif (count($field) == 1) {
                         // index view
                         if ($field[0] && !$this->_isValidModuleField($module, $field[0])) {
                             $moduleErrors[] = $module . " module [{$view}] view references unknown field '" . $field[0] . "'";
                         }
                     }
                 }
             }
         }
         // Warn if the module is missing standard views
         if ($viewCounter < count($views)) {
             $this->out('<warning>' . (int) $viewCounter . ' views</warning> ... ', 0);
         } else {
             $this->out('<info>' . (int) $viewCounter . ' views</info> ... ', 0);
         }
         $result = empty($moduleErrors) ? '<success>OK</success>' : '<error>FAIL</error>';
         $this->out($result);
         $errors = array_merge($errors, $moduleErrors);
     }
     $this->_printCheckStatus($errors);
     return count($errors);
 }
 /**
  * Method that fetches action fields from the corresponding csv file.
  *
  * @param  \Cake\Network\Request $request Request object
  * @param  string                $action  Action name
  * @return array
  */
 protected function _getActionFields(Request $request, $action = null)
 {
     $result = [];
     $controller = $request->controller;
     if (is_null($action)) {
         $action = $request->action;
     }
     try {
         $pathFinder = new ViewPathFinder();
         $path = $pathFinder->find($controller, $action);
         $parser = new ViewParser();
         $result = $parser->parseFromPath($path);
     } catch (InvalidArgumentException $e) {
         Log::error($e);
     }
     return $result;
 }
 /**
  * Method that retrieves table csv fields, by specified action.
  *
  * @param  string $tableName Table name
  * @param  string $action    Action name
  * @return array             table fields
  */
 protected function _getCsvFields($tableName, $action)
 {
     $result = [];
     if (empty($tableName) || empty($action)) {
         return $result;
     }
     try {
         $pathFinder = new ViewPathFinder();
         $path = $pathFinder->find($tableName, $action);
         $csvFields = $this->_getFieldsFromCsv($path);
     } catch (Exception $e) {
         return $result;
     }
     if (empty($csvFields)) {
         return $result;
     }
     $result = array_map(function ($v) {
         return $v[0];
     }, $csvFields);
     return $result;
 }
 /**
  * Method that passes csv defined Table fields to the View
  *
  * @return void
  */
 protected function _setTableFields()
 {
     $result = [];
     $pathFinder = new ViewPathFinder();
     $path = $pathFinder->find($this->request->controller, $this->request->action);
     $result = $this->_getFieldsFromCsv($path);
     list($plugin, $model) = pluginSplit($this->_tableInstance->registryAlias());
     /*
             add plugin and model names to each of the fields
     */
     $result = $this->_setFieldPluginAndModel($result, $model, $plugin);
     /*
             If action requires panels, arrange the fields into the panels
     */
     if (in_array($this->request->action, $this->_panelActions)) {
         $result = $this->_arrangePanels($result);
     }
     $this->_controllerInstance->set('fields', $result);
     $this->_controllerInstance->set('_serialize', ['fields']);
 }