]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TagSub/TagSub.php
AJAX submit actions for tag subscribe/unsubscribe
[quix0rs-gnu-social.git] / plugins / TagSub / TagSub.php
1 <?php
2 /**
3  * Data class to store local tag subscriptions
4  *
5  * PHP version 5
6  *
7  * @category TagSubPlugin
8  * @package  StatusNet
9  * @author   Brion Vibber <brion@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the tag subscriptions
36  *
37  * @category PollPlugin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45
46 class TagSub extends Managed_DataObject
47 {
48     public $__table = 'tagsub'; // table name
49     public $tag;         // text
50     public $profile_id;  // int -> profile.id
51     public $created;     // datetime
52
53     /**
54      * Get an instance by key
55      *
56      * This is a utility method to get a single instance with a given key value.
57      *
58      * @param string $k Key to use to lookup (usually 'user_id' for this class)
59      * @param mixed  $v Value to lookup
60      *
61      * @return TagSub object found, or null for no hits
62      *
63      */
64     function staticGet($k, $v=null)
65     {
66         return Memcached_DataObject::staticGet('TagSub', $k, $v);
67     }
68
69     /**
70      * Get an instance by compound key
71      *
72      * This is a utility method to get a single instance with a given set of
73      * key-value pairs. Usually used for the primary key for a compound key; thus
74      * the name.
75      *
76      * @param array $kv array of key-value mappings
77      *
78      * @return TagSub object found, or null for no hits
79      *
80      */
81     function pkeyGet($kv)
82     {
83         return Memcached_DataObject::pkeyGet('TagSub', $kv);
84     }
85
86     /**
87      * The One True Thingy that must be defined and declared.
88      */
89     public static function schemaDef()
90     {
91         return array(
92             'description' => 'TagSubPlugin tag subscription records',
93             'fields' => array(
94                 'tag' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash tag associated with this subscription'),
95                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
96                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
97             ),
98             'primary key' => array('tag', 'profile_id'),
99             'foreign keys' => array(
100                 'tagsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
101             ),
102             'indexes' => array(
103                 'tagsub_created_idx' => array('created'),
104                 'tagsub_profile_id_tag_idx' => array('profile_id', 'tag'),
105             ),
106         );
107     }
108
109     /**
110      * Start a tag subscription!
111      *
112      * @param profile $profile subscriber
113      * @param string $tag subscribee
114      * @return TagSub
115      */
116     static function start(Profile $profile, $tag)
117     {
118         $ts = new TagSub();
119         $ts->tag = $tag;
120         $ts->profile_id = $profile->id;
121         $ts->created = common_sql_now();
122         $ts->insert();
123         return $ts;
124     }
125
126     /**
127      * End a tag subscription!
128      *
129      * @param profile $profile subscriber
130      * @param string $tag subscribee
131      */
132     static function cancel(Profile $profile, $tag)
133     {
134         $ts = TagSub::pkeyGet(array('tag' => $tag,
135                                     'profile_id' => $profile->id));
136         if ($ts) {
137             $ts->delete();
138         }
139     }
140 }