aboutsummaryrefslogtreecommitdiffstats
path: root/mod/connections.php
blob: e2d467c601c412fc7aedfa004ae84aaba08d1cb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php

require_once('include/Contact.php');
require_once('include/socgraph.php');
require_once('include/contact_selectors.php');
require_once('include/group.php');
require_once('include/contact_widgets.php');
require_once('include/zot.php');
require_once('include/widgets.php');

function connections_init(&$a) {

	if(! local_user())
		return;

	$channel = $a->get_channel();
	if($channel)
		head_set_icon($channel['xchan_photo_s']);

}

function connections_post(&$a) {
	
	if(! local_user())
		return;

	$contact_id = intval(argv(1));
	if(! $contact_id)
		return;

	$orig_record = q("SELECT * FROM abook WHERE abook_id = %d AND abook_channel = %d LIMIT 1",
		intval($contact_id),
		intval(local_user())
	);

	if(! $orig_record) {
		notice( t('Could not access contact record.') . EOL);
		goaway(z_root() . '/connections');
		return; // NOTREACHED
	}

	call_hooks('contact_edit_post', $_POST);

	$profile_id = $_POST['profile_assign'];
	if($profile_id) {
		$r = q("SELECT profile_guid FROM profile WHERE profile_guid = '%s' AND `uid` = %d LIMIT 1",
			dbesc($profile_id),
			intval(local_user())
		);
		if(! count($r)) {
			notice( t('Could not locate selected profile.') . EOL);
			return;
		}
	}

	$hidden = intval($_POST['hidden']);

	$priority = intval($_POST['poll']);
	if($priority > 5 || $priority < 0)
		$priority = 0;

	$closeness = intval($_POST['closeness']);
	if($closeness < 0)
		$closeness = 99;

	$abook_my_perms = 0;

	foreach($_POST as $k => $v) {
		if(strpos($k,'perms_') === 0) {
			$abook_my_perms += $v;
		}
	}			

	$abook_flags = $orig_record[0]['abook_flags'];
	$new_friend = false;


	if(($_REQUEST['pending']) && ($abook_flags & ABOOK_FLAG_PENDING)) {
		$abook_flags = ( $abook_flags ^ ABOOK_FLAG_PENDING );
		$new_friend = true;
	}

	$r = q("UPDATE abook SET abook_profile = '%s', abook_my_perms = %d , abook_closeness = %d, abook_flags = %d
		where abook_id = %d AND abook_channel = %d LIMIT 1",
		dbesc($profile_id),
		intval($abook_my_perms),
		intval($closeness),
		intval($abook_flags),
		intval($contact_id),
		intval(local_user())
	);

	if($r)
		info( t('Connection updated.') . EOL);
	else
		notice( t('Failed to update connection record.') . EOL);

	if((x($a->data,'abook')) && $a->data['abook']['abook_my_perms'] != $abook_my_perms 
		&& (! ($a->data['abook']['abook_flags'] & ABOOK_FLAG_SELF))) {
		proc_run('php', 'include/notifier.php', 'permission_update', $contact_id);
	}

	if($new_friend) {
		$channel = $a->get_channel();		
		$default_group = $channel['channel_default_group'];
		if($default_group) {
			require_once('include/group.php');
			$g = group_rec_byhash(local_user(),$default_group);
			if($g)
				group_add_member(local_user(),'',$a->data['abook_xchan'],$g['id']);
		}



		// Check if settings permit ("post new friend activity" is allowed, and 
		// friends in general or this friend in particular aren't hidden) 
		// and send out a new friend activity
		// TODO

		// pull in a bit of content if there is any to pull in
		proc_run('php','include/onepoll.php',$contact_id);

	}

	// Refresh the structure in memory with the new data

	$r = q("SELECT abook.*, xchan.* 
		FROM abook left join xchan on abook_xchan = xchan_hash
		WHERE abook_channel = %d and abook_id = %d LIMIT 1",
		intval(local_user()),
		intval($contact_id)
	);
	if($r) {
		$a->data['abook'] = $r[0];
	}

	if($new_friend) {
		$arr = array('channel_id' => local_user(), 'abook' => $a->data['abook']);
		call_hooks('accept_follow', $arr);
	}

	connections_clone($a);

	return;

}

