1
0
This repository has been archived on 2021-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
gallery3-contrib/modules/photoannotation/helpers/photoannotation.php

171 lines
7.2 KiB
PHP
Raw Normal View History

<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class photoannotation_Core {
public static function saveuser($user_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description) {
//Since we are associating a user we will remove any old annotation of this user on this photo
$item_old_users = ORM::factory("items_user")
->where("user_id", "=", $user_id)
->where("item_id", "=", $item_id)
->find_all();
if (count($item_old_users) > 1) {
foreach ($item_old_users as $item_old_user) {
$item_old_user->delete();
}
$item_user = ORM::factory("items_user");
} elseif (count($item_old_users) == 1) {
$item_user = ORM::factory("items_user", $item_old_users[0]->id);
} else {
$item_user = ORM::factory("items_user");
photoannotation::send_notifications($user_id, $item_id, "newtag");
}
$item_user->user_id = $user_id;
$item_user->item_id = $item_id;
$item_user->x1 = $str_x1;
$item_user->y1 = $str_y1;
$item_user->x2 = $str_x2;
$item_user->y2 = $str_y2;
$item_user->description = $description;
$item_user->save();
}
public static function saveface($tag_id, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id = "") {
if ($annotate_id == "") {
$item_face = ORM::factory("items_face");
} else {
$item_face = ORM::factory("items_face")
->where("id", "=", $annotate_id)
->find();
}
$item_face->tag_id = $tag_id;
$item_face->item_id = $item_id;
$item_face->x1 = $str_x1;
$item_face->y1 = $str_y1;
$item_face->x2 = $str_x2;
$item_face->y2 = $str_y2;
$item_face->description = $description;
$item_face->save();
}
public static function savenote($item_title, $item_id, $str_x1, $str_y1, $str_x2, $str_y2, $description, $annotate_id = "") {
if ($annotate_id == "") {
$item_note = ORM::factory("items_note");
} else {
$item_note = ORM::factory("items_note")
->where("id", "=", $annotate_id)
->find();
}
$item_note->item_id = $item_id;
$item_note->x1 = $str_x1;
$item_note->y1 = $str_y1;
$item_note->x2 = $str_x2;
$item_note->y2 = $str_y2;
$item_note->title = $item_title;
$item_note->description = $description;
$item_note->save();
}
public static function send_notifications($recipient_id, $item_id, $mailtype) {
//Load the item
$item = ORM::factory("item")->where("id", "=", $item_id)->find();
if (!$item->loaded()) {
return false;
}
//Load the user
$recipient = ORM::factory("user")->where("id", "=", $recipient_id)->find();
if (!$recipient->loaded()) {
return false;
}
//Only send mail if the notifications are switched on globally
if (!module::get_var("photoannotation", "nonotifications", false)) {
//Get the users settings
$notification_settings = self::get_user_notification_settings($recipient);
//Check which type of mail to send
switch ($mailtype) {
case "newtag":
//Only send if user has this option enabled
if ($notification_settings->newtag) {
//Get subject and body and send the mail
$subject = module::get_var("photoannotation", "newtagsubject", "Someone tagged a photo of you");
$body = module::get_var("photoannotation", "newtagbody", "Hello %name, please visit %url to view the photo.");
$body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body);
return self::_send_mail($recipient->email, $subject, $body);
}
break;
case "newcomment":
//Only send if user has this option enabled
if ($notification_settings->comment) {
//Get subject and body and send the mail
$subject = module::get_var("photoannotation", "newcommentsubject", "Someone added a comment to photo of you");
$body = module::get_var("photoannotation", "newcommentbody", "Hello %name, please visit %url to read the comment.");
$body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body);
return self::_send_mail($recipient->email, $subject, $body);
}
break;
case "updatecomment":
//Only send if user has this option enabled
if ($notification_settings->comment) {
//Get subject and body and send the mail
$subject = module::get_var("photoannotation", "updatedcommentsubject", "Someone updated a comment to photo of you");
$body = module::get_var("photoannotation", "updatedcommentbody", "Hello %name, please visit %url to read the comment.");
$body = str_ireplace(array("%url", "%name"), array($item->abs_url(), $recipient->display_name()), $body);
return self::_send_mail($recipient->email, $subject, $body);
}
}
}
return false;
}
private static function _send_mail($mailto, $subject, $message) {
//Send the notification mail
return Sendmail::factory()
->to($mailto)
->subject($subject)
->header("Mime-Version", "1.0")
->header("Content-type", "text/html; charset=utf-8")
->message($message)
->send();
}
public static function get_user_notification_settings($user) {
//Try loading the notification settings of user
$notification_settings = ORM::factory("photoannotation_notification")->where("user_id", "=", $user->id)->find();
if (!$notification_settings->loaded()) {
//If the user did not save his settings use the website default
$notify = module::get_var("photoannotation", "notificationoptout", false);
$notification_settings = ORM::factory("photoannotation_notification");
$notification_settings->user_id = $user->id;
$notification_settings->newtag = $notify;
$notification_settings->comment = $notify;
$notification_settings->save();
}
return $notification_settings;
}
public static function get_user_cloud() {
$users = ORM::factory("user")->order_by("name", "ASC")->find_all();
foreach ($users as $user) {
$items_users_count = ORM::factory("items_user")->where("user_id", "=", $user->id)->count_all();
if ($items_users_count > 0) {
$user_array[] = $user->display_name();
}
}
return $user_array;
}
}