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

@ -377,13 +377,10 @@ function photos_post(App $a)
if (!empty($_POST['rotate']) && (intval($_POST['rotate']) == 1 || intval($_POST['rotate']) == 2)) { if (!empty($_POST['rotate']) && (intval($_POST['rotate']) == 1 || intval($_POST['rotate']) == 2)) {
Logger::log('rotate'); Logger::log('rotate');
$r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0 LIMIT 1", $photo = Photo::getPhotoForUser($page_owner_uid, $resource_id);
DBA::escape($resource_id),
intval($page_owner_uid)
);
if (DBA::isResult($r)) { if (DBA::isResult($photo)) {
$image = new Image($r[0]['data'], $r[0]['type']); $image = Photo::getImageForPhoto($photo);
if ($image->isValid()) { if ($image->isValid()) {
$rotate_deg = ((intval($_POST['rotate']) == 1) ? 270 : 90); $rotate_deg = ((intval($_POST['rotate']) == 1) ? 270 : 90);
@ -392,26 +389,14 @@ function photos_post(App $a)
$width = $image->getWidth(); $width = $image->getWidth();
$height = $image->getHeight(); $height = $image->getHeight();
$x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0", Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 0], $image);
DBA::escape($image->asString()),
intval($height),
intval($width),
DBA::escape($resource_id),
intval($page_owner_uid)
);
if ($width > 640 || $height > 640) { if ($width > 640 || $height > 640) {
$image->scaleDown(640); $image->scaleDown(640);
$width = $image->getWidth(); $width = $image->getWidth();
$height = $image->getHeight(); $height = $image->getHeight();
$x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 1", Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 1], $image);
DBA::escape($image->asString()),
intval($height),
intval($width),
DBA::escape($resource_id),
intval($page_owner_uid)
);
} }
if ($width > 320 || $height > 320) { if ($width > 320 || $height > 320) {
@ -419,34 +404,19 @@ function photos_post(App $a)
$width = $image->getWidth(); $width = $image->getWidth();
$height = $image->getHeight(); $height = $image->getHeight();
$x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 2", Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 2], $image);
DBA::escape($image->asString()),
intval($height),
intval($width),
DBA::escape($resource_id),
intval($page_owner_uid)
);
} }
} }
} }
} }
$p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC", $photo = Photo::getPhotoForUser($page_owner_uid, $resource_id);
DBA::escape($resource_id),
intval($page_owner_uid)
);
if (DBA::isResult($p)) { if (DBA::isResult($photo)) {
$ext = $phototypes[$p[0]['type']]; $ext = $phototypes[$photo['type']];
$r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d", Photo::update(
DBA::escape($desc), ['desc' => $desc, 'album' => $albname, 'allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow, 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny],
DBA::escape($albname), ['resource-id' => $resource_id, 'uid' => $page_owner_uid]
DBA::escape($str_contact_allow),
DBA::escape($str_group_allow),
DBA::escape($str_contact_deny),
DBA::escape($str_group_deny),
DBA::escape($resource_id),
intval($page_owner_uid)
); );
// Update the photo albums cache if album name was changed // Update the photo albums cache if album name was changed
@ -458,7 +428,7 @@ function photos_post(App $a)
/* Don't make the item visible if the only change was the album name */ /* Don't make the item visible if the only change was the album name */
$visibility = 0; $visibility = 0;
if ($p[0]['desc'] !== $desc || strlen($rawtags)) { if ($photo['desc'] !== $desc || strlen($rawtags)) {
$visibility = 1; $visibility = 1;
} }
@ -474,7 +444,7 @@ function photos_post(App $a)
$arr['parent-uri'] = $uri; $arr['parent-uri'] = $uri;
$arr['post-type'] = Item::PT_IMAGE; $arr['post-type'] = Item::PT_IMAGE;
$arr['wall'] = 1; $arr['wall'] = 1;
$arr['resource-id'] = $p[0]['resource-id']; $arr['resource-id'] = $photo['resource-id'];
$arr['contact-id'] = $owner_record['id']; $arr['contact-id'] = $owner_record['id'];
$arr['owner-name'] = $owner_record['name']; $arr['owner-name'] = $owner_record['name'];
$arr['owner-link'] = $owner_record['url']; $arr['owner-link'] = $owner_record['url'];
@ -483,15 +453,15 @@ function photos_post(App $a)
$arr['author-link'] = $owner_record['url']; $arr['author-link'] = $owner_record['url'];
$arr['author-avatar'] = $owner_record['thumb']; $arr['author-avatar'] = $owner_record['thumb'];
$arr['title'] = $title; $arr['title'] = $title;
$arr['allow_cid'] = $p[0]['allow_cid']; $arr['allow_cid'] = $photo['allow_cid'];
$arr['allow_gid'] = $p[0]['allow_gid']; $arr['allow_gid'] = $photo['allow_gid'];
$arr['deny_cid'] = $p[0]['deny_cid']; $arr['deny_cid'] = $photo['deny_cid'];
$arr['deny_gid'] = $p[0]['deny_gid']; $arr['deny_gid'] = $photo['deny_gid'];
$arr['visible'] = $visibility; $arr['visible'] = $visibility;
$arr['origin'] = 1; $arr['origin'] = 1;
$arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' $arr['body'] = '[url=' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $photo['resource-id'] . ']'
. '[img]' . System::baseUrl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]' . '[img]' . System::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $photo['scale'] . '.'. $ext . '[/img]'
. '[/url]'; . '[/url]';
$item_id = Item::insert($arr); $item_id = Item::insert($arr);
@ -660,10 +630,10 @@ function photos_post(App $a)
$arr['author-link'] = $owner_record['url']; $arr['author-link'] = $owner_record['url'];
$arr['author-avatar'] = $owner_record['thumb']; $arr['author-avatar'] = $owner_record['thumb'];
$arr['title'] = ''; $arr['title'] = '';
$arr['allow_cid'] = $p[0]['allow_cid']; $arr['allow_cid'] = $photo['allow_cid'];
$arr['allow_gid'] = $p[0]['allow_gid']; $arr['allow_gid'] = $photo['allow_gid'];
$arr['deny_cid'] = $p[0]['deny_cid']; $arr['deny_cid'] = $photo['deny_cid'];
$arr['deny_gid'] = $p[0]['deny_gid']; $arr['deny_gid'] = $photo['deny_gid'];
$arr['visible'] = 1; $arr['visible'] = 1;
$arr['verb'] = ACTIVITY_TAG; $arr['verb'] = ACTIVITY_TAG;
$arr['gravity'] = GRAVITY_PARENT; $arr['gravity'] = GRAVITY_PARENT;
@ -672,19 +642,19 @@ function photos_post(App $a)
$arr['tag'] = $tagged[4]; $arr['tag'] = $tagged[4];
$arr['inform'] = $tagged[2]; $arr['inform'] = $tagged[2];
$arr['origin'] = 1; $arr['origin'] = 1;
$arr['body'] = L10n::t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . L10n::t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]'); $arr['body'] = L10n::t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . ']' . L10n::t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ;
$arr['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n"; $arr['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . ']' . '[img]' . System::baseUrl() . "/photo/" . $photo['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ;
$arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>'; $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
$arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n"); $arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
if ($tagged[3]) { if ($tagged[3]) {
$arr['object'] .= XML::escape('<link rel="photo" type="'.$p[0]['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n"); $arr['object'] .= XML::escape('<link rel="photo" type="'.$photo['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n");
} }
$arr['object'] .= '</link></object>' . "\n"; $arr['object'] .= '</link></object>' . "\n";
$arr['target'] = '<target><type>' . ACTIVITY_OBJ_IMAGE . '</type><title>' . $p[0]['desc'] . '</title><id>' $arr['target'] = '<target><type>' . ACTIVITY_OBJ_IMAGE . '</type><title>' . $photo['desc'] . '</title><id>'
. System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>'; . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . '</id>';
$arr['target'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>'; $arr['target'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$photo['type'].'" href="' . System::baseUrl() . "/photo/" . $photo['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
$item_id = Item::insert($arr); $item_id = Item::insert($arr);
} }
@ -720,10 +690,7 @@ function photos_post(App $a)
* they acquire comments, likes, dislikes, and/or tags * they acquire comments, likes, dislikes, and/or tags
*/ */
$r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ", $r = Photo::select([], ["`album` = ? AND `uid` = ? AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR", $album, $page_owner_uid]);
DBA::escape($album),
intval($page_owner_uid)
);
if (!DBA::isResult($r) || ($album == L10n::t('Profile Photos'))) { if (!DBA::isResult($r) || ($album == L10n::t('Profile Photos'))) {
$visible = 1; $visible = 1;

View file

@ -66,6 +66,48 @@ class Photo extends BaseObject
return DBA::selectFirst("photo", $fields, $conditions, $params); 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 * @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, * on success, "no sign" image info, if user has no permission,
* false if photo does not exists * 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 * @param integer $scale Scale of the photo. Defaults to 0
* *
* @return boolean|array * @return boolean|array
@ -275,6 +317,37 @@ class Photo extends BaseObject
return DBA::delete("photo", $conditions, $options); 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 string $image_url Remote URL
* @param integer $uid user id * @param integer $uid user id