]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag_subscription.php
Remove sequenceKey() since we now use Managed_DataObject
[quix0rs-gnu-social.git] / classes / Profile_tag_subscription.php
1 <?php
2 /**
3  * Table Definition for profile_tag_subscription
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Profile_tag_subscription extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'profile_tag_subscription';                     // table name
13     public $profile_tag_id;                         // int(4)  not_null
14     public $profile_id;                             // int(4)  not_null
15     public $created;                                // datetime   not_null default_0000-00-00%2000%3A00%3A00
16     public $modified;                               // timestamp()   not_null default_CURRENT_TIMESTAMP
17
18     /* Static get */
19     function staticGet($k,$v=null)
20     { return Memcached_DataObject::staticGet('Profile_tag_subscription',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     public static function schemaDef()
26     {
27         return array(
28             'fields' => array(
29                 'profile_tag_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile_tag'),
30                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
31
32                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
33                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
34             ),
35             'primary key' => array('profile_tag_id', 'profile_id'),
36             'foreign keys' => array(
37                 'profile_tag_subscription_profile_list_id_fkey' => array('profile_list', array('profile_tag_id' => 'id')),
38                 'profile_tag_subscription_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
39             ),
40             'indexes' => array(
41                 // @fixme probably we want a (profile_id, created) index here?
42                 'profile_tag_subscription_profile_id_idx' => array('profile_id'),
43                 'profile_tag_subscription_created_idx' => array('created'),
44             ),
45         );
46     }
47
48     function pkeyGet($kv)
49     {
50         return Memcached_DataObject::pkeyGet('Profile_tag_subscription', $kv);
51     }
52
53     static function add($peopletag, $profile)
54     {
55         if ($peopletag->private) {
56             return false;
57         }
58
59         if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
60             $args = array('profile_tag_id' => $peopletag->id,
61                           'profile_id' => $profile->id);
62             $existing = Profile_tag_subscription::pkeyGet($args);
63             if(!empty($existing)) {
64                 return $existing;
65             }
66
67             $sub = new Profile_tag_subscription();
68             $sub->profile_tag_id = $peopletag->id;
69             $sub->profile_id = $profile->id;
70             $sub->created = common_sql_now();
71
72             $result = $sub->insert();
73
74             if (!$result) {
75                 common_log_db_error($sub, 'INSERT', __FILE__);
76                 // TRANS: Exception thrown when inserting a list subscription in the database fails.
77                 throw new Exception(_('Adding list subscription failed.'));
78             }
79
80             $ptag = Profile_list::staticGet('id', $peopletag->id);
81             $ptag->subscriberCount(true);
82
83             Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
84             return $ptag;
85         }
86     }
87
88     static function remove($peopletag, $profile)
89     {
90         $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
91                                               'profile_id' => $profile->id));
92
93         if (empty($sub)) {
94             // silence is golden?
95             return true;
96         }
97
98         if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
99             $result = $sub->delete();
100
101             if (!$result) {
102                 common_log_db_error($sub, 'DELETE', __FILE__);
103                 // TRANS: Exception thrown when deleting a list subscription from the database fails.
104                 throw new Exception(_('Removing list subscription failed.'));
105             }
106
107             $peopletag->subscriberCount(true);
108
109             Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
110             return true;
111         }
112     }
113
114     // called if a tag gets deleted / made private
115     static function cleanup($profile_list) {
116         $subs = new self();
117         $subs->profile_tag_id = $profile_list->id;
118         $subs->find();
119
120         while($subs->fetch()) {
121             $profile = Profile::staticGet('id', $subs->profile_id);
122             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
123             // Delete anyway
124             $subs->delete();
125             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
126         }
127     }
128
129     function insert()
130     {
131         $result = parent::insert();
132         if ($result) {
133             self::blow('profile_list:subscriber_count:%d', 
134                        $this->profile_tag_id);
135         }
136         return $result;
137     }
138
139     function delete()
140     {
141         $result = parent::delete();
142         if ($result) {
143             self::blow('profile_list:subscriber_count:%d', 
144                        $this->profile_tag_id);
145         }
146         return $result;
147     }
148 }