| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 | <?php
namespace Muzich\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use \Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\Constraints as Assert;
use Muzich\CoreBundle\Validator as MuzichAssert;
use Muzich\CoreBundle\Entity\Tag;
/**
 * L'Element est l'Element central de l'application. C'est cet
 * entité qui stocke le media partagé sur le réseau.
 * 
 * @ORM\Entity
 * @ORM\Table(name="element")
 * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\ElementRepository")
 * 
 */
class Element
{
  
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @var type int
   */
  protected $id;
  /**
   * Cet attribut stocke le type d'élément.
   * 
   * @ORM\Column(type="string", length=64)
   * @Assert\NotBlank()
   * @Assert\MaxLength(1024)
   */
  protected $type;
  
  /**
   * Cet attribut stocke la liste des tags liés a cet élément.
   * 
   * @ORM\ManyToMany(targetEntity="Tag", inversedBy="elements")
   * @ORM\JoinTable(name="elements_tag")
   * @MuzichAssert\Tags()
   */
  private $tags;
  /**
   * Propriétaire de l'élément
   * 
   * @ORM\ManyToOne(targetEntity="User", inversedBy="elements")
   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
   */
  protected $owner;
  /**
   * Groupe de l'élément
   * 
   * @ORM\ManyToOne(targetEntity="Group", inversedBy="elements")
   * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
   * @MuzichAssert\GroupOwnedOrPublic()
   */
  protected $group = null;
  
  /**
   * Cet attribu stocke les enregistrements UsersElementsFavorites liés
   * a ce Tag dans le cadre des Elements favoris.
   * 
   * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="element")
   */
  protected $elements_favorites;
  
  /**
   * L'url est l'url du media. 
   * 
   * @ORM\Column(type="string", length=1024)
   * @Assert\NotBlank(message = "error.element.url.notblank")
   * @Assert\MaxLength(limit = 1024, message = "error.element.url.tolong")
   * @Assert\Url(message = "error.element.url.invalid")
   * @var type string
   */
  protected $url;
  
  /**
   * Libellé du media
   * 
   * @ORM\Column(type = "string", length = 128)
   * @Assert\NotBlank(message = "error.element.name.notblank")
   * @Assert\MinLength(limit = 3, message = "error.element.name.toshort")
   * @Assert\MaxLength(limit = 64, message = "error.element.name.tolong")
   * @var type string
   */
  protected $name;
  
  /**
   * Code d'embed
   * 
   * @ORM\Column(type="text", nullable=true)
   * @var type string
   */
  protected $embed;
  
  /**
   * @var datetime $created
   *
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   */
  private $created;
  /**
   * @var datetime $updated
   *
   * @ORM\Column(type="datetime")
   * @Gedmo\Timestampable(on="update")
   */
  private $updated;
  
  /**
   * @var string $thumbnail_url
   *
   * @ORM\Column(type="string", length=512, nullable=true)
   */
  protected $thumbnail_url;
  
  /**
   * Commentaires stocké au format json
   * 
   * array(
   *   array(
   *     "u" => array(              // Des infos sur l'utilisateur auteur du commentaire
   *       "i" => "IdDuUser",       // l'id
   *       "s" => "LeSlugDuUser",   // le slug
   *       "n" => "NameDuUser"      // le name
   *     ),
   *     "d" => "LaDate",        // Date au format Y-m-d H:i:s
   *     "c" => "Comment"           // Le commentaire
   *   ),
   *   [...]
   * );
   * 
   * @ORM\Column(type="text", nullable=true)
   * @var type string
   */
  protected $comments;
  
  /**
   * Compteur de signalements
   * 
   * @ORM\Column(type="integer", nullable=true)
   * @var int 
   */
  protected $count_report;
  
  /**
   * array json des id users ayant reporté l'élément
   * 
   * @ORM\Column(type="text", nullable=true)
   * @var string 
   */
  protected $report_ids;
  
  /**
   * array json des id users ayant voté +1
   * 
   * @ORM\Column(type="text", nullable=true)
   * @var string 
   */
  protected $vote_good_ids;
  
