/** * 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) { $translations = (array) $row['_i18n']; $result = []; foreach ($translations as $translation) { unset($translation['id']); $result[$translation['locale']] = $translation; } $row['_translations'] = $result; unset($row['_i18n']); if (is_object($row)) { $row->clean(); } return $row; }); }
/** * 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; }); }
/** * 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; }); }
/** * Convert db resultset to messages array. * * @param \Cake\Datasource\ResultSetInterface $results ResultSet instance. * * @return array */ protected function _messages(ResultSetInterface $results) { if (!$results->count()) { return []; } $messages = []; $pluralForms = 0; $item = $results->first(); // There are max 6 plural forms possible but most people won't need // that so will only have the required number of value_{n} fields in db. for ($i = 5; $i > 0; $i--) { if (isset($item['value_' . $i])) { $pluralForms = $i; break; } } foreach ($results as $item) { $singular = $item['singular']; $context = $item['context']; $translation = $item['value_0']; if ($context) { $messages[$singular]['_context'][$context] = $item['value_0']; } else { $messages[$singular] = $item['value_0']; } if (empty($item['plural'])) { continue; } $key = $item['plural']; $plurals = []; for ($i = 0; $i <= $pluralForms; $i++) { $plurals[] = $item['value_' . $i]; } if ($context) { $messages[$key]['_context'][$context] = $plurals; } else { $messages[$key] = $plurals; } } return $messages; }
/** * 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; }); }