Example #1
0
 /**
  * Creates a new tag with the given name.
  *
  * @param string $tag The name of the tag.
  * @return array A status array, where the 'status' index is the
  *               status code of the operation, and the 'response' index
  *               is the tag's id in the database on STATUS_OK or an error string otherwise.
  */
 public static function create($tag)
 {
     return Database::INSERT_INTO(TAGS_TABLE . ' (name) VALUES (?)', array(self::sanitize_tag_name($tag)));
 }
Example #2
0
 /**
  * Uploads a revision of an existing media file to the server.
  *
  * @param int $id The id of the media file this revision is for.
  * @param array $file The uploaded file from the $_FILES array.
  * @param string $title The media's title.
  * @param string $description The media's description.
  * @param string $tags A comma separated string of tags.
  * @return array A status array, where the 'status' index is the status code of the operation, and the 'response' index is the uploaded revision's ID on STATUS_OK or an error string otherwise.
  */
 public static function upload_revision($id, $file, $title, $description, $tags)
 {
     // Ensure that nothing was wrong with the file upload
     $result = self::check_file_status($file);
     if ($result['status'] != STATUS_OK) {
         return $result;
     }
     // Get the original media
     $original = new Media($id);
     // Move the original media to a revision file
     $rev_name = $original->filename . '_r' . $original->get_revision_count();
     $original->move_file("{$rev_name}.{$original->extension}");
     // Collect and sanitize information about the media
     $result = self::collect_file_data($file, $title, $description, $filename, $extension, $tags);
     if ($result['status'] != STATUS_OK) {
         // Move the original file back
         $original->move_file("{$filename}.{$extension}");
         return $result;
     }
     // Move the media file onto the server (true to allow duplicates)
     $result = self::move_to_server($file, $filename, $extension, $histograms, true);
     if ($result['status'] != STATUS_OK) {
         // Move the original file back
         $original->move_file("{$filename}.{$extension}");
         return $result;
     }
     // Update the media entry in the database
     $set = 'title=?,description=?,filename=?,type=?,uploader=?,uploaded=NOW(),histograms=?';
     $where = 'id=?';
     $values = array($title, $description, $filename, $extension, CIF_ID, $histograms, $id);
     $result = Database::UPDATE(IMAGE_TABLE . " SET {$set} WHERE {$where}", $values);
     if ($result['status'] == STATUS_OK) {
         // Update its tag associations on success
         self::update_tags(new Media($id), $tags);
         // Add the original media to the revisions table
         $cols = 'image_id,filename,type,uploader,uploaded';
         $places = '?,       ?,       ?,   ?,       ?';
         $values = array($original->id, $original->filename, $original->extension, $original->uploader, $original->upload_date);
         $result = Database::INSERT_INTO(REVISIONS_TABLE . " ({$cols}) VALUES ({$places})", $values);
     }
     return $result;
 }
Example #3
0
 /**
  * Associates the given tag name with the media.
  *
  * @param string $tag The name of the tag.
  */
 public function add_tag($tag)
 {
     // Query for the tag's id
     $result = Database::SELECT('id FROM ' . TAGS_TABLE . ' WHERE name=? LIMIT 1', array(Tag::sanitize_tag_name($tag)));
     // If the query was successful, associate the tag with the media
     if ($result['status'] == STATUS_OK) {
         Database::INSERT_INTO(TAG_RELATION_TABLE . ' (image_id, tag_id) VALUES (?, ?)', array($this->id, $result['response'][0]['id']));
     }
 }