Commit e7135457 authored by Guillaume Wauquier's avatar Guillaume Wauquier

Merge branch '8-kml-refactoring' into ajout-des-dates-creation-modification

parents d8f36648 71a8b6c6
......@@ -31,6 +31,9 @@ abstract class APIHelper implements API
/** Override this const in children classes
* to the full-qualified model name. */
const MODEL_NAME = null;
/** Override this const in children classes to add a default sorting order.
* It is a string or an array passed to DAO->search. */
const DEFAULT_ORDER = null;
public function __construct($dao) {
$this->dao = $dao;
......@@ -40,6 +43,14 @@ abstract class APIHelper implements API
return new static($app->getDao());
}
/** Use default order if no order is given. */
protected function getOrder($order) {
if ($order === null && static::DEFAULT_ORDER !== null) {
$order = static::DEFAULT_ORDER;
}
return $order;
}
/** Get a single entry from it's ID. */
public function get($id) {
return $this->dao->read(static::MODEL_NAME, $id);
......@@ -48,6 +59,7 @@ abstract class APIHelper implements API
/** Get all entries.
* @param $order The order fields (see DAO->search) */
public function getAll($order = null) {
$order = $this->getOrder($order);
return $this->dao->search(static::MODEL_NAME,
null, null, null, $order);
}
......@@ -57,6 +69,7 @@ abstract class APIHelper implements API
}
public function search($conditions, $count = null, $offset = 0, $order = null) {
$order = $this->getOrder($order);
return $this->dao->search(static::MODEL_NAME, $conditions,
$count, $offset, $order);
}
......
......@@ -28,6 +28,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class CategoryAPI extends APIRefHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\Category';
const DEFAULT_ORDER = ['dispOrder', 'reference'];
private function fillChildrenCategories($cat, &$result) {
$children = $this->dao->search(static::MODEL_NAME,
......
......@@ -26,6 +26,7 @@ namespace Pasteque\Server\API;
class CustomerAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\Customer';
const DEFAULT_ORDER = 'dispName';
public function getTopIds($limit = 10) {
// This is a report, but there are no reports.
......
......@@ -31,6 +31,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class PaymentmodeAPI extends APIHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\PaymentMode';
const DEFAULT_ORDER = 'dispOrder';
// Same as APIHelper::write but delete return and values for replacement.
public function write($data) {
......
......@@ -29,6 +29,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class PlaceAPI extends APIHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\Floor';
const DEFAULT_ORDER = 'dispOrder';
// Same as APIHelper::write but delete all places for replacement.
public function write($data) {
......
......@@ -29,6 +29,8 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class ProductAPI extends APIRefHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\Product';
const DEFAULT_ORDER = ['dispOrder', 'label'];
private static $VISIBLE_CONDITION = null;
private static $ARCHIVE_CONDITION = null;
private static function visibleCondition() {
......@@ -46,14 +48,16 @@ class ProductAPI extends APIRefHelper
/** Get all Products currently in sold. */
public function getAll($order = null) {
$order = $this->getOrder($order);
return $this->dao->search(static::MODEL_NAME,
static::visibleCondition());
static::visibleCondition(), null, null, $order);
}
/** Get all Products currently not in sold. */
public function getArchive() {
$order = $this->getOrder(null);
return $this->dao->search(static::MODEL_NAME,
static::archiveCondition());
static::archiveCondition(), null, null, $order);
}
/** Get all products from a category, excluding archive.
......@@ -61,17 +65,18 @@ class ProductAPI extends APIRefHelper
* Valid Id is an int or a numeric string.
* @throws \InvalidArgumentException If $category is not of valid type. */
public function getFromCategory($category) {
$order = $this->getOrder(null);
if (is_numeric($category)) {
$cat = $this->dao->read(Category::class, intval($category));
if ($cat != null) {
return $this->dao->search(static::MODEL_NAME,
[new DAOCondition('category', '=', $cat),
static::visibleCondition()]);
static::visibleCondition()], null, null, $order);
}
} else if (is_object($category) && is_a($category, Category::class)) {
return $this->dao->search(static::MODEL_NAME,
[new DAOCondition('category', '=', $category),
static::visibleCondition()]);
static::visibleCondition()], null, null, $order);
} else {
throw new \InvalidArgumentException('Incompatible category type');
}
......
......@@ -26,4 +26,5 @@ namespace Pasteque\Server\API;
class RoleAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\Role';
const DEFAULT_ORDER = 'name';
}
\ No newline at end of file
......@@ -30,6 +30,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class TariffareaAPI extends APIHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\TariffArea';
const DEFAULT_ORDER = 'dispOrder';
// Same as APIHelper::write but delete all price for replacement.
public function write($data) {
......
......@@ -26,4 +26,5 @@ namespace Pasteque\Server\API;
class TaxAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\Tax';
const DEFAULT_ORDER = '-rate';
}
\ No newline at end of file
......@@ -32,6 +32,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class TicketAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\Ticket';
const DEFAULT_ORDER = '-number';
/** Check for duplicated ticket in database. This is required because
* Doctrine will crash everything if a DB exception is thrown. */
......
......@@ -29,9 +29,7 @@ use \Pasteque\Server\System\DAO\DAOCondition;
class UserAPI extends APIHelper implements API
{
const MODEL_NAME = 'Pasteque\Server\Model\User';
const DEFAULT_ORDER = 'name';
public function getByName($name)
{
......
......@@ -107,7 +107,8 @@ class CompositionGroup extends DoctrineModel
public function setImage($image) { $this->image = $image; }
/**
* @OneToMany(targetEntity="\Pasteque\Server\Model\CompositionProduct", mappedBy="compositionGroup", cascade={"persist"}, orphanRemoval=true) */
* @OneToMany(targetEntity="\Pasteque\Server\Model\CompositionProduct", mappedBy="compositionGroup", cascade={"persist"}, orphanRemoval=true)
* @OrderBy({"dispOrder" = "ASC"}) */
protected $compositionProducts;
public function getCompositionProducts() { return $this->compositionProducts; }
public function setCompositionProducts($compositionProducts) {
......
......@@ -279,7 +279,8 @@ class Product extends DoctrineModel
public function setComposition($composition) { $this->composition = $composition; }
/**
* @OneToMany(targetEntity="\Pasteque\Server\Model\CompositionGroup", mappedBy="product", cascade={"persist"}, orphanRemoval=true) */
* @OneToMany(targetEntity="\Pasteque\Server\Model\CompositionGroup", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
* @OrderBy({"dispOrder" = "ASC"}) */
protected $compositionGroups;
public function getcompositionGroups() { return $this->compositionGroups; }
public function setCompositionGroups($compositionGroups) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment