/**
  * Initializes the instance.
  *
  * @param CRUDDataFactoryInterface $dataFactory
  * the factory to create the concrete CRUDData instances
  * @param string $crudFile
  * the CRUD YAML file to parse
  * @param CRUDFileProcessorInterface $fileProcessor
  * the file processor used for file fields
  * @param boolean $manageI18n
  * holds whether we manage the i18n
  * @param Application $app
  * the application container
  */
 public function init(CRUDDataFactoryInterface $dataFactory, $crudFile, CRUDFileProcessorInterface $fileProcessor, $manageI18n, Application $app)
 {
     $this->initMissingServiceProviders($app);
     $this->manageI18n = $manageI18n;
     $locales = $this->initLocales($app);
     $parsedYaml = $this->readYaml($crudFile);
     $this->datas = array();
     foreach (empty($parsedYaml) ? array() : $parsedYaml as $name => $crud) {
         if (!is_array($crud) || !isset($crud['fields'])) {
             continue;
         }
         $label = array_key_exists('label', $crud) ? $crud['label'] : $name;
         $localeLabels = $this->getLocaleLabels($locales, $crud);
         $standardFieldLabels = array('id' => $app['translator']->trans('crudlex.label.id'), 'created_at' => $app['translator']->trans('crudlex.label.created_at'), 'updated_at' => $app['translator']->trans('crudlex.label.updated_at'));
         $definition = new CRUDEntityDefinition($crud['table'], $crud['fields'], $label, $localeLabels, $standardFieldLabels, $this);
         $this->configureDefinition($definition, $crud);
         $this->datas[$name] = $dataFactory->createData($definition, $fileProcessor);
     }
     $this->initChildren();
 }
Esempio n. 2
0
 /**
  * Initializes the instance.
  *
  * @param CRUDDataFactoryInterface $dataFactory
  * the factory to create the concrete CRUDData instances
  * @param string $crudFile
  * the CRUD YAML file to parse
  * @param CRUDFileProcessorInterface $fileProcessor
  * the file processor used for file fields
  * @param boolean $manageI18n
  * holds whether we manage the i18n
  * @param Application $app
  * the application container
  */
 public function init(CRUDDataFactoryInterface $dataFactory, $crudFile, CRUDFileProcessorInterface $fileProcessor, $manageI18n, Application $app)
 {
     $this->manageI18n = $manageI18n;
     if (!$app->offsetExists('translator')) {
         $app->register(new \Silex\Provider\TranslationServiceProvider(), array('locale_fallbacks' => array('en')));
     }
     if (!$app->offsetExists('session')) {
         $app->register(new \Silex\Provider\SessionServiceProvider());
     }
     if (!$app->offsetExists('url_generator')) {
         $app->register(new \Silex\Provider\UrlGeneratorServiceProvider());
     }
     if (!$app->offsetExists('twig')) {
         $app->register(new \Silex\Provider\TwigServiceProvider());
         $app['twig.loader.filesystem']->addPath(__DIR__ . '/../views/', 'crud');
     }
     $app['translator']->addLoader('yaml', new YamlFileLoader());
     $localeDir = __DIR__ . '/../locales';
     $langFiles = scandir($localeDir);
     $locales = array();
     foreach ($langFiles as $langFile) {
         if ($langFile == '.' || $langFile == '..') {
             continue;
         }
         $locale = substr($langFile, 0, strpos($langFile, '.yml'));
         $locales[] = $locale;
         $app['translator']->addResource('yaml', $localeDir . '/' . $langFile, $locale);
     }
     $parsedYaml = $this->readYaml($crudFile);
     $this->datas = array();
     foreach (empty($parsedYaml) ? [] : $parsedYaml as $name => $crud) {
         if (!is_array($crud) || !isset($crud['fields'])) {
             continue;
         }
         $label = array_key_exists('label', $crud) ? $crud['label'] : $name;
         $localeLabels = array();
         foreach ($locales as $locale) {
             if (array_key_exists('label_' . $locale, $crud)) {
                 $localeLabels[$locale] = $crud['label_' . $locale];
             }
         }
         $standardFieldLabels = array('id' => $app['translator']->trans('crudlex.label.id'), 'created_at' => $app['translator']->trans('crudlex.label.created_at'), 'updated_at' => $app['translator']->trans('crudlex.label.updated_at'));
         $definition = new CRUDEntityDefinition($crud['table'], $crud['fields'], $label, $localeLabels, $standardFieldLabels, $this);
         $this->datas[$name] = $dataFactory->createData($definition, $fileProcessor);
         if (array_key_exists('deleteCascade', $crud)) {
             $this->datas[$name]->getDefinition()->setDeleteCascade($crud['deleteCascade']);
         }
         if (array_key_exists('listFields', $crud)) {
             $this->datas[$name]->getDefinition()->setListFieldNames($crud['listFields']);
         }
         if (array_key_exists('filter', $crud)) {
             $this->datas[$name]->getDefinition()->setFilter($crud['filter']);
         }
         if (array_key_exists('childrenLabelFields', $crud)) {
             $this->datas[$name]->getDefinition()->setChildrenLabelFields($crud['childrenLabelFields']);
         }
         if (array_key_exists('pageSize', $crud)) {
             $this->datas[$name]->getDefinition()->setPageSize($crud['pageSize']);
         }
     }
     foreach ($this->datas as $name => $data) {
         $fields = $data->getDefinition()->getFieldNames();
         foreach ($fields as $field) {
             if ($data->getDefinition()->getType($field) == 'reference') {
                 $this->datas[$data->getDefinition()->getReferenceEntity($field)]->getDefinition()->addChild($data->getDefinition()->getTable(), $field, $name);
             }
         }
     }
 }