Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jenskooij committed Apr 24, 2018
2 parents a747ff4 + df52ac0 commit fe7535e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/storage/entities/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use CloudControl\Cms\components\CmsComponent;
use CloudControl\Cms\storage\storage\DocumentStorage;
use CloudControl\Cms\util\DocumentSorter;

/**
* Class Document
Expand All @@ -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');
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
56 changes: 56 additions & 0 deletions src/util/DocumentSorter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Created by jensk on 23-4-2018.
*/

namespace CloudControl\Cms\util;


use CloudControl\Cms\storage\entities\Document;

class DocumentSorter
{
protected static $orderByField;
protected static $order = 'ASC';

/**
* Sorts an array of Document instances
* @param array $documents
* @param string $field
* @param string $order
* @return array
*/
public static function sortDocumentsByField($documents, $field, $order = 'ASC')
{
self::$orderByField = $field;
self::$order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';
usort($documents, '\CloudControl\Cms\util\DocumentSorter::fieldCompare');
if ($order === 'DESC') {
return array_reverse($documents);
}
return $documents;
}

/**
* Compares two documents
* @param Document $a
* @param Document $b
* @return int
*/
protected static function fieldCompare(Document $a, Document $b) {
$field = self::$orderByField;
if (property_exists('\CloudControl\Cms\storage\entities\Document', $field)) {
return strcasecmp($a->{$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]);
}
}

0 comments on commit fe7535e

Please sign in to comment.