freenetis-github/application/models/users_contacts.php @ a33fa7cc
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is release under GPLv3 licence.
|
|||
*
|
|||
* More info about licence can be found:
|
|||
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
*
|
|||
* More info about project can be found:
|
|||
* http://www.freenetis.org/
|
|||
*
|
|||
*/
|
|||
/**
|
|||
* Pivot table for connecting users and theirs contacts, also containts
|
|||
* whitelisted column for defining whitelist of contact in notification.
|
|||
*
|
|||
* @package Model
|
|||
*/
|
|||
class Users_contacts_Model extends Model
|
|||
{
|
|||
/**
|
|||
c1bdc1c4 | Michal Kliment | * Returns all contacts of members by type
|
|
8baed187 | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @author Michal Kliment, Ondrej Fibich
|
|
* @param integer|array $member_id Member ID or array of member IDs
|
|||
8baed187 | Michal Kliment | * @param integer $type
|
|
c1bdc1c4 | Michal Kliment | * @param boolean $ignore_whitelisted
|
|
* @param boolean $notify_former_members Can be former members notified?
|
|||
* @param boolean $notify_interrupted_members Can be interrupted members notified?
|
|||
* @param boolean $ignore_member_notif_settings Should be member notification setting ignored?
|
|||
8baed187 | Michal Kliment | * @return Mysql_Result
|
|
*/
|
|||
c1bdc1c4 | Michal Kliment | public function get_contacts_by_member_and_type (
|
|
$member_id, $type, $ignore_whitelisted = FALSE,
|
|||
$notify_former_members = FALSE, $notify_interrupted_members = FALSE,
|
|||
$ignore_member_notif_settings = FALSE)
|
|||
8baed187 | Michal Kliment | {
|
|
c1bdc1c4 | Michal Kliment | /* where */
|
|
if (!is_array($member_id))
|
|||
{
|
|||
$where = 'm.id = ' . intval($member_id);
|
|||
}
|
|||
else if (count($member_id))
|
|||
{
|
|||
$where = 'm.id IN (' . implode(',', array_map('intval', $member_id)) . ')';
|
|||
}
|
|||
else // empty (non sense condition)
|
|||
{
|
|||
$where = '1 = 2';
|
|||
}
|
|||
/* whitelist */
|
|||
$whitelisted = '';
|
|||
if (!$ignore_whitelisted)
|
|||
{
|
|||
$whitelisted = " AND m.id NOT IN
|
|||
(
|
|||
SELECT mw.member_id
|
|||
FROM members_whitelists mw
|
|||
WHERE mw.member_id = " . intval($member_id) . "
|
|||
AND mw.since <= CURDATE()
|
|||
AND mw.until >= CURDATE()
|
|||
)";
|
|||
}
|
|||
/* member whitelist - member notification settings */
|
|||
$member_whitelisted = '';
|
|||
if (!$ignore_member_notif_settings)
|
|||
{
|
|||
switch ($type)
|
|||
{
|
|||
case Contact_Model::TYPE_EMAIL:
|
|||
$member_whitelisted = " AND m.notification_by_email > 0 ";
|
|||
break;
|
|||
case Contact_Model::TYPE_PHONE:
|
|||
$member_whitelisted = " AND m.notification_by_sms > 0 ";
|
|||
break;
|
|||
}
|
|||
}
|
|||
/* former members */
|
|||
$former = '';
|
|||
if (!$notify_former_members)
|
|||
{
|
|||
$former = ' AND m.type <> ' . intval(Member_Model::TYPE_FORMER);
|
|||
}
|
|||
/* interrupted members */
|
|||
$interrupted = '';
|
|||
if (!$notify_interrupted_members)
|
|||
{
|
|||
$interrupted = " AND m.id NOT IN
|
|||
(
|
|||
SELECT mi.member_id
|
|||
FROM membership_interrupts mi
|
|||
JOIN members_fees mf ON mi.members_fee_id = mf.id
|
|||
WHERE mi.member_id = " . intval($member_id) . "
|
|||
AND mf.activation_date <= CURDATE()
|
|||
AND mf.deactivation_date >= CURDATE()
|
|||
) ";
|
|||
}
|
|||
/* query */
|
|||
8baed187 | Michal Kliment | return $this->db->query("
|
|
c1bdc1c4 | Michal Kliment | SELECT c.value, a.balance, m.id AS member_id, m.name AS member_name,
|
|
8baed187 | Michal Kliment | (
|
|
SELECT GROUP_CONCAT(vs.variable_symbol) AS variable_symbol
|
|||
FROM variable_symbols vs
|
|||
WHERE vs.account_id = a.id
|
|||
) AS variable_symbol, u.login, cou.country_code
|
|||
FROM members m
|
|||
c1bdc1c4 | Michal Kliment | JOIN accounts a ON a.member_id = m.id
|
|
8baed187 | Michal Kliment | JOIN users u ON u.member_id = m.id
|
|
JOIN users_contacts uc ON uc.user_id = u.id
|
|||
JOIN contacts c ON uc.contact_id = c.id AND c.type = ?
|
|||
LEFT JOIN contacts_countries cc ON cc.contact_id = c.id
|
|||
LEFT JOIN countries cou ON cou.id = cc.country_id
|
|||
c1bdc1c4 | Michal Kliment | WHERE $where $former $whitelisted $member_whitelisted $interrupted
|
|
GROUP BY c.id
|
|||
", $type);
|
|||
8baed187 | Michal Kliment | }
|
|
/**
|
|||
c1bdc1c4 | Michal Kliment | * Returns all contacts of users by type
|
|
8baed187 | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @author Michal Kliment, Ondrej Fibich
|
|
* @param integer|array $user_id User ID or array of user IDs
|
|||
8baed187 | Michal Kliment | * @param integer $type
|
|
c1bdc1c4 | Michal Kliment | * @param boolean $ignore_whitelisted
|
|
* @param boolean $notify_former_members Can be former members notified?
|
|||
* @param boolean $notify_interrupted_members Can be interrupted members notified?
|
|||
* @param boolean $ignore_member_notif_settings Should be member notification setting ignored?
|
|||
8baed187 | Michal Kliment | * @return Mysql_Result
|
|
*/
|
|||
c1bdc1c4 | Michal Kliment | public function get_contacts_by_user_and_type (
|
|
$user_id, $type, $ignore_whitelisted = FALSE,
|
|||
$notify_former_members = FALSE, $notify_interrupted_members = FALSE,
|
|||
$ignore_member_notif_settings = FALSE)
|
|||
8baed187 | Michal Kliment | {
|
|
c1bdc1c4 | Michal Kliment | /* where */
|
|
if (!is_array($user_id))
|
|||
{
|
|||
$where = 'u.id = ' . intval($user_id);
|
|||
}
|
|||
else if (count($user_id))
|
|||
{
|
|||
$where = 'u.id IN (' . implode(',', array_map('intval', $user_id)) . ')';
|
|||
}
|
|||
else // empty (non sense condition)
|
|||
{
|
|||
$where = '1 = 2';
|
|||
}
|
|||
/* whitelist */
|
|||
$whitelisted = '';
|
|||
8baed187 | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | if (!$ignore_whitelisted)
|
|
{
|
|||
$whitelisted = " AND m.id NOT IN
|
|||
(
|
|||
SELECT mw.member_id
|
|||
FROM members_whitelists mw
|
|||
WHERE mw.member_id = " . intval($user_id) . "
|
|||
AND mw.since <= CURDATE()
|
|||
AND mw.until >= CURDATE()
|
|||
)";
|
|||
}
|
|||
/* member whitelist - member notification settings */
|
|||
$member_whitelisted = '';
|
|||
if (!$ignore_member_notif_settings)
|
|||
{
|
|||
switch ($type)
|
|||
{
|
|||
case Contact_Model::TYPE_EMAIL:
|
|||
$member_whitelisted = " AND m.notification_by_email > 0 ";
|
|||
break;
|
|||
case Contact_Model::TYPE_PHONE:
|
|||
$member_whitelisted = " AND m.notification_by_sms > 0 ";
|
|||
break;
|
|||
}
|
|||
}
|
|||
/* former members */
|
|||
$former = '';
|
|||
if (!$notify_former_members)
|
|||
{
|
|||
$former = ' AND m.type <> ' . intval(Member_Model::TYPE_FORMER);
|
|||
}
|
|||
/* interrupted members */
|
|||
$interrupted = '';
|
|||
if (!$notify_interrupted_members)
|
|||
{
|
|||
$interrupted = " AND m.id NOT IN
|
|||
(
|
|||
SELECT mi.member_id
|
|||
FROM membership_interrupts mi
|
|||
JOIN members_fees mf ON mi.members_fee_id = mf.id
|
|||
WHERE mi.member_id = " . intval($user_id) . "
|
|||
AND mf.activation_date <= CURDATE()
|
|||
AND mf.deactivation_date >= CURDATE()
|
|||
) ";
|
|||
}
|
|||
/* query */
|
|||
8baed187 | Michal Kliment | return $this->db->query("
|
|
SELECT c.value, a.balance, m.id AS member_id, m.name AS member_name,
|
|||
(
|
|||
c1bdc1c4 | Michal Kliment | SELECT GROUP_CONCAT(vs.variable_symbol) AS variable_symbol
|
|
FROM variable_symbols vs
|
|||
WHERE vs.account_id = a.id
|
|||
8baed187 | Michal Kliment | ) AS variable_symbol, u.login, cou.country_code
|
|
FROM members m
|
|||
JOIN accounts a ON a.member_id = m.id
|
|||
JOIN users u ON u.member_id = m.id
|
|||
c1bdc1c4 | Michal Kliment | JOIN users_contacts uc ON uc.user_id = u.id
|
|
8baed187 | Michal Kliment | JOIN contacts c ON uc.contact_id = c.id AND c.type = ?
|
|
LEFT JOIN contacts_countries cc ON cc.contact_id = c.id
|
|||
LEFT JOIN countries cou ON cou.id = cc.country_id
|
|||
c1bdc1c4 | Michal Kliment | WHERE $where $former $whitelisted $member_whitelisted $interrupted
|
|
8baed187 | Michal Kliment | GROUP BY c.id
|
|
c1bdc1c4 | Michal Kliment | ", $type);
|
|
8baed187 | Michal Kliment | }
|
|
/**
|
|||
* Returns all contacts by given type
|
|||
*
|
|||
* @author Michal Kliment
|
|||
* @param integer $type
|
|||
* @param bool $ignore_whitelisted
|
|||
* @return Mysql_Result
|
|||
*/
|
|||
public function get_all_contacts_by_type ($type, $ignore_whitelisted = FALSE)
|
|||
{
|
|||
c1bdc1c4 | Michal Kliment | $whitelisted = '';
|
|
if (!$ignore_whitelisted)
|
|||
{
|
|||
$whitelisted = "AND m.id NOT IN
|
|||
(
|
|||
SELECT mw.member_id
|
|||
FROM members_whitelists mw
|
|||
WHERE mw.since <= CURDATE() AND mw.until >= CURDATE()
|
|||
)";
|
|||
}
|
|||
8baed187 | Michal Kliment | ||
return $this->db->query("
|
|||
SELECT c.value, a.balance, m.id AS member_id, m.name AS member_name,
|
|||
(
|
|||
c1bdc1c4 | Michal Kliment | SELECT GROUP_CONCAT(vs.variable_symbol) AS variable_symbol
|
|
FROM variable_symbols vs
|
|||
WHERE vs.account_id = a.id
|
|||
8baed187 | Michal Kliment | ) AS variable_symbol, u.login, cou.country_code
|
|
FROM contacts c
|
|||
c1bdc1c4 | Michal Kliment | JOIN users_contacts uc ON uc.contact_id = c.id
|
|
8baed187 | Michal Kliment | JOIN users u ON uc.user_id = u.id
|
|
JOIN members m ON u.member_id = m.id
|
|||
JOIN accounts a ON a.member_id = m.id
|
|||
LEFT JOIN contacts_countries cc ON cc.contact_id = c.id
|
|||
LEFT JOIN countries cou ON cou.id = cc.country_id
|
|||
c1bdc1c4 | Michal Kliment | WHERE m.type <> ? AND c.type = ? $whitelisted AND m.id NOT IN
|
|
8baed187 | Michal Kliment | (
|
|
c1bdc1c4 | Michal Kliment | SELECT mi.member_id
|
|
FROM membership_interrupts mi
|
|||
8baed187 | Michal Kliment | JOIN members_fees mf ON mi.members_fee_id = mf.id
|
|
c1bdc1c4 | Michal Kliment | WHERE mf.activation_date <= CURDATE() AND
|
|
mf.deactivation_date >= CURDATE()
|
|||
8baed187 | Michal Kliment | )
|
|
GROUP BY c.id
|
|||
", array(Member_Model::TYPE_FORMER, $type));
|
|||
}
|
|||
c1bdc1c4 | Michal Kliment | ||
/**
|
|||
* Finds e-mail boxes of the given user on which the inner user mail may
|
|||
* be redirected.
|
|||
*
|
|||
* @param int $user_id User ID
|
|||
* @return Mysql_Result Email contacts
|
|||
*/
|
|||
public function get_redirected_email_boxes_of($user_id) {
|
|||
return $this->db->query("
|
|||
SELECT c.*
|
|||
FROM users_contacts uc
|
|||
JOIN contacts c ON c.id = uc.contact_id
|
|||
WHERE uc.user_id = ? AND c.type = ? AND uc.mail_redirection = 1
|
|||
", $user_id, Contact_Model::TYPE_EMAIL);
|
|||
}
|
|||
/**
|
|||
* Gets ID of user who owns given contact.
|
|||
*
|
|||
* @param int $contact_id
|
|||
* @return int|null User ID or null
|
|||
*/
|
|||
public function get_user_of_contact($contact_id)
|
|||
{
|
|||
$result = $this->db->query("
|
|||
SELECT user_id
|
|||
FROM users_contacts
|
|||
WHERE contact_id = ?
|
|||
", $contact_id);
|
|||
if ($result && $result->count() == 1)
|
|||
{
|
|||
return $result->current()->user_id;
|
|||
}
|
|||
return null;
|
|||
}
|
|||
8baed187 | Michal Kliment | }
|