Ejemplo n.º 1
0
 /**
  * Insert new image into database.
  *
  * @param $sha1
  * @param $fileSize
  * @param $imageDescription
  * @param $tag
  * @param $location
  * @return Image
  */
 public function insert($sha1, $fileSize, $imageDescription, $tag, $location)
 {
     $image = new Image();
     $image->setHash($sha1);
     $image->setFileSize($fileSize);
     $image->setDescription($imageDescription);
     $dateCreated = gmdate("Y-m-d H:i:s");
     $image->setDateCreated($dateCreated);
     $query = $this->db->prepare("\n            INSERT INTO images (description, file_size, sha1_hash, date_created, tag_id, location_id)\n            VALUES(:description, :file_size, :sha1_hash, :date_created, :tag_id, :location_id)\n        ");
     $query->bindValue(":description", $imageDescription);
     $query->bindValue(":file_size", $fileSize);
     $query->bindValue(":sha1_hash", $sha1);
     $query->bindValue(":date_created", $dateCreated);
     if ($tag != null) {
         $tagModel = new TagModel($this->db);
         $myTag = $tagModel->getTagByValue($tag->getValue());
         if ($myTag == null) {
             $myTag = $tagModel->insert($tag->getValue());
         }
         $query->bindValue(":tag_id", $myTag->getId());
         $image->setTag($myTag);
     } else {
         $query->bindValue(":tag_id", 0);
     }
     if ($location != null) {
         $locationModel = new LocationModel($this->db);
         $myLocation = $locationModel->getLocationByValue($location->getLat(), $location->getLon());
         if ($myLocation == null) {
             $myLocation = $locationModel->insert($location->getLat(), $location->getLon());
         }
         $query->bindValue(":location_id", $myLocation->getId());
         $image->setLocation($myLocation);
     } else {
         $query->bindValue(":location_id", 0);
     }
     $query->execute();
     $image->setId($this->db->lastInsertId());
     return $image;
 }