  /**
   * Compteur de points
   * 
   * @ORM\Column(type="integer", nullable=true)
   * @var int 
   */
  protected $points;
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
    return $this->id;
  }
  /**
   * Set url
   *
   * @param string $url
   */
  public function setUrl($url)
  {
    $this->url = $url;
  }
  /**
   * Get url
   *
   * @return string 
   */
  public function getUrl()
  {
    return $this->url;
  }
  /**
   * Set name
   *
   * @param string $name
   */
  public function setName($name)
  {
    $this->name = $name;
  }
  /**
   * Get name
   *
   * @return string 
   */
  public function getName()
  {
    return $this->name;
  }
  
  /**
   * Set type
   *
   * @param string $type
   */
  public function setType($type)
  {
    $this->type = $type;
  }
  /**
   * Get type
   *
   * @return string 
   */
  public function getType()
  {
    return $this->type;
  }
  
  
  public function __construct($url = null)
  {
    //$this->tags = new ArrayCollection();
    $this->url = $url;
  }
  
  public function __toString()
  {
      return $this->name;
  }
  
  /**
   * Add tags
   *
   * @param Tag $tags
   */
  public function addTag(Tag $tags)
  {
    $this->tags[] = $tags;
  }
  /**
   * Get tags
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getTags()
  {
    return $this->tags;
  }
  
  public function getTagsIdsJson()
  {
    $ids = array();
    if (count($this->getTags()))
    {
      foreach ($this->getTags() as $tag)
      {
        $ids[] = $tag->getId();
      }
    }
    return json_encode($ids);
  }
  
  public function setTags($tags)
  {
    $this->tags = $tags;
  }
  /**
   * Set owner
   *
   * @param User $owner
   */
  public function setOwner(User $owner)
  {
      $this->owner = $owner;
  }
  /**
   * Get owner
   *
   * @return User 
   */
  public function getOwner()
  {
      return $this->owner;
  }
  /**
   * Add elements_favorites
   *
   * @param UsersElementsFavorites $elementsFavorites
   */
  public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  {
      $this->elements_favorites[] = $elementsFavorites;
  }
  /**
   * Get elements_favorites
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getElementsFavorites()
  {
      return $this->elements_favorites;
  }
  /**
   * Set group
   *
   * @param Group $group
   */
  public function setGroup($group)
  {
      $this->group = $group;
  }
  /**
   * Get group
   *
   * @return Group 
   */
  public function getGroup()
  {
      return $this->group;
  }
  /**
   * Set embed
   *
   * @param string $code
   */
  public function setEmbed($code)
  {
      $this->embed = $code;
  }
  /**
   * Get embed
   *
   * @return string 
   */
  public function getEmbed()
  {
      return $this->embed;
  }
  /**
   * Set created
   *
   * @param date $created
   */
  public function setCreated($created)
  {
      $this->created = $created;
  }
  /**
   * Get created
   *
   * @return date 
   */
  public function getCreated()
  {
      return $this->created;
  }
  /**
   * Set updated
   *
   * @param datetime $updated
   */
  public function setUpdated($updated)
  {
      $this->updated = $updated;
  }
  /**
   * Get updated
   *
   * @return datetime 
   */
  public function getUpdated()
  {
      return $this->updated;
  }
  /**
   * Set thumbnail url
   *
   * @param string $thumbnail_url
   */
  public function setThumbnailUrl($thumbnail_url)
  {
      $this->thumbnail_url = $thumbnail_url;
  }
  /**
   * Get thumbnail url
   *
   * @return datetime 
   */
  public function getThumbnailUrl()
  {
      return $this->thumbnail_url;
  }
  
  /**
   *
   * @return type array
   */
  public function getComments()
  {
    return json_decode($this->comments, true);
  }
  
  public function getCountReport()
  {
    return $this->count_report;
  }
  
  public function setCountReport($count)
  {
    $this->count_report = $count;
  }
  
  public function getReportIds()
  {
    return json_decode($this->report_ids, true);
  }
  
  public function setReportIds($report_ids)
  {
    $this->report_ids = json_encode($report_ids);
  }
  
  /**
   *
   * @param array $comments 
   */
  public function setComments($comments)
  {
    $this->comments = json_encode($comments);
  }
  
  /**
   * Etablie des relation vers des tags.
   * (Supprime les anciens tags, au niveau de l'objet seulement)
   * 
   * @param array $ids 
   */
  public function setTagsWithIds(EntityManager $em, $ids)
  {
    $this->tags = null;
    if (count($ids))
    {
      $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
      // Pour les nouveaux ids restants
      foreach ($tags as $tag)
      {      
        $this->addTag($tag);
      }
    }
  }
  
  public function getCountFavorite()
  {
    return count($this->elements_favorites);
  }
  
  public function setGroupToId()
  {
    $this->group = $this->group->getId();
  }
  
//  public function deleteTag(Tag $tag)
//  {
//    $this->tags->removeElement($tag);
//  }
  
  public function hasTag(Tag $tag_t)
  {
    foreach ($this->getTags() as $tag)
    {
      if ($tag_t->getId() == $tag->getId())
      {
        return true;
      }
    }
    return false;
  }
  
  public function getPoints()
  {
    if ($this->points === null)
    {
      return '0';
    }
    
    return $this->points;
  }
  
  public function setPoints($points)
  {
    $this->points = $points;
  }
  
  public function getVoteGoodIds()
  {
    return json_decode($this->vote_good_ids, true);
  }
  
  public function setVoteGoodIds($votes_ids)
  {
    $this->vote_good_ids = json_encode($votes_ids);
  }
  
  public function addVoteGood($user_id)
  {
    $votes = $this->getVoteGoodIds();
    if (!count($votes))
    {
      $votes = array();
    }
    
    if (!$this->hasVoteGood($user_id))
    {
      $votes[] = (string)$user_id;
    }
    $this->setPoints($this->getPoints()+1);
    $this->setVoteGoodIds($votes);
  }
  
  public function removeVoteGood($user_id)
  {
    if (count($votes = $this->getVoteGoodIds()))
    {
      $votes_n = array();
      foreach ($votes as $id)
      {
        if ($id != $user_id)
        {
          $votes_n[] = (string)$id;
        }
      }
      
      $this->setPoints($this->getPoints()-1);
      $this->setVoteGoodIds($votes_n);
    }
  }
  
  public function hasVoteGood($user_id)
  {
    if (count($votes = $this->getVoteGoodIds()))
    {
      foreach ($votes as $id)
      {
        if ($id == $user_id)
        {
          return true;
        }
      }
    }
    return false;
  }
  
}
 |