Example #1
0
 /**
  * Modifies the results from a table find in order to merge full translation records
  * into each entity under the `_translations` key
  *
  * @param \Cake\Datasource\ResultSetInterface $results Results to modify.
  * @return \Cake\Collection\Collection
  */
 public function groupTranslations($results)
 {
     return $results->map(function ($row) {
         if (!$row instanceof EntityInterface) {
             return $row;
         }
         $translations = (array) $row->get('_i18n');
         $grouped = new Collection($translations);
         $result = [];
         foreach ($grouped->combine('field', 'content', 'locale') as $locale => $keys) {
             $entityClass = $this->_table->entityClass();
             $translation = new $entityClass($keys + ['locale' => $locale], ['markNew' => false, 'useSetters' => false, 'markClean' => true]);
             $result[$locale] = $translation;
         }
         $options = ['setter' => false, 'guard' => false];
         $row->set('_translations', $result, $options);
         unset($row['_i18n']);
         $row->clean();
         return $row;
     });
 }
 /**
  * Tests that it is possible to both override fields with a translation and
  * also find separately other translations
  *
  * @return void
  */
 public function testTranslationsHasManyWithOverride()
 {
     $table = TableRegistry::get('Articles');
     $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
     $table->hasMany('Comments');
     $comments = $table->hasMany('Comments')->target();
     $comments->addBehavior('Translate', ['fields' => ['comment']]);
     $table->locale('cze');
     $comments->locale('eng');
     $results = $table->find('translations')->contain(['Comments' => function ($q) {
         return $q->find('translations')->select(['id', 'comment', 'article_id']);
     }]);
     $comments = $results->first()->comments;
     $expected = [1 => 'Comment #1', 2 => 'Comment #2', 3 => 'Comment #3', 4 => 'Comment #4'];
     $list = new Collection($comments);
     $this->assertEquals($expected, $list->combine('id', 'comment')->toArray());
     $expected = [['eng' => ['comment' => 'Comment #1', 'locale' => 'eng']], ['eng' => ['comment' => 'Comment #2', 'locale' => 'eng']], ['eng' => ['comment' => 'Comment #3', 'locale' => 'eng']], ['eng' => ['comment' => 'Comment #4', 'locale' => 'eng'], 'spa' => ['comment' => 'Comentario #4', 'locale' => 'spa']]];
     $translations = $this->_extractTranslations($comments);
     $this->assertEquals($expected, $translations->toArray());
     $this->assertEquals('Titulek #1', $results->first()->title);
     $this->assertEquals('Obsah #1', $results->first()->body);
 }
Example #3
0
 public function getDataNorth()
 {
     // Init table
     $resultsTable = TableRegistry::get('Results');
     $query = $resultsTable->find('all', ['fields' => ['date_result'], 'conditions' => ['area' => Configure::read('Area.north.code')], 'order' => ['date_result' => 'DESC']]);
     $newestDate = $query->first()->date_result->modify('+1 days')->i18nFormat('YYYY-MM-dd');
     $endDate = date('H', strtotime('+7 hour')) > 18 ? 0 : 1;
     //Init variable
     $http = new Client();
     $begin = new \DateTime($newestDate);
     $end = new \DateTime();
     $end->modify("-{$endDate} day");
     $interval = new \DateInterval('P1D');
     $daterange = new \DatePeriod($begin, $interval, $end);
     // Get list city
     $collection = new Collection(Configure::read('City'));
     $arrCity = $collection->combine('w', 'code')->toArray();
     foreach ($daterange as $date) {
         $dateFormat = $date->format("d-m-Y");
         $wday = $date->format("w");
         $dateResult = $date->format("Ymd");
         $url = "http://www.xoso.net/getkqxs/mien-bac/{$dateFormat}.js";
         $response = $http->get($url);
         preg_match_all("|'(.*)'|", $response->body(), $match);
         $dom = new \DOMDocument();
         $dom->loadHTML($match[1][2]);
         foreach ($dom->getElementsByTagName('td') as $node) {
             $class = $node->getAttribute('class');
             $value = preg_replace('/\\s+/', '', $node->nodeValue);
             if (preg_match('/^giai(\\d|db)+$/', $class) && $value !== '') {
                 $arrContent = explode('-', $value);
                 foreach ($arrContent as $content) {
                     $result = $resultsTable->newEntity();
                     $result->date_result = $dateResult;
                     $result->level = Configure::read("Result_Level.{$class}");
                     $result->content = $content;
                     $result->area = 1;
                     $result->city = $arrCity[$wday];
                     $result->created_date = $end->format("YmdHis");
                     $result->modified_date = $end->format("YmdHis");
                     $resultsTable->save($result);
                 }
             }
         }
     }
 }
 /**
  * Modifies the results from a table find in order to merge full version records
  * into each entity under the `_versions` key
  *
  * @param \Cake\Datasource\ResultSetInterface $results Results to modify.
  * @return \Cake\Collection\Collection
  */
 public function groupVersions($results)
 {
     $property = $this->versionAssociation()->property();
     return $results->map(function ($row) use($property) {
         $versionField = $this->_config['versionField'];
         $versions = (array) $row->get($property);
         $grouped = new Collection($versions);
         $result = [];
         foreach ($grouped->combine('field', 'content', 'version_id') as $versionId => $keys) {
             $entityClass = $this->_table->entityClass();
             $version = new $entityClass($keys + [$versionField => $versionId], ['markNew' => false, 'useSetters' => false, 'markClean' => true]);
             $result[$versionId] = $version;
         }
         $options = ['setter' => false, 'guard' => false];
         $row->set('_versions', $result, $options);
         unset($row[$property]);
         $row->clean();
         return $row;
     });
 }
 /**
  * Modifies the results from a table find in order to merge full version records
  * into each entity under the `_versions` key
  *
  * @param \Cake\Datasource\ResultSetInterface $results Results to modify.
  * @return \Cake\Collection\Collection
  */
 public function groupVersions($results)
 {
     return $results->map(function ($row) {
         $versions = (array) $row->get('__version');
         $grouped = new Collection($versions);
         $result = [];
         foreach ($grouped->combine('field', 'content', 'version_id') as $versionId => $keys) {
             $version = $this->_table->newEntity($keys + ['version_id' => $versionId], ['markNew' => false, 'useSetters' => false, 'markClean' => true]);
             $result[$versionId] = $version;
         }
         $options = ['setter' => false, 'guard' => false];
         $row->set('_versions', $result, $options);
         unset($row['__version']);
         $row->clean();
         return $row;
     });
 }