aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Widget/Random_block.php
blob: 9052bea0357607834d1786cae3e7ec8f2f4ec9ad (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
<?php

/**
 *   * Name: Random block
 *   * Description: Display a random block item
 */

namespace Zotlabs\Widget;

class Random_block {

	function widget($arr) {

		$channel_id = 0;
		if(array_key_exists('channel_id',$arr) && intval($arr['channel_id']))
			$channel_id = intval($arr['channel_id']);
		if(! $channel_id)
			$channel_id = \App::$profile_uid;
		if(! $channel_id)
			return '';

		if(array_key_exists('contains',$arr))
			$contains = $arr['contains'];

		$o = '';

		require_once('include/security.php');
		$sql_options = item_permissions_sql($channel_id);

		$randfunc = db_getfunc('RAND');

		$r = q("select item.* from item left join iconfig on item.id = iconfig.iid
			where item.uid = %d and iconfig.cat = 'system' and iconfig.v like '%s' and iconfig.k = 'BUILDBLOCK' and
			item_type = %d $sql_options order by $randfunc limit 1",
			intval($channel_id),
			dbesc('%' . $contains . '%'),
			intval(ITEM_TYPE_BLOCK)
		);

		if($r) {
			$o = '<div class="widget bblock">';
			if($r[0]['title'])
				$o .= '<h3>' . $r[0]['title'] . '</h3>';

			$o .= prepare_text($r[0]['body'],$r[0]['mimetype']);
			$o .= '</div>';
		}

		return $o;
	}
}