]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription_queue.php
8626f0674b26e28e3bca7b18b3b2c5fe5425e702
[quix0rs-gnu-social.git] / classes / Subscription_queue.php
1 <?php
2 /**
3  * Table Definition for subscription_queue
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Subscription_queue extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'subscription_queue';       // table name
13     public $subscriber;
14     public $subscribed;
15     public $created;
16
17     /* Pkey get */
18     function pkeyGet($k)
19     { return Memcached_DataObject::pkeyGet('Subscription_queue',$k); }
20
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23
24     public static function schemaDef()
25     {
26         return array(
27             'description' => 'Holder for subscription requests awaiting moderation.',
28             'fields' => array(
29                 'subscriber' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
30                 'subscribed' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile being subscribed to'),
31                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
32             ),
33             'primary key' => array('subscriber', 'subscribed'),
34             'indexes' => array(
35                 'subscription_queue_subscriber_created_idx' => array('subscriber', 'created'),
36                 'subscription_queue_subscribed_created_idx' => array('subscribed', 'created'),
37             ),
38             'foreign keys' => array(
39                 'subscription_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')),
40                 'subscription_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')),
41             )
42         );
43     }
44
45     public static function saveNew(Profile $subscriber, Profile $subscribed)
46     {
47         $rq = new Subscription_queue();
48         $rq->subscriber = $subscriber->id;
49         $rq->subscribed = $subscribed->id;
50         $rq->created = common_sql_now();
51         $rq->insert();
52         return $rq;
53     }
54
55     function exists($subscriber, $other)
56     {
57         $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->id,
58                                                  'subscribed' => $other->id));
59         return (empty($sub)) ? false : true;
60     }
61
62     /**
63      * Complete a pending subscription, as we've got approval of some sort.
64      *
65      * @return Subscription
66      */
67     public function complete()
68     {
69         $subscriber = Profile::staticGet('id', $this->subscriber);
70         $subscribed = Profile::staticGet('id', $this->subscribed);
71         $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
72         if ($sub) {
73             $this->delete();
74         }
75         return $sub;
76     }
77
78     /**
79      * Cancel an outstanding subscription request to the other profile.
80      */
81     public function abort()
82     {
83         $subscriber = Profile::staticGet('id', $this->subscriber);
84         $subscribed = Profile::staticGet('id', $this->subscribed);
85         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
86             $this->delete();
87             Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
88         }
89     }
90
91     /**
92      * Send notifications via email etc to group administrators about
93      * this exciting new pending moderation queue item!
94      */
95     public function notify()
96     {
97         $other = Profile::staticGet('id', $this->subscriber);
98         $listenee = User::staticGet('id', $this->subscribed);
99         mail_subscribe_pending_notify_profile($listenee, $other);
100     }
101 }