Skip to content

Commit

Permalink
Phpcbf
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelh78 committed Apr 16, 2024
1 parent 79379e6 commit e4b3aa1
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 73 deletions.
6 changes: 3 additions & 3 deletions galette/includes/routes/management.routes.php
Expand Up @@ -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}]',
Expand Down
22 changes: 17 additions & 5 deletions galette/lib/Galette/Controllers/Crud/CrudHelper.php
Expand Up @@ -4,18 +4,31 @@

use Slim\App;

/**
* CrudHelper
*
* @author Manuel <manuelh78dev@ik.me>
*/
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",
Expand Down Expand Up @@ -46,7 +59,6 @@ public static function AddRoutesBasicEntityCRUD(App $app, string $controllerClas
"/{$basePath}/edit/{id:\d+}",
[$controllerClassName, 'doEdit']
)->setname("edit{$entity}")->add($authenticate);

}
}

Expand Down
Expand Up @@ -29,9 +29,9 @@
use Analog\Analog;

/**
* Galette Legal status controller
* LegalStatuss Controller
*
* @author Johan Cwiklinski <johan@x-tnd.be>
* @author Manuel <manuelh78dev@ik.me>
*/

class LegalStatussController extends CrudController
Expand Down
2 changes: 1 addition & 1 deletion galette/lib/Galette/Controllers/Crud/MembersController.php
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion galette/lib/Galette/Core/Galette.php
Expand Up @@ -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"),
Expand Down
6 changes: 4 additions & 2 deletions galette/lib/Galette/Entity/Adherent.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
69 changes: 47 additions & 22 deletions galette/lib/Galette/Entity/Base/EntityFromDb.php
Expand Up @@ -8,6 +8,11 @@
use Analog\Analog;
use Galette\Features\I18n;

/**
* base class EntityFromDb
*
* @author Manuel <manuelh78dev@ik.me>
*/
class EntityFromDb
{
use I18n;
Expand All @@ -24,11 +29,16 @@ class EntityFromDb
private $i18nProperties = [];


protected $TABLE, $PK;
protected $TABLE;
protected $PK;

/**
* Main constructor
*
* @param int|ArrayObject<string, int|string>|null $args Arguments
* @param DB $zdb Database
* @param array<string,string> $tableDescription propertyname => db column name
* @param array $options add virtual properties, override or valid a value...
* @param int|ArrayObject<string, int|string>|null $args Arguments
*/
public function __construct($zdb, $tableDescription, $options, int|ArrayObject $args = null)
{
Expand Down Expand Up @@ -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" .
Expand All @@ -95,7 +106,7 @@ public function load(int $id): void
}

/**
* Load entity from a db ResultSet
* Load entity from a DB ResultSet
*
* @param ArrayObject<string, int|string> $rs ResultSet
*
Expand Down Expand Up @@ -128,8 +139,6 @@ public function __toString(): string
/**
* Store this entity in database
*
* @param Db $zdb Database instance
*
* @return boolean
*/
public function store(): bool
Expand All @@ -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]);
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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)) {
Expand Down Expand Up @@ -312,7 +321,7 @@ public function __set(string $name, $value): void
}

/**
* setValue()
* setValue
*
* @param string $name Property name
* @param mixed $value Property value
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -374,5 +400,4 @@ public function __call($name, $arguments)
}
throw new \RuntimeException("Entity::$name property not available.");
}

}
13 changes: 12 additions & 1 deletion galette/lib/Galette/Entity/Base/Translate.php
Expand Up @@ -2,10 +2,21 @@

namespace Galette\Entity\Base;

/**
* Translate helper
*
* @author Manuel <manuelh78dev@ik.me>
*/
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);
}
}
}
19 changes: 14 additions & 5 deletions galette/lib/Galette/Entity/LegalStatus.php
Expand Up @@ -6,13 +6,24 @@
use ArrayObject;
use Galette\Entity\Base\EntityFromDb;

/**
* LegalStatus
*
* @author Manuel <manuelh78dev@ik.me>
*/
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(
Expand All @@ -22,21 +33,19 @@ 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
);
}

/**
* Remove current legal status
*
* @param Db $zdb Database instance
*
* @return boolean
*/
public function remove(): bool
Expand Down
10 changes: 6 additions & 4 deletions galette/lib/Galette/Entity/PaymentType.php
Expand Up @@ -34,8 +34,6 @@
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*
* @property integer $id
* @property string $name
*/

class PaymentType extends EntityFromDb
Expand All @@ -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(
Expand Down Expand Up @@ -137,5 +140,4 @@ public function isSystemType(): bool
{
return isset($this->getSystemTypes()[$this->id]);
}

}

0 comments on commit e4b3aa1

Please sign in to comment.