Sfoglia il codice sorgente

Improve InputUser for FormValidator - refs BT#12955

Angel Fernando Quiroz Campos 7 anni fa
parent
commit
2ff96dbc09

+ 38 - 22
main/inc/lib/formvalidator/Element/InputUser.php

@@ -1,56 +1,72 @@
 <?php
 /* For licensing terms, see /license.txt */
 
-use Chamilo\UserBundle\Entity\User as ChUser;
+use Chamilo\UserBundle\Entity\User;
 
 class InputUser extends HTML_QuickForm_input
 {
-    /** @var ChUser */
-    private $user;
-    private $imageSize;
-    private $subTitle;
+    /** @var User */
+    private $user = null;
+    private $imageSize = 'small';
+    private $subTitle = '';
 
-    public function __construct($name, $label, ChUser $user = null, $attributes = [])
+    public function __construct($name, $label, $attributes = [])
     {
-        $this->user = $user;
-        $this->imageSize = 'small';
-        $this->subTitle = null;
-
         if (isset($attributes['image_size'])) {
             $this->imageSize = $attributes['image_size'];
             unset($attributes['image_size']);
         }
 
+        if (isset($attributes['sub_title'])) {
+            $this->subTitle = $attributes['sub_title'];
+            unset($attributes['sub_title']);
+        }
+
         parent::__construct($name, $label, $attributes);
 
         $this->setType('hidden');
+    }
 
-        if ($this->user) {
-            $this->subTitle = $this->user->getUsername();
-            $this->setValue($this->user->getId());
-        }
+    /**
+     * @inheritDoc
+     */
+    public function setValue($value)
+    {
+        $this->user = !is_a($value, 'Chamilo\UserBundle\Entity\User')
+            ? UserManager::getManager()->find($value)
+            : $value;
 
-        if (isset($attributes['sub_title'])) {
-            $this->subTitle = $attributes['sub_title'];
-            unset($attributes['sub_title']);
-        }
+        parent::setValue($this->user->getId());
     }
 
     public function toHtml()
     {
+        if (!$this->user) {
+            return '';
+        }
+
+        $userInfo = api_get_user_info($this->user->getId());
+        $userPicture = isset($userInfo["avatar_{$this->imageSize}"])
+            ? $userInfo["avatar_{$this->imageSize}"]
+            : $userInfo["avatar"];
+
+        if (!$this->subTitle) {
+            $this->subTitle = $this->user->getUsername();
+        }
+
         $html = parent::toHtml();
         $html .= '
             <div class="media">
                 <div class="media-left">
-                    <img src="' . $this->user->getUserPicture($this->imageSize) . '" alt="' . $this->user->getCompleteName() . '">
+                    <img src="'.$userPicture.'" alt="'.$this->user->getCompleteName().'">
                 </div>
                 <div class="media-body">
-                    <h4 class="media-heading">' . $this->user->getCompleteName() . '</h4>
-                    ' . $this->subTitle . '
+                    <h4 class="media-heading">'.$this->user->getCompleteName().'</h4>
+                    '.$this->subTitle.'
                 </div>
             </div>
         ';
 
         return $html;
     }
-}
+}

+ 15 - 0
main/inc/lib/formvalidator/FormValidator.class.php

@@ -1751,6 +1751,21 @@ EOT;
             }
         }
     }
+
+    /**
+     * Add a \InputUser element to the form.
+     * It needs a Chamilo\UserBundle\Entity\User as value.
+     * The exported value is the Chamilo\UserBundle\Entity\User ID
+     * @param string $name
+     * @param string $label
+     * @param string $imageSize Optional. Small, medium or large image
+     * @param string $subtitle Optional. The subtitle for the field
+     * @return \InputUser
+     */
+    public function addInputUser($name, $label, $imageSize = 'small', $subtitle = '')
+    {
+        return $this->addElement('InputUser', $name, $label, ['image_size' => $imageSize, 'sub_title' => $subtitle]);
+    }
 }
 
 /**