Maslosoft Manganel API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/manganel
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
* @link https://maslosoft.com/manganel/
*/
namespace Maslosoft\Manganel;
use Maslosoft\Mangan\Interfaces\DataProviderInterface;
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait;
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait;
use Maslosoft\Mangan\Traits\DataProvider\DataTrait;
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait;
use Maslosoft\Mangan\Traits\ModelAwareTrait;
use Maslosoft\Mangan\Traits\SortAwareTrait;
use Maslosoft\Manganel\Helpers\Debug\DebugSearchProvider;
/**
* SearchProvider
*
* @method SearchCriteria getCriteria()
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class SearchProvider implements DataProviderInterface
{
use ConfigureTrait,
DataTrait,
ModelAwareTrait,
PaginationTrait,
SortAwareTrait,
CriteriaTrait
{
CriteriaTrait::setCriteria as traitSetCriteria;
}
const CriteriaClass = SearchCriteria::class;
/**
* Finder instance
*
* This is protected, not private so it can be debugged
* @see DebugSearchProvider
* @var SearchFinder
*/
protected $finder = null;
/**
* Total items count cache
* @var int
*/
private $totalItemCount = null;
/**
* When stopped no data will be retrieved. Used by external libraries.
* @var boolean
*/
private $isStopped = false;
public function __construct($modelClass = null, $config = [])
{
if (is_array($modelClass))
{
$models = $modelClass;
}
else
{
$models = [$modelClass];
}
foreach ($models as $modelClass)
{
$this->configure($modelClass, $config);
}
$this->finder = new SearchFinder($models);
$criteria = $this->getCriteria();
assert($criteria instanceof SearchCriteria);
$criteria->setModels($models);
}
public function stop($doStop = true)
{
$this->isStopped = $doStop;
}
protected function fetchData()
{
if ($this->isStopped)
{
return [];
}
$criteria = $this->configureFetch();
$models = $criteria->getModels();
if (!empty($models))
{
$this->finder->setModels($models);
}
return $this->finder->findAll($criteria);
}
public function setCriteria($criteria)
{
assert($criteria instanceof SearchCriteria);
$criteria->setModels($this->finder->getModels());
return $this->traitSetCriteria($criteria);
}
public function getItemCount($refresh = false)
{
return count($this->getData($refresh));
}
public function getTotalItemCount()
{
if ($this->isStopped)
{
$this->totalItemCount = 0;
}
if ($this->totalItemCount === null)
{
$qb = new QueryBuilder($this->getModel());
/**
* TODO Must count with criteria too!
* And multi model
*/
$criteria = new SearchCriteria($this->getCriteria());
$criteria->setModels($this->finder->getModels());
$criteria->setLimit(false);
$criteria->setOffset(false);
$qb->setCriteria($criteria);
$this->totalItemCount = $qb->count($this->getCriteria()->getSearch());
}
return $this->totalItemCount;
}
}
API documentation generated by ApiGen