function connections_clone(&$a) {

		if(! array_key_exists('abook',$a->data))
			return;
		$clone = $a->data['abook'];

		unset($clone['abook_id']);
		unset($clone['abook_account']);
		unset($clone['abook_channel']);

		build_sync_packet(0 /* use the current local_user */, array('abook' => array($clone)));
}


function connections_content(&$a) {

	$sort_type = 0;
	$o = '';


	if(! local_user()) {
		notice( t('Permission denied.') . EOL);
		return login();
	}

	$blocked     = false;
	$hidden      = false;
	$ignored     = false;
	$archived    = false;
	$unblocked   = false;
	$pending     = false;
	$unconnected = false;
	$all         = false;

	$_SESSION['return_url'] = $a->query_string;

	$search_flags = 0;
	$head = '';

	if(argc() == 2) {
		switch(argv(1)) {
			case 'blocked':
				$search_flags = ABOOK_FLAG_BLOCKED;
				$head = t('Blocked');
				$blocked = true;
				break;
			case 'ignored':
				$search_flags = ABOOK_FLAG_IGNORED;
				$head = t('Ignored');
				$ignored = true;
				break;
			case 'hidden':
				$search_flags = ABOOK_FLAG_HIDDEN;
				$head = t('Hidden');
				$hidden = true;
				break;
			case 'archived':
				$search_flags = ABOOK_FLAG_ARCHIVED;
				$head = t('Archived');
				$archived = true;
				break;
			case 'pending':
				$search_flags = ABOOK_FLAG_PENDING;
				$head = t('New');
				$pending = true;
				nav_set_selected('intros');
				break;
			case 'ifpending':
				$r = q("SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and not (abook_flags & %d) and (abook_flags & %d) and not (xchan_flags & %d )",
					intval(local_user()),
					intval(ABOOK_FLAG_SELF|ABOOK_FLAG_PENDING|ABOOK_FLAG_IGNORED),
					intval(ABOOK_FLAG_PENDING),
					intval(XCHAN_FLAGS_DELETED|XCHAN_FLAGS_ORPHAN)
				);
				if($r && $r[0]['total']) {
					$search_flags = ABOOK_FLAG_PENDING;
					$head = t('New');
					$pending = true;
					nav_set_selected('intros');
					$a->argv[1] = 'pending';
				}
				else {
					$head = t('All');
					$search_flags = 0;
					$all = true;
					$a->argc = 1;
					unset($a->argv[1]);
				}
				break;
			case 'unconnected':
				$search_flags = ABOOK_FLAG_UNCONNECTED;
				$head = t('Unconnected');
				$unconnected = true;
				break;

			case 'all':
				$head = t('All');
			default:
				$search_flags = 0;
				$all = true;
				break;

		}

		$sql_extra = (($search_flags) ? " and ( abook_flags & " . $search_flags . " ) " : "");
		if(argv(1) === 'pending')
			$sql_extra .= " and not ( abook_flags & " . ABOOK_FLAG_IGNORED . " ) ";

	}
	else {
		$sql_extra = " and not ( abook_flags & " . ABOOK_FLAG_BLOCKED . " ) ";
		$unblocked = true;
	}

	$search = ((x($_REQUEST,'search')) ? notags(trim($_REQUEST['search'])) : '');

	$tabs = array(
		array(
			'label' => t('Suggestions'),
			'url'   => z_root() . '/suggest', 
			'sel'   => '',
			'title' => t('Suggest new connections'),
		),
		array(
			'label' => t('New Connections'),
			'url'   => z_root() . '/connections/pending', 
			'sel'   => ($pending) ? 'active' : '',
			'title' => t('Show pending (new) connections'),
		),
		array(
			'label' => t('All Connections'),
			'url'   => z_root() . '/connections/all', 
			'sel'   => ($all) ? 'active' : '',
			'title' => t('Show all connections'),
		),
		array(
			'label' => t('Unblocked'),
			'url'   => z_root() . '/connections',
			'sel'   => (($unblocked) && (! $search) && (! $nets)) ? 'active' : '',
			'title' => t('Only show unblocked connections'),
		),

		array(
			'label' => t('Blocked'),
			'url'   => z_root() . '/connections/blocked',
			'sel'   => ($blocked) ? 'active' : '',
			'title' => t('Only show blocked connections'),
		),

		array(
			'label' => t('Ignored'),
			'url'   => z_root() . '/connections/ignored',
			'sel'   => ($ignored) ? 'active' : '',
			'title' => t('Only show ignored connections'),
		),

		array(
			'label' => t('Archived'),
			'url'   => z_root() . '/connections/archived',
			'sel'   => ($archived) ? 'active' : '',
			'title' => t('Only show archived connections'),
		),

		array(
			'label' => t('Hidden'),
			'url'   => z_root() . '/connections/hidden',
			'sel'   => ($hidden) ? 'active' : '',
			'title' => t('Only show hidden connections'),
		),

		array(
			'label' => t('Unconnected'),
			'url'   => z_root() . '/connections/unconnected',
			'sel'   => ($unconnected) ? 'active' : '',
			'title' => t('Only show one-way connections'),
		),


	);

	$tab_tpl = get_markup_template('common_tabs.tpl');
	$t = replace_macros($tab_tpl, array('$tabs'=>$tabs));

	$searching = false;
	if($search) {
		$search_hdr = $search;
		$search_txt = dbesc(protect_sprintf(preg_quote($search)));
		$searching = true;
	}
	$sql_extra .= (($searching) ? protect_sprintf(" AND xchan_name like '%$search_txt%' ") : "");

 	
	$r = q("SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash 
		where abook_channel = %d and not (abook_flags & %d) and not (xchan_flags & %d ) $sql_extra $sql_extra2 ",
		intval(local_user()),
		intval(ABOOK_FLAG_SELF),
		intval(XCHAN_FLAGS_DELETED|XCHAN_FLAGS_ORPHAN)
	);
	if($r) {
		$a->set_pager_total($r[0]['total']);
		$total = $r[0]['total'];
	}

	$r = q("SELECT abook.*, xchan.* FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash
		WHERE abook_channel = %d and not (abook_flags & %d) and not ( xchan_flags & %d) $sql_extra $sql_extra2 ORDER BY xchan_name LIMIT %d , %d ",
		intval(local_user()),
		intval(ABOOK_FLAG_SELF),
		intval(XCHAN_FLAGS_DELETED|XCHAN_FLAGS_ORPHAN),
		intval($a->pager['start']),
		intval($a->pager['itemspage'])
	);

	$contacts = array();

	if(count($r)) {

		foreach($r as $rr) {
			if($rr['xchan_url']) {
				$contacts[] = array(
					'img_hover' => sprintf( t('%1$s [%2$s]'),$rr['xchan_name'],$rr['xchan_url']),
					'edit_hover' => t('Edit contact'),
					'id' => $rr['abook_id'],
					'alt_text' => $alt_text,
					'dir_icon' => $dir_icon,
					'thumb' => $rr['xchan_photo_m'], 
					'name' => $rr['xchan_name'],
					'username' => $rr['xchan_name'],
					'classes' => (($rr['abook_flags'] & ABOOK_FLAG_ARCHIVED) ? 'archived' : ''),
					'link' => z_root() . '/connedit/' . $rr['abook_id'],
					'url' => chanlink_url($rr['xchan_url']),
					'network' => network_to_name($rr['network']),
				);
			}
		}
	}
	
	$o .= replace_macros(get_markup_template('connections.tpl'),array(
		'$header' => t('Connections') . (($head) ? ' - ' . $head : ''),
		'$tabs' => $t,
		'$total' => $total,
		'$search' => $search_hdr,
		'$desc' => t('Search your connections'),
		'$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
		'$submit' => t('Find'),
		'$edit' => t('Edit'),
		'$cmd' => $a->cmd,
		'$contacts' => $contacts,
		'$paginate' => paginate($a),

	)); 
	
	return $o;
}