]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Entity/Adherent.php
Cannot set member as its own parent; closes #1806
[galette.git] / galette / lib / Galette / Entity / Adherent.php
index e6753efa008d5d3e9ab80b94cc2c782b962ec1a4..f8de8e0f03d78e69bf3de823d63f85501372e7a0 100644 (file)
@@ -38,6 +38,7 @@ namespace Galette\Entity;
 
 use ArrayObject;
 use Galette\Events\GaletteEvent;
+use Galette\Features\HasEvent;
 use Galette\Features\Socials;
 use Throwable;
 use Analog\Analog;
@@ -70,7 +71,7 @@ use Galette\Features\Dynamics;
  * @property string $stitle Title label
  * @property string $company_name
  * @property string $name
- * @property string $surname
+ * @property ?string $surname
  * @property string $nickname
  * @property string $birthdate Localized birthdate
  * @property string $rbirthdate Raw birthdate
@@ -116,7 +117,7 @@ use Galette\Features\Dynamics;
  * @property string $sname
  * @property string $saddress
  * @property string $contribstatus State of member contributions
- * @property string $days_remaining
+ * @property integer $days_remaining
  * @property-read integer $parent_id
  * @property Social $social Social networks/Contact
  * @property string $number Member number
@@ -126,6 +127,7 @@ class Adherent
 {
     use Dynamics;
     use Socials;
+    use HasEvent;
 
     public const TABLE = 'adherents';
     public const PK = 'id_adh';
@@ -183,7 +185,7 @@ class Adherent
     private $_groups = [];
     private $_managed_groups = [];
     private $_parent;
-    private $_children;
+    private $_children = [];
     private $_duplicate = false;
     private $_socials;
     private $_number;
@@ -191,15 +193,6 @@ class Adherent
     private $_row_classes;
 
     private $_self_adh = false;
-    private $_deps = array(
-        'picture'   => true,
-        'groups'    => true,
-        'dues'      => true,
-        'parent'    => false,
-        'children'  => false,
-        'dynamics'  => false,
-        'socials'   => false
-    );
 
     private $zdb;
     private $preferences;
@@ -234,16 +227,10 @@ class Adherent
 
         if ($deps !== null) {
             if (is_array($deps)) {
-                $this->_deps = array_merge(
-                    $this->_deps,
-                    $deps
-                );
+                $this->setDeps($deps);
             } elseif ($deps === false) {
                 //no dependencies
-                $this->_deps = array_fill_keys(
-                    array_keys($this->_deps),
-                    false
-                );
+                $this->disableAllDeps();
             } else {
                 Analog::log(
                     '$deps should be an array, ' . gettype($deps) . ' given!',
@@ -252,6 +239,12 @@ class Adherent
             }
         }
 
+        $this
+            ->withAddEvent()
+            ->withEditEvent()
+            ->withoutDeleteEvent()
+            ->activateEvents();
+
         if ($args == null || is_int($args)) {
             if (is_int($args) && $args > 0) {
                 $this->load($args);
@@ -455,7 +448,7 @@ class Adherent
      */
     private function loadParent(): void
     {
-        if (!$this->_parent instanceof Adherent) {
+        if ($this->_parent !== null && !$this->_parent instanceof Adherent) {
             $deps = array_fill_keys(array_keys($this->_deps), false);
             $this->_parent = new Adherent($this->zdb, (int)$this->_parent, $deps);
         }
@@ -757,7 +750,7 @@ class Adherent
      */
     public function getRowClass(bool $public = false): string
     {
-        $strclass = ($this->isActive()) ? 'active' : 'inactive';
+        $strclass = ($this->isActive()) ? 'active-account' : 'inactive-account';
         if ($public === false) {
             $strclass .= $this->_row_classes;
         }
@@ -1331,30 +1324,29 @@ class Adherent
                     $this->errors[] = _T("- Non-valid E-Mail address!") .
                         ' (' . $this->getFieldLabel($field) . ')';
                 }
-                if ($field == 'email_adh') {
-                    try {
-                        $select = $this->zdb->select(self::TABLE);
-                        $select->columns(
-                            array(self::PK)
-                        )->where(array('email_adh' => $value));
-                        if (!empty($this->_id)) {
-                            $select->where->notEqualTo(
-                                self::PK,
-                                $this->_id
-                            );
-                        }
 
-                        $results = $this->zdb->execute($select);
-                        if ($results->count() !== 0) {
-                            $this->errors[] = _T("- This E-Mail address is already used by another member!");
-                        }
-                    } catch (Throwable $e) {
-                        Analog::log(
-                            'An error occurred checking member email uniqueness.',
-                            Analog::ERROR
+                try {
+                    $select = $this->zdb->select(self::TABLE);
+                    $select->columns(
+                        array(self::PK)
+                    )->where(array('email_adh' => $value));
+                    if (!empty($this->_id)) {
+                        $select->where->notEqualTo(
+                            self::PK,
+                            $this->_id
                         );
-                        $this->errors[] = _T("An error has occurred while looking if login already exists.");
                     }
+
+                    $results = $this->zdb->execute($select);
+                    if ($results->count() !== 0) {
+                        $this->errors[] = _T("- This E-Mail address is already used by another member!");
+                    }
+                } catch (Throwable $e) {
+                    Analog::log(
+                        'An error occurred checking member email uniqueness.',
+                        Analog::ERROR
+                    );
+                    $this->errors[] = _T("An error has occurred while looking if login already exists.");
                 }
                 break;
             case 'login_adh':
@@ -1465,8 +1457,14 @@ class Adherent
                 }
                 break;
             case 'parent_id':
-                $this->$prop = ($value instanceof Adherent) ? (int)$value->id : (int)$value;
-                $this->loadParent();
+                $pid = ($value instanceof Adherent) ? (int)$value->id : (int)$value;
+                if ($pid === $this->id) {
+                    $this->errors[] = _T("A member cannot be its own parent!");
+                    $this->$prop = null;
+                } else {
+                    $this->$prop = $pid;
+                    $this->loadParent();
+                }
                 break;
         }
     }
@@ -1558,7 +1556,6 @@ class Adherent
                 }
             }
 
-            $success = false;
             if (empty($this->_id)) {
                 //we're inserting a new member
                 unset($values[self::PK]);
@@ -1585,9 +1582,8 @@ class Adherent
                             $this->sname
                         );
                     }
-                    $success = true;
 
-                    $event = 'member.add';
+                    $event = $this->getAddEventName();
                 } else {
                     $hist->add(_T("Fail to add new member."));
                     throw new \Exception(
@@ -1623,21 +1619,18 @@ class Adherent
                         $this->sname
                     );
                 }
-                $success = true;
-                $event = 'member.edit';
+                $event = $this->getEditEventName();
             }
 
             //dynamic fields
-            if ($success) {
-                $success = $this->dynamicsStore();
-                $this->storeSocials($this->id);
-            }
+            $this->dynamicsStore();
+            $this->storeSocials($this->id);
 
             //send event at the end of process, once all has been stored
-            if ($event !== null) {
+            if ($event !== null && $this->areEventsEnabled()) {
                 $emitter->dispatch(new GaletteEvent($event, $this));
             }
-            return $success;
+            return true;
         } catch (Throwable $e) {
             Analog::log(
                 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
@@ -1662,7 +1655,7 @@ class Adherent
                 array('date_modif_adh' => $modif_date)
             )->where([self::PK => $this->_id]);
 
-            $edit = $this->zdb->execute($update);
+            $this->zdb->execute($update);
             $this->_modification_date = $modif_date;
         } catch (Throwable $e) {
             Analog::log(
@@ -1734,21 +1727,21 @@ class Adherent
             }
             switch ($name) {
                 case 'sadmin':
+                    return (($this->isAdmin()) ? _T("Yes") : _T("No"));
                 case 'sdue_free':
+                    return (($this->isDueFree()) ? _T("Yes") : _T("No"));
                 case 'sappears_in_list':
+                    return (($this->appearsInMembersList()) ? _T("Yes") : _T("No"));
                 case 'sstaff':
                     return (($this->$real) ? _T("Yes") : _T("No"));
-                    break;
                 case 'sactive':
-                    return (($this->$real) ? _T("Active") : _T("Inactive"));
-                    break;
+                    return (($this->isActive()) ? _T("Active") : _T("Inactive"));
                 case 'stitle':
                     if (isset($this->_title) && $this->_title instanceof Title) {
                         return $this->_title->tshort;
                     } else {
                         return null;
                     }
-                    break;
                 case 'sstatus':
                     $status = new Status($this->zdb);
                     return $status->getLabel($this->_status);
@@ -1909,8 +1902,6 @@ class Adherent
             default:
                 return property_exists($this, $rname);
         }
-
-        return false;
     }
 
     /**
@@ -1923,12 +1914,11 @@ class Adherent
     public function getEmail(): string
     {
         $email = $this->_email;
-        if (empty($email)) {
+        if (empty($email) && $this->hasParent()) {
             $this->loadParent();
             $email = $this->parent->email;
         }
 
-        //@phpstan-ignore-next-line
         return $email ?? '';
     }
 
@@ -2043,11 +2033,12 @@ class Adherent
     /**
      * Handle files (photo and dynamics files)
      *
-     * @param array $files Files sent
+     * @param array $files    Files sent
+     * @param array $cropping Cropping properties
      *
      * @return array|true
      */
-    public function handleFiles(array $files)
+    public function handleFiles(array $files, array $cropping = null)
     {
         $this->errors = [];
         // picture upload
@@ -2055,7 +2046,11 @@ class Adherent
             if ($files['photo']['error'] === UPLOAD_ERR_OK) {
                 if ($files['photo']['tmp_name'] != '') {
                     if (is_uploaded_file($files['photo']['tmp_name'])) {
-                        $res = $this->picture->store($files['photo']);
+                        if ($this->preferences->pref_force_picture_ratio == 1 && isset($cropping)) {
+                            $res = $this->picture->store($files['photo'], false, $cropping);
+                        } else {
+                            $res = $this->picture->store($files['photo']);
+                        }
                         if ($res < 0) {
                             $this->errors[]
                                 = $this->picture->getErrorMessage($res);
@@ -2288,82 +2283,12 @@ class Adherent
     }
 
     /**
-     * Reset dependencies to load
-     *
-     * @return $this
-     */
-    public function disableAllDeps(): self
-    {
-        foreach ($this->_deps as &$dep) {
-            $dep = false;
-        }
-        return $this;
-    }
-
-    /**
-     * Enable all dependencies to load
-     *
-     * @return $this
-     */
-    public function enableAllDeps(): self
-    {
-        foreach ($this->_deps as &$dep) {
-            $dep = true;
-        }
-        return $this;
-    }
-
-    /**
-     * Enable a load dependency
-     *
-     * @param string $name Dependency name
+     * Get prefix for events
      *
-     * @return $this
-     */
-    public function enableDep(string $name): self
-    {
-        if (!isset($this->_deps[$name])) {
-            Analog::log(
-                'dependency ' . $name . ' does not exists!',
-                Analog::WARNING
-            );
-        } else {
-            $this->_deps[$name] = true;
-        }
-
-        return $this;
-    }
-
-    /**
-     * Enable a load dependency
-     *
-     * @param string $name Dependency name
-     *
-     * @return $this
-     */
-    public function disableDep(string $name): self
-    {
-        if (!isset($this->_deps[$name])) {
-            Analog::log(
-                'dependency ' . $name . ' does not exists!',
-                Analog::WARNING
-            );
-        } else {
-            $this->_deps[$name] = false;
-        }
-
-        return $this;
-    }
-
-    /**
-     * Is load dependency enabled?
-     *
-     * @param string $name Dependency name
-     *
-     * @return boolean
+     * @return string
      */
-    protected function isDepEnabled(string $name): bool
+    protected function getEventsPrefix(): string
     {
-        return $this->_deps[$name];
+        return 'member';
     }
 }