diff --git a/galette/includes/routes/management.routes.php b/galette/includes/routes/management.routes.php index 421fddd94..14eae95b8 100644 --- a/galette/includes/routes/management.routes.php +++ b/galette/includes/routes/management.routes.php @@ -208,9 +208,9 @@ function ($request, $response) use ($routeparser) { [PdfController::class, 'storeModels'] )->setName('pdfModels')->add($authenticate); -CrudHelper::AddRoutesBasicEntityCRUD($app, Crud\TitlesController::class, $authenticate); -CrudHelper::AddRoutesBasicEntityCRUD($app, Crud\LegalStatussController::class, $authenticate); -CrudHelper::AddRoutesBasicEntityCRUD($app, Crud\PaymentTypesController::class, $authenticate); +CrudHelper::addRoutesBasicEntityCRUD($app, Crud\TitlesController::class, $authenticate); +CrudHelper::addRoutesBasicEntityCRUD($app, Crud\LegalStatussController::class, $authenticate); +CrudHelper::addRoutesBasicEntityCRUD($app, Crud\PaymentTypesController::class, $authenticate); $app->get( '/texts[/{lang}/{ref}]', diff --git a/galette/lib/Galette/Controllers/Crud/CrudHelper.php b/galette/lib/Galette/Controllers/Crud/CrudHelper.php index af797e69b..862f62639 100644 --- a/galette/lib/Galette/Controllers/Crud/CrudHelper.php +++ b/galette/lib/Galette/Controllers/Crud/CrudHelper.php @@ -4,18 +4,31 @@ use Slim\App; +/** + * CrudHelper + * + * @author Manuel + */ class CrudHelper { - //Ajouter des routes automatiquement pour un object Entity via le nom de son controller - public static function AddRoutesBasicEntityCRUD(App $app, string $controllerClassName, $authenticate) + /** + * Ajouter des routes automatiquement pour un object Entity via le nom de son controller + * + * @param Slim\App $app Slim application + * @param string $controllerClassName controller class name AController::class + * @param $authenticate Middleware for user authentification + * + * @return void + */ + public static function addRoutesBasicEntityCRUD(App $app, string $controllerClassName, $authenticate) { //Galette\Controllers\Crud\TitlesController -> Title $entity = str_replace(['Controller', '\\'], ['', '/'], $controllerClassName); $entity = basename($entity); $entity = substr($entity, 0, -1); - $baseName=lcfirst($entity); - $basePath=strtolower($entity); + $baseName = lcfirst($entity); + $basePath = strtolower($entity); $app->get( "/{$basePath}s", @@ -46,7 +59,6 @@ public static function AddRoutesBasicEntityCRUD(App $app, string $controllerClas "/{$basePath}/edit/{id:\d+}", [$controllerClassName, 'doEdit'] )->setname("edit{$entity}")->add($authenticate); - } } diff --git a/galette/lib/Galette/Controllers/Crud/LegalStatussController.php b/galette/lib/Galette/Controllers/Crud/LegalStatussController.php index cc2089fc2..6781fc7f7 100644 --- a/galette/lib/Galette/Controllers/Crud/LegalStatussController.php +++ b/galette/lib/Galette/Controllers/Crud/LegalStatussController.php @@ -29,9 +29,9 @@ use Analog\Analog; /** - * Galette Legal status controller + * LegalStatuss Controller * - * @author Johan Cwiklinski + * @author Manuel */ class LegalStatussController extends CrudController diff --git a/galette/lib/Galette/Controllers/Crud/MembersController.php b/galette/lib/Galette/Controllers/Crud/MembersController.php index 17c2978c6..d4b2fe36e 100644 --- a/galette/lib/Galette/Controllers/Crud/MembersController.php +++ b/galette/lib/Galette/Controllers/Crud/MembersController.php @@ -988,7 +988,7 @@ public function edit( //Titles $titles = new Titles($this->zdb); //LegalStatus - $legalStatus = new LegalStatuss($this->zdb); + $legalStatus = new LegalStatuss($this->zdb); //Groups $groups = new Groups($this->zdb, $this->login); diff --git a/galette/lib/Galette/Core/Galette.php b/galette/lib/Galette/Core/Galette.php index b2a6db3aa..44826e90a 100644 --- a/galette/lib/Galette/Core/Galette.php +++ b/galette/lib/Galette/Core/Galette.php @@ -413,7 +413,7 @@ public static function getMenus(bool $public = false): array 'name' => 'legalStatuss', 'aliases' => ['editLegalStatus'] ] - ], + ], [ 'label' => _T("PDF models"), 'title' => _T("Manage PDF models"), diff --git a/galette/lib/Galette/Entity/Adherent.php b/galette/lib/Galette/Entity/Adherent.php index d45f86b08..91a9f281c 100644 --- a/galette/lib/Galette/Entity/Adherent.php +++ b/galette/lib/Galette/Entity/Adherent.php @@ -130,7 +130,7 @@ class Adherent private ?int $id; //Identity private Title|string|null $title = null; - private int $id_legal_status=LegalStatus::INDIVIDUAL; + private int $id_legal_status = LegalStatus::INDIVIDUAL; private ?string $company_name; private ?string $name; private ?string $surname; @@ -1402,7 +1402,9 @@ public function validate(string $field, $value, array $values): void } break; case 'id_legal_status': - if ($value !== '') $this->id_legal_status = intval($value); + if ($value !== '') { + $this->id_legal_status = intval($value); + } break; case 'email_adh': if (!GaletteMail::isValidEmail($value)) { diff --git a/galette/lib/Galette/Entity/Base/EntityFromDb.php b/galette/lib/Galette/Entity/Base/EntityFromDb.php index 4df8f0f1d..3e25e0cfd 100644 --- a/galette/lib/Galette/Entity/Base/EntityFromDb.php +++ b/galette/lib/Galette/Entity/Base/EntityFromDb.php @@ -8,6 +8,11 @@ use Analog\Analog; use Galette\Features\I18n; +/** + * base class EntityFromDb + * + * @author Manuel + */ class EntityFromDb { use I18n; @@ -24,11 +29,16 @@ class EntityFromDb private $i18nProperties = []; - protected $TABLE, $PK; + protected $TABLE; + protected $PK; + /** * Main constructor * - * @param int|ArrayObject|null $args Arguments + * @param DB $zdb Database + * @param array $tableDescription propertyname => db column name + * @param array $options add virtual properties, override or valid a value... + * @param int|ArrayObject|null $args Arguments */ public function __construct($zdb, $tableDescription, $options, int|ArrayObject $args = null) { @@ -82,8 +92,9 @@ public function load(int $id): void $results = $this->zdb->execute($select); $res = $results->current(); - if ($res) + if ($res) { $this->loadFromRs($res); + } } catch (Throwable $e) { Analog::log( "Error when loading {$this->entity} (#$id) Message:\n" . @@ -95,7 +106,7 @@ public function load(int $id): void } /** - * Load entity from a db ResultSet + * Load entity from a DB ResultSet * * @param ArrayObject $rs ResultSet * @@ -128,8 +139,6 @@ public function __toString(): string /** * Store this entity in database * - * @param Db $zdb Database instance - * * @return boolean */ public function store(): bool @@ -143,7 +152,6 @@ public function store(): bool try { if (isset($this->id) && $this->id > 0) { - foreach ($this->i18nProperties as $prop) { if ($this->oldValues[$prop] !== null) { $this->deleteTranslation($this->oldValues[$prop]); @@ -193,7 +201,7 @@ public function remove(): bool $delete->where([$this->PK => $this->id]); $this->zdb->execute($delete); - //I18n + //I18n foreach ($this->i18nProperties as $prop) { $this->deleteTranslation($this->values[$prop]); } @@ -229,9 +237,9 @@ public function __get(string $name): mixed /** * getValue * - * @param string $name Property name + * @param string $name Property name * @param string $translated translate returned string - * + * * @return mixed */ public function getValue(string $name, bool $translated): mixed @@ -246,17 +254,17 @@ public function getValue(string $name, bool $translated): mixed //from other property $k = "$name:from"; if (self::getOption($k, $f)) { - if (is_callable($f)) + if (is_callable($f)) { $value = $f(); - elseif ($f != '') + } elseif ($f != '') { $value = $this->getValue($f, $translated); //equivalent $this->{$f}; + } $found = true; } } - if ($found) //value can be null - { - //override default + if ($found) { //value can be null + //override default $k = "$name:override"; if (array_key_exists($k, $this->options)) { $fct = $this->options[$k]; @@ -269,8 +277,9 @@ public function getValue(string $name, bool $translated): mixed if (array_key_exists($k, $this->options)) { $fct = $this->options[$k]; - if (!$fct($value)) + if (!$fct($value)) { throw new \Exception($name . ' ' . _T('invalid value !')); + } } if ($translated && in_array($name, $this->i18nProperties)) { @@ -312,7 +321,7 @@ public function __set(string $name, $value): void } /** - * setValue() + * setValue * * @param string $name Property name * @param mixed $value Property value @@ -335,12 +344,14 @@ public function setValue(string $name, $value): void if (array_key_exists($k, $this->options)) { $fct = $this->options[$k]; - if (!$fct($value)) + if (!$fct($value)) { throw new \Exception($name . ' ' . _T('invalid value !')); + } } - if (array_key_exists($name, $this->values)) + if (array_key_exists($name, $this->values)) { $this->oldValues[$name] = $this->values[$name]; + } $this->values[$name] = $value; } else { Analog::log( @@ -350,15 +361,30 @@ public function setValue(string $name, $value): void } } + /** + * getOption + * + * @param string $name Option name + * @param mixed $option by reference, Property value + * + * @return bool true if this option exist + */ private function getOption(string $name, mixed &$option): bool { - if (!array_key_exists($name, $this->options)) + if (!array_key_exists($name, $this->options)) { return false; + } $option = $this->options[$name]; return true; } - //Implement a getMyProperty() for all columns in database; example : getId() + /** + * __call + * Implement a getMyProperty() for all columns in database; example : getId() + * @param string $name Method name getXXXX + * @param array $arguments getXXXX([$arguments]) + * @return mixed optional returned value + */ public function __call($name, $arguments) { $arg1 = (count($arguments) >= 1) ? $arguments[0] : false; @@ -374,5 +400,4 @@ public function __call($name, $arguments) } throw new \RuntimeException("Entity::$name property not available."); } - } diff --git a/galette/lib/Galette/Entity/Base/Translate.php b/galette/lib/Galette/Entity/Base/Translate.php index 8e6244da5..65018d83f 100644 --- a/galette/lib/Galette/Entity/Base/Translate.php +++ b/galette/lib/Galette/Entity/Base/Translate.php @@ -2,10 +2,21 @@ namespace Galette\Entity\Base; +/** + * Translate helper + * + * @author Manuel + */ class Translate { + /** + * getFromLang + * + * @param string $value string to translate + * @return string Translates string in current language + */ public static function getFromLang(string $value): string { return _T(strip_tags($value), 'galette', false); } -} \ No newline at end of file +} diff --git a/galette/lib/Galette/Entity/LegalStatus.php b/galette/lib/Galette/Entity/LegalStatus.php index 6c903bddd..0006a3483 100644 --- a/galette/lib/Galette/Entity/LegalStatus.php +++ b/galette/lib/Galette/Entity/LegalStatus.php @@ -6,13 +6,24 @@ use ArrayObject; use Galette\Entity\Base\EntityFromDb; +/** + * LegalStatus + * + * @author Manuel + */ class LegalStatus extends EntityFromDb { public const TABLE = 'legalstatus'; public const PK = 'id_legalstatus'; - public const INDIVIDUAL = 1; //physical member + public const INDIVIDUAL = 1; //physical member + /** + * Main constructor + * + * @param DB $zdb Database + * @param ?ArrayObject|int $args item data to load + */ public function __construct(Db $zdb, ArrayObject|int $args = null) { parent::__construct( @@ -22,12 +33,12 @@ public function __construct(Db $zdb, ArrayObject|int $args = null) 'id' => self::PK, 'short' => 'short_label', 'long' => 'long_label', - ], + ], [ 'toString' => 'long', //Automatic add and removeTranslation() when store() 'i18n' => ['short', 'long'] - ], + ], $args ); } @@ -35,8 +46,6 @@ public function __construct(Db $zdb, ArrayObject|int $args = null) /** * Remove current legal status * - * @param Db $zdb Database instance - * * @return boolean */ public function remove(): bool diff --git a/galette/lib/Galette/Entity/PaymentType.php b/galette/lib/Galette/Entity/PaymentType.php index db444e9d3..c1535ab52 100644 --- a/galette/lib/Galette/Entity/PaymentType.php +++ b/galette/lib/Galette/Entity/PaymentType.php @@ -34,8 +34,6 @@ * * @author Johan Cwiklinski * - * @property integer $id - * @property string $name */ class PaymentType extends EntityFromDb @@ -53,7 +51,12 @@ class PaymentType extends EntityFromDb public const TRANSFER = 4; public const PAYPAL = 5; - + /** + * Main constructor + * + * @param DB $zdb Database + * @param ?ArrayObject|int $args item data to load + */ public function __construct(Db $zdb, ArrayObject|int $args = null) { parent::__construct( @@ -137,5 +140,4 @@ public function isSystemType(): bool { return isset($this->getSystemTypes()[$this->id]); } - } diff --git a/galette/lib/Galette/Entity/Title.php b/galette/lib/Galette/Entity/Title.php index 9034eabc7..39d73a11c 100644 --- a/galette/lib/Galette/Entity/Title.php +++ b/galette/lib/Galette/Entity/Title.php @@ -26,6 +26,11 @@ use Galette\Entity\Base\Translate; use ArrayObject; +/** + * Title + * + * @author Manuel + */ class Title extends EntityFromDb { public const TABLE = 'titles'; @@ -35,6 +40,12 @@ class Title extends EntityFromDb public const MRS = 2; public const MISS = 3; + /** + * Main constructor + * + * @param DB $zdb Database + * @param ?ArrayObject|int $args item data to load + */ public function __construct(Db $zdb, ArrayObject|int $args = null) { parent::__construct( @@ -51,17 +62,20 @@ public function __construct(Db $zdb, ArrayObject|int $args = null) //if long value is null get short value 'long:override' => function ($value) { - return ($value == null || trim($value) === '') ? $this->short : $value; }, + return ($value == null || trim($value) === '') ? $this->short : $value; + }, 'long:warningnoempty' => true, //Virtual properties - 'tshort:from' => 'short', //OR function() { return $this->short; }, + 'tshort:from' => 'short', //OR function() { return $this->short; }, 'tlong:from' => 'long', 'tshort:override' => function ($value) { - return Translate::getFromLang($value); }, + return Translate::getFromLang($value); + }, 'tlong:override' => function ($value) { - return Translate::getFromLang($value); }, + return Translate::getFromLang($value); + }, ], $args ); @@ -70,8 +84,6 @@ public function __construct(Db $zdb, ArrayObject|int $args = null) /** * Remove current title * - * @param Db $zdb Database instance - * * @return boolean */ public function remove(): bool @@ -83,6 +95,3 @@ public function remove(): bool return parent::remove(); } } - - - diff --git a/galette/lib/Galette/Repository/LegalStatuss.php b/galette/lib/Galette/Repository/LegalStatuss.php index 68337bba8..7c8dd16ba 100644 --- a/galette/lib/Galette/Repository/LegalStatuss.php +++ b/galette/lib/Galette/Repository/LegalStatuss.php @@ -9,9 +9,9 @@ use Galette\Entity\LegalStatus; /** - * Payment types + * LegalStatus repository * - * @author Johan Cwiklinski + * @author Manuel */ class LegalStatuss extends Repository { @@ -45,5 +45,4 @@ protected function loadDefaults(): array 'long_label' => 'Entreprise' )]; } - } diff --git a/galette/lib/Galette/Repository/PaymentTypes.php b/galette/lib/Galette/Repository/PaymentTypes.php index 5a00b03a4..a09e0e7fe 100644 --- a/galette/lib/Galette/Repository/PaymentTypes.php +++ b/galette/lib/Galette/Repository/PaymentTypes.php @@ -35,11 +35,12 @@ class PaymentTypes extends Repository { use RepositoryTrait; + public const TABLE = PaymentType::TABLE; public const PK = PaymentType::PK; - - + + /** * Checks for missing payment types in the database * @@ -88,7 +89,7 @@ protected function checkUpdate(): bool return false; } - + /** * Get defaults values diff --git a/galette/lib/Galette/Repository/Repository.php b/galette/lib/Galette/Repository/Repository.php index 258728722..e264a9a77 100644 --- a/galette/lib/Galette/Repository/Repository.php +++ b/galette/lib/Galette/Repository/Repository.php @@ -30,7 +30,6 @@ use Laminas\Db\ResultSet\ResultSet; use stdClass; - /** * Repositories * @@ -63,8 +62,8 @@ abstract class Repository */ public function __construct( Db $zdb, - Preferences $preferences=null, - Login $login=null, + Preferences $preferences = null, + Login $login = null, ?string $entity = null, ?string $ns = null, string $prefix = '' @@ -175,11 +174,9 @@ protected function setFilters(Pagination $filters): self * * @return array */ - //HM : TODO , pas clair, a approfondir protected function loadDefaults(): array { throw new \Exception("Error : you need to override loadDefaults()"); - //return $this->defaults; } /** diff --git a/galette/lib/Galette/Repository/RepositoryTrait.php b/galette/lib/Galette/Repository/RepositoryTrait.php index 5ac0b76ab..fc787d7e7 100644 --- a/galette/lib/Galette/Repository/RepositoryTrait.php +++ b/galette/lib/Galette/Repository/RepositoryTrait.php @@ -11,6 +11,11 @@ use DI\Attribute\Inject; use Analog\Analog; +/** + * RepositoryTrait + * + * @author Manuel + */ trait RepositoryTrait { private $entityClassName; @@ -50,10 +55,17 @@ public function getList(): array|ResultSet public static function getAll(): array { global $zdb; - $ptypes = new self($zdb); + $ptypes = new self($zdb); return $ptypes->getList(); } + /** + * Loads an entity from its id + * + * @param int $id Entry ID + * + * @return boolean true if query succeed, false otherwise + */ public function load(int $id): null|\ArrayObject { $select = $this->zdb->select(constant($this->entity . '::TABLE')); @@ -64,6 +76,12 @@ public function load(int $id): null|\ArrayObject $return = $results->current(); } + /** + * Count all entities + * + * + * @return int a count, 0 isf empty + */ public function countAll(): int { $ent = $this->entity; @@ -157,12 +175,12 @@ protected function checkUpdate(): bool /** * Insert values in database * - * @param string $table Table name + * @param string $table Table name * @param array $values Values to insert * * @return void */ - private function multipleInsert(string $table, array $values): void + protected function multipleInsert(string $table, array $values): void { foreach ($values as $row) { $insert = $this->zdb->insert($table); diff --git a/galette/lib/Galette/Repository/Titles.php b/galette/lib/Galette/Repository/Titles.php index 2594fb7b2..183d8cbd2 100644 --- a/galette/lib/Galette/Repository/Titles.php +++ b/galette/lib/Galette/Repository/Titles.php @@ -35,6 +35,7 @@ class Titles extends Repository { use RepositoryTrait; + public const TABLE = 'titles'; public const PK = 'id_title'; @@ -63,10 +64,6 @@ protected function loadDefaults(): array ]; } - - - - /** * Set default titles at install time