]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Foreign_link.php
Merge commit 'refs/merge-requests/159' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / classes / Foreign_link.php
1 <?php
2 /**
3  * Table Definition for foreign_link
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Foreign_link extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'foreign_link';                    // table name
13     public $user_id;                         // int(4)  primary_key not_null
14     public $foreign_id;                      // bigint(8)  primary_key not_null unsigned
15     public $service;                         // int(4)  primary_key not_null
16     public $credentials;                     // varchar(255)
17     public $noticesync;                      // tinyint(1)   not_null default_1
18     public $friendsync;                      // tinyint(1)   not_null default_2
19     public $profilesync;                     // tinyint(1)   not_null default_1
20     public $last_noticesync;                 // datetime()
21     public $last_friendsync;                 // datetime()
22     public $created;                         // datetime()   not_null
23     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
24
25     /* Static get */
26     function staticGet($k,$v=null)
27     { return Memcached_DataObject::staticGet('Foreign_link',$k,$v); }
28
29     /* the code above is auto generated do not remove the tag below */
30     ###END_AUTOCODE
31
32     public static function schemaDef()
33     {
34         return array(
35             'fields' => array(
36                 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'link to user on this system, if exists'),
37                 'foreign_id' => array('type' => 'int', 'size' => 'big', 'unsigned' => true, 'not null' => true, 'description' => 'link to user on foreign service, if exists'),
38                 'service' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to service'),
39                 'credentials' => array('type' => 'varchar', 'length' => 255, 'description' => 'authc credentials, typically a password'),
40                 'noticesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies'),
41                 'friendsync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 2, 'description' => 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
42                 'profilesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
43                 'last_noticesync' => array('type' => 'datetime', 'description' => 'last time notices were imported'),
44                 'last_friendsync' => array('type' => 'datetime', 'description' => 'last time friends were imported'),
45                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
46                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
47             ),
48             'primary key' => array('user_id', 'foreign_id', 'service'),
49             'foreign keys' => array(
50                 'foreign_link_user_id_fkey' => array('user', array('user_id' => 'id')),
51                 'foreign_link_foreign_id_fkey' => array('foreign_user', array('foreign_id' => 'id', 'service' => 'service')),
52                 'foreign_link_service_fkey' => array('foreign_service', array('service' => 'id')),
53             ),
54             'indexes' => array(
55                 'foreign_user_user_id_idx' => array('user_id'),
56             ),
57         );
58     }
59
60     static function getByUserID($user_id, $service)
61     {
62         if (empty($user_id) || empty($service)) {
63             return null;
64         }
65
66         $flink = new Foreign_link();
67
68         $flink->service = $service;
69         $flink->user_id = $user_id;
70         $flink->limit(1);
71
72         $result = $flink->find(true);
73
74         return empty($result) ? null : $flink;
75     }
76
77     static function getByForeignID($foreign_id, $service)
78     {
79         if (empty($foreign_id) || empty($service)) {
80             return null;
81         } else {
82             $flink = new Foreign_link();
83             $flink->service = $service;
84             $flink->foreign_id = $foreign_id;
85             $flink->limit(1);
86
87             $result = $flink->find(true);
88
89             return empty($result) ? null : $flink;
90         }
91     }
92
93     function set_flags($noticesend, $noticerecv, $replysync, $friendsync)
94     {
95         if ($noticesend) {
96             $this->noticesync |= FOREIGN_NOTICE_SEND;
97         } else {
98             $this->noticesync &= ~FOREIGN_NOTICE_SEND;
99         }
100
101         if ($noticerecv) {
102             $this->noticesync |= FOREIGN_NOTICE_RECV;
103         } else {
104             $this->noticesync &= ~FOREIGN_NOTICE_RECV;
105         }
106
107         if ($replysync) {
108             $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
109         } else {
110             $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
111         }
112
113         if ($friendsync) {
114             $this->friendsync |= FOREIGN_FRIEND_RECV;
115         } else {
116             $this->friendsync &= ~FOREIGN_FRIEND_RECV;
117         }
118
119         $this->profilesync = 0;
120     }
121
122     // Convenience methods
123     function getForeignUser()
124     {
125         $fuser = new Foreign_user();
126         $fuser->service = $this->service;
127         $fuser->id = $this->foreign_id;
128
129         $fuser->limit(1);
130
131         if ($fuser->find(true)) {
132             return $fuser;
133         }
134
135         return null;
136     }
137
138     function getUser()
139     {
140         return User::staticGet($this->user_id);
141     }
142
143     // Make sure we only ever delete one record at a time
144     function safeDelete()
145     {
146         if (!empty($this->user_id)
147             && !empty($this->foreign_id)
148             && !empty($this->service))
149         {
150             return $this->delete();
151         } else {
152             common_debug(LOG_WARNING,
153                 'Foreign_link::safeDelete() tried to delete a '
154                 . 'Foreign_link without a fully specified compound key: '
155                 . var_export($this, true));
156             return false;
157         }
158     }
159 }