]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/User_flag_profile.php
Merge remote-tracking branch 'evan/blogplugin' into newblogplugin
[quix0rs-gnu-social.git] / plugins / UserFlag / User_flag_profile.php
1 <?php
2 /**
3  * Data class for profile flags
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@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) 2009, 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 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for profile flags
38  *
39  * A class representing a user flagging another profile for review.
40  *
41  * @category Action
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://status.net/
46  */
47 class User_flag_profile extends Memcached_DataObject
48 {
49     ###START_AUTOCODE
50     /* the code below is auto generated do not remove the above tag */
51
52     public $__table = 'user_flag_profile';               // table name
53     public $profile_id;                      // int(4)  primary_key not_null
54     public $user_id;                         // int(4)  primary_key not_null
55     public $created;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
56     public $cleared;                         // datetime   not_null default_0000-00-00%2000%3A00%3A00
57
58     /* Static get */
59     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_flag_profile',$k,$v); }
60
61     /* the code above is auto generated do not remove the tag below */
62     ###END_AUTOCODE
63
64     /**
65      * return table definition for DB_DataObject
66      *
67      * @return array array of column definitions
68      */
69     function table()
70     {
71         return array(
72                      'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
73                      'user_id'    => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
74                      'created'    => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
75                      'cleared'    => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME
76                      );
77     }
78
79     /**
80      * return key definitions for DB_DataObject
81      *
82      * @return array of key names
83      */
84     function keys()
85     {
86         return array_keys($this->keyTypes());
87     }
88
89     /**
90      * return key definitions for DB_DataObject
91      *
92      * @return array map of key definitions
93      */
94     function keyTypes()
95     {
96         return array('profile_id' => 'K', 'user_id' => 'K');
97     }
98
99     /**
100      * Magic formula for non-autoincrementing integer primary keys
101      *
102      * If a table has a single integer column as its primary key, DB_DataObject
103      * assumes that the column is auto-incrementing and makes a sequence table
104      * to do this incrementation. Since we don't need this for our class, we
105      * overload this method and return the magic formula that DB_DataObject needs.
106      *
107      * @return array magic three-false array that stops auto-incrementing.
108      */
109     function sequenceKey()
110     {
111         return array(false, false, false);
112     }
113
114     /**
115      * Get a single object with multiple keys
116      *
117      * @param array $kv Map of key-value pairs
118      *
119      * @return User_flag_profile found object or null
120      */
121     function pkeyGet($kv)
122     {
123         return Memcached_DataObject::pkeyGet('User_flag_profile', $kv);
124     }
125
126     /**
127      * Check if a flag exists for given profile and user
128      *
129      * @param integer $profile_id Profile to check for
130      * @param integer $user_id    User to check for
131      *
132      * @return boolean true if exists, else false
133      */
134     static function exists($profile_id, $user_id)
135     {
136         $ufp = User_flag_profile::pkeyGet(array('profile_id' => $profile_id,
137                                                 'user_id' => $user_id));
138
139         return !empty($ufp);
140     }
141
142     /**
143      * Create a new flag
144      *
145      * @param integer $user_id    ID of user who's flagging
146      * @param integer $profile_id ID of profile being flagged
147      *
148      * @return boolean success flag
149      */
150     static function create($user_id, $profile_id)
151     {
152         $ufp = new User_flag_profile();
153
154         $ufp->profile_id = $profile_id;
155         $ufp->user_id    = $user_id;
156         $ufp->created    = common_sql_now();
157
158         if (!$ufp->insert()) {
159             // TRANS: Server exception.
160             // TRANS: %d is a profile ID (number).
161             $msg = sprintf(_m('Could not flag profile "%d" for review.'),
162                            $profile_id);
163             throw new ServerException($msg);
164         }
165
166         $ufp->free();
167
168         return true;
169     }
170 }