diff --git a/src/storage/entities/Document.php b/src/storage/entities/Document.php index 27e8c46..e40891a 100644 --- a/src/storage/entities/Document.php +++ b/src/storage/entities/Document.php @@ -9,6 +9,7 @@ use CloudControl\Cms\components\CmsComponent; use CloudControl\Cms\storage\storage\DocumentStorage; +use CloudControl\Cms\util\DocumentSorter; /** * Class Document @@ -35,12 +36,17 @@ class Document public $lastModificationDate; public $creationDate; public $lastModifiedBy; + public $publicationDate; + public $unpublishedChanges; protected $documentStorage; protected $fields; protected $bricks; protected $dynamicBricks; protected $content; + protected $order = 'ASC'; + protected $orderByField; + protected $dbHandle; private static $JSON_ENCODED_FIELDS = array('fields', 'bricks', 'dynamicBricks'); @@ -103,12 +109,23 @@ public function getContent() $docs = $this->documentStorage->getDocumentsBySlug($slug); } + if ($this->orderByField !== null) { + $docs = $this->orderContentByField($docs); + } + $this->content = $docs; } return $this->content; } + public function orderByField($fieldName, $order = 'ASC') + { + $this->orderByField = $fieldName; + $this->order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC'; + return $this; + } + /** * @return string */ @@ -152,5 +169,12 @@ private function getJsonEncodedField($name) return $this->getPropertyIfExists($name); } - + /** + * @param array $docs + * @return array + */ + protected function orderContentByField($docs) + { + return DocumentSorter::sortDocumentsByField($docs, $this->orderByField, $this->order); + } } \ No newline at end of file diff --git a/src/util/DocumentSorter.php b/src/util/DocumentSorter.php new file mode 100644 index 0000000..2d47c86 --- /dev/null +++ b/src/util/DocumentSorter.php @@ -0,0 +1,56 @@ +{$field}, $b->{$field}); + } + + if (!isset($a->fields->{$field}[0])) { + return -3; + } + + if (!isset($b->fields->{$field}[0])) { + return 3; + } + + return strcasecmp($a->fields->{$field}[0], $b->fields->{$field}[0]); + } +} \ No newline at end of file