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