Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #112 from apility/feature/fetch-unpublished
Browse files Browse the repository at this point in the history
Add support for $_fetch_unpublished modifier
  • Loading branch information
olavoyen committed Dec 2, 2019
2 parents f22a79c + fb10c44 commit 5feda37
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/cli.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

global $_mode;

$_GET['_path'] = '';
$_mode = 'cli';

NF::setRoot(realpath(__DIR__ . '/../../../../') . '/');
NF::init();
Expand Down
62 changes: 52 additions & 10 deletions src/model/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ abstract class Structure implements ArrayAccess, Serializable, JsonSerializable

use FieldMapping;

/**
* Allows fetching of unpublished entries
*
* @var bool
*/
protected static $_fetch_unpublished = true;

protected static $_booted = false;
protected static $_hooks = [];
protected static $_existing_hooks = [
Expand Down Expand Up @@ -75,6 +82,33 @@ public function __construct($attributes = [])
static::bootUnlessBooted();
}

public function getPublishedAttribute ($published) {
global $_mode;

if ($published && $this->use_time) {
$start = null;

try {
$start = Carbon::parse($this->start);
} catch (Exception $ex) {
$start = Carbon::parse(0);
}

try {
$stop = $this->stop ? $this->stop : PHP_INT_MAX;
$stop = Carbon::parse($stop);
} catch (Exception $ex) {
$stop = Carbon::parse(PHP_INT_MAX);
}

$now = Carbon::now();

return $now->gte($start) && $now->lte($stop);
}

return (bool) $published;
}

public function save()
{
static::performHookOn($this, 'saving');
Expand Down Expand Up @@ -273,7 +307,7 @@ public static function all()
$cacheKey = 'entry/' . $entry['id'];
NF::$cache->save($cacheKey, $entry);
return static::generateObject($entry);
}, $response))->values();
}, $response))->filter()->values();
}

public static function find($id)
Expand Down Expand Up @@ -386,63 +420,63 @@ public static function resolveOrFail($slug)
public static function query(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'query'], $args);
}

public static function count()
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'count'], []);
}

public static function where(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'where'], $args);
}

public static function pluck(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'pluck'], $args);
}

public static function whereBetween(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'whereBetween'], $args);
}

public static function orderBy(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'orderBy'], $args);
}

public static function first()
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'firstOrFail'], []);
}

public static function paginate(...$args)
{
$structureId = (new static)->directory;
$query = new StructureQuery($structureId, new static);
$query = new StructureQuery($structureId, new static, static::$_fetch_unpublished);

return call_user_func_array([$query, 'paginate'], $args);
}
Expand All @@ -462,7 +496,15 @@ public static function generateObject($data)
return $revision;
}

return new static($data);
$entry = new static($data);

if ($entry) {
if (!$_mode && static::$_fetch_unpublished === false || static::$_fetch_unpublished === false && $_mode === 'cli') {
return $entry->published ? $entry : null;
}

return $entry;
}
}

private static function bootUnlessBooted()
Expand Down
6 changes: 5 additions & 1 deletion src/model/StructureQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ class StructureQuery
private $_firstPageSize = null;
private $_fieldsModified = false;

public function __construct($directory, $class)
public function __construct($directory, $class, $fetchUnpublished = true)
{
$this->_class = $class;
$this->_directory = $directory;
$this->_search = new ElasticSearch();
$this->_search->directory($this->_directory);
$this->_search->limit(10000);

if (!$fetchUnpublished) {
$this->_search->where('published', true);
}
}

private function isArgsArray($args)
Expand Down

0 comments on commit 5feda37

Please sign in to comment.