Add update() to Photo model

Edit mod/photos.php to use Photo model
This commit is contained in:
fabrixxm 2018-11-21 17:55:16 +01:00 committed by Hypolite Petovan
parent 06157c2ab0
commit 3e13799e70
2 changed files with 105 additions and 65 deletions

View file

@ -66,6 +66,48 @@ class Photo extends BaseObject
return DBA::selectFirst("photo", $fields, $conditions, $params);
}
/**
* @brief Get a photo for user id
*
* @param integer $uid User id
* @param string $resourceid Rescource ID of the photo
* @param array $conditions Array of fields for conditions
* @param array $params Array of several parameters
*
* @return bool|array
*
* @see \Friendica\Database\DBA::select
*/
public static function getPhotosForUser($uid, $resourceid, array $conditions = [], array $params = [])
{
$conditions["resource-id"] = $resourceid;
$conditions["uid"] = $uid;
return self::select([], $conditions, $params);
}
/**
* @brief Get a photo for user id
*
* @param integer $uid User id
* @param string $resourceid Rescource ID of the photo
* @param integer $scale Scale of the photo. Defaults to 0
* @param array $conditions Array of fields for conditions
* @param array $params Array of several parameters
*
* @return bool|array
*
* @see \Friendica\Database\DBA::select
*/
public static function getPhotoForUser($uid, $resourceid, $scale = 0, array $conditions = [], array $params = [])
{
$conditions["resource-id"] = $resourceid;
$conditions["uid"] = $uid;
$conditions["scale"] = $scale;
return self::selectFirst([], $conditions, $params);
}
/**
* @brief Get a single photo given resource id and scale
*
@ -73,7 +115,7 @@ class Photo extends BaseObject
* on success, "no sign" image info, if user has no permission,
* false if photo does not exists
*
* @param string $resourceid Rescource ID for the photo
* @param string $resourceid Rescource ID of the photo
* @param integer $scale Scale of the photo. Defaults to 0
*
* @return boolean|array
@ -275,6 +317,37 @@ class Photo extends BaseObject
return DBA::delete("photo", $conditions, $options);
}
/**
* @brief Update a photo
*
* @param array $fields Contains the fields that are updated
* @param array $conditions Condition array with the key values
* @param Image $img Image to update. Optional, default null.
* @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
*
* @return boolean Was the update successfull?
*
* @see \Friendica\Database\DBA::update
*/
public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
{
if (!is_null($img)) {
// get photo to update
$photos = self::select(["backend-class","backend-ref"], $conditions);
foreach($photos as $photo) {
$backend_class = (string)$photo["backend-class"];
if ($backend_class !== "") {
$fields["backend-ref"] = $backend_class::put($img->asString(), $photo["backend-ref"]);
} else {
$fields["data"] = $img->asString();
}
}
}
return DBA::update("photo", $fields, $conditions);
}
/**
* @param string $image_url Remote URL
* @param integer $uid user id