Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
13 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. require_once('include/bbcode.php');
  3. require_once('include/datetime.php');
  4. require_once('include/event.php');
  5. require_once('include/items.php');
  6. function events_post(&$a) {
  7. if(! local_user())
  8. return;
  9. $event_id = ((x($_POST,'event_id')) ? intval($_POST['event_id']) : 0);
  10. $cid = ((x($_POST,'cid')) ? intval($_POST['cid']) : 0);
  11. $uid = local_user();
  12. $startyear = intval($_POST['startyear']);
  13. $startmonth = intval($_POST['startmonth']);
  14. $startday = intval($_POST['startday']);
  15. $starthour = intval($_POST['starthour']);
  16. $startminute = intval($_POST['startminute']);
  17. $finishyear = intval($_POST['finishyear']);
  18. $finishmonth = intval($_POST['finishmonth']);
  19. $finishday = intval($_POST['finishday']);
  20. $finishhour = intval($_POST['finishhour']);
  21. $finishminute = intval($_POST['finishminute']);
  22. $adjust = intval($_POST['adjust']);
  23. $nofinish = intval($_POST['nofinish']);
  24. // The default setting for the `private` field in event_store() is false, so mirror that
  25. $private_event = false;
  26. $start = sprintf('%d-%d-%d %d:%d:0',$startyear,$startmonth,$startday,$starthour,$startminute);
  27. if($nofinish)
  28. $finish = '0000-00-00 00:00:00';
  29. else
  30. $finish = sprintf('%d-%d-%d %d:%d:0',$finishyear,$finishmonth,$finishday,$finishhour,$finishminute);
  31. if($adjust) {
  32. $start = datetime_convert(date_default_timezone_get(),'UTC',$start);
  33. if(! $nofinish)
  34. $finish = datetime_convert(date_default_timezone_get(),'UTC',$finish);
  35. }
  36. else {
  37. $start = datetime_convert('UTC','UTC',$start);
  38. if(! $nofinish)
  39. $finish = datetime_convert('UTC','UTC',$finish);
  40. }
  41. // Don't allow the event to finish before it begins.
  42. // It won't hurt anything, but somebody will file a bug report
  43. // and we'll waste a bunch of time responding to it. Time that
  44. // could've been spent doing something else.
  45. if(strcmp($finish,$start) < 0)
  46. $finish = $start;
  47. $summary = escape_tags(trim($_POST['summary']));
  48. $desc = escape_tags(trim($_POST['desc']));
  49. $location = escape_tags(trim($_POST['location']));
  50. $type = 'event';
  51. if((! $desc) || (! $start)) {
  52. notice( t('Event description and start time are required.') . EOL);
  53. goaway($a->get_baseurl() . '/events/new');
  54. }
  55. $share = ((intval($_POST['share'])) ? intval($_POST['share']) : 0);
  56. $c = q("select id from contact where uid = %d and self = 1 limit 1",
  57. intval(local_user())
  58. );
  59. if(count($c))
  60. $self = $c[0]['id'];
  61. else
  62. $self = 0;
  63. if($share) {
  64. $str_group_allow = perms2str($_POST['group_allow']);
  65. $str_contact_allow = perms2str($_POST['contact_allow']);
  66. $str_group_deny = perms2str($_POST['group_deny']);
  67. $str_contact_deny = perms2str($_POST['contact_deny']);
  68. // Undo the pseudo-contact of self, since there are real contacts now
  69. if( strpos($str_contact_allow, '<' . $self . '>') !== false )
  70. {
  71. $str_contact_allow = str_replace('<' . $self . '>', '', $str_contact_allow);
  72. }
  73. // Make sure to set the `private` field as true. This is necessary to
  74. // have the posts show up correctly in Diaspora if an event is created
  75. // as visible only to self at first, but then edited to display to others.
  76. if( strlen($str_group_allow) or strlen($str_contact_allow) or strlen($str_group_deny) or strlen($str_contact_deny) )
  77. {
  78. $private_event = true;
  79. }
  80. }
  81. else {
  82. // Note: do not set `private` field for self-only events. It will
  83. // keep even you from seeing them!
  84. $str_contact_allow = '<' . $self . '>';
  85. $str_group_allow = $str_contact_deny = $str_group_deny = '';
  86. }
  87. $datarray = array();
  88. $datarray['start'] = $start;
  89. $datarray['finish'] = $finish;
  90. $datarray['summary'] = $summary;
  91. $datarray['desc'] = $desc;
  92. $datarray['location'] = $location;
  93. $datarray['type'] = $type;
  94. $datarray['adjust'] = $adjust;
  95. $datarray['nofinish'] = $nofinish;
  96. $datarray['uid'] = $uid;
  97. $datarray['cid'] = $cid;
  98. $datarray['allow_cid'] = $str_contact_allow;
  99. $datarray['allow_gid'] = $str_group_allow;
  100. $datarray['deny_cid'] = $str_contact_deny;
  101. $datarray['deny_gid'] = $str_group_deny;
  102. $datarray['private'] = $private_event;
  103. $datarray['id'] = $event_id;
  104. $datarray['created'] = $created;
  105. $datarray['edited'] = $edited;
  106. $item_id = event_store($datarray);
  107. if(! $cid)
  108. proc_run('php',"include/notifier.php","event","$item_id");
  109. }
  110. function events_content(&$a) {
  111. if(! local_user()) {
  112. notice( t('Permission denied.') . EOL);
  113. return;
  114. }
  115. $htpl = get_markup_template('event_head.tpl');
  116. $a->page['htmlhead'] .= replace_macros($htpl,array('$baseurl' => $a->get_baseurl()));
  117. $o ="";
  118. // tabs
  119. $tabs = profile_tabs($a, True);
  120. $mode = 'view';
  121. $y = 0;
  122. $m = 0;
  123. if($a->argc > 1) {
  124. if($a->argc > 2 && $a->argv[1] == 'event') {
  125. $mode = 'edit';
  126. $event_id = intval($a->argv[2]);
  127. }
  128. if($a->argv[1] === 'new') {
  129. $mode = 'new';
  130. $event_id = 0;
  131. }
  132. if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
  133. $mode = 'view';
  134. $y = intval($a->argv[1]);
  135. $m = intval($a->argv[2]);
  136. }
  137. }
  138. if($mode == 'view') {
  139. $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  140. $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
  141. if(! $y)
  142. $y = intval($thisyear);
  143. if(! $m)
  144. $m = intval($thismonth);
  145. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  146. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  147. if($y < 1901)
  148. $y = 1900;
  149. if($y > 2099)
  150. $y = 2100;
  151. $nextyear = $y;
  152. $nextmonth = $m + 1;
  153. if($nextmonth > 12) {
  154. $nextmonth = 1;
  155. $nextyear ++;
  156. }
  157. $prevyear = $y;
  158. if($m > 1)
  159. $prevmonth = $m - 1;
  160. else {
  161. $prevmonth = 12;
  162. $prevyear --;
  163. }
  164. $dim = get_dim($y,$m);
  165. $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
  166. $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
  167. if ($a->argv[1] === 'json'){
  168. if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
  169. if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
  170. }
  171. $start = datetime_convert('UTC','UTC',$start);
  172. $finish = datetime_convert('UTC','UTC',$finish);
  173. $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
  174. $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
  175. if (x($_GET,'id')){
  176. $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
  177. `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
  178. WHERE `event`.`uid` = %d AND `event`.`id` = %d",
  179. intval(local_user()),
  180. intval($_GET['id'])
  181. );
  182. } else {
  183. $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
  184. `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
  185. WHERE `event`.`uid` = %d
  186. AND (( `adjust` = 0 AND `finish` >= '%s' AND `start` <= '%s' )
  187. OR ( `adjust` = 1 AND `finish` >= '%s' AND `start` <= '%s' )) ",
  188. intval(local_user()),
  189. dbesc($start),
  190. dbesc($finish),
  191. dbesc($adjust_start),
  192. dbesc($adjust_finish)
  193. );
  194. }
  195. $links = array();
  196. if(count($r)) {
  197. $r = sort_by_date($r);
  198. foreach($r as $rr) {
  199. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  200. if(! x($links,$j))
  201. $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
  202. }
  203. }
  204. $events=array();
  205. $last_date = '';
  206. $fmt = t('l, F j');
  207. if(count($r)) {
  208. $r = sort_by_date($r);
  209. foreach($r as $rr) {
  210. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  211. $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
  212. $d = day_translate($d);
  213. $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
  214. if ($rr['nofinish']){
  215. $end = null;
  216. } else {
  217. $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
  218. }
  219. $is_first = ($d !== $last_date);
  220. $last_date = $d;
  221. $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
  222. $title = strip_tags(bbcode($rr['summary']));
  223. if(! $title) {
  224. list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
  225. $title = strip_tags($title);
  226. }
  227. $html = format_event_html($rr);
  228. $rr['desc'] = bbcode($rr['desc']);
  229. $rr['location'] = bbcode($rr['location']);
  230. $events[] = array(
  231. 'id'=>$rr['id'],
  232. 'start'=> $start,
  233. 'end' => $end,
  234. 'allDay' => false,
  235. 'title' => $title,
  236. 'j' => $j,
  237. 'd' => $d,
  238. 'edit' => $edit,
  239. 'is_first'=>$is_first,
  240. 'item'=>$rr,
  241. 'html'=>$html,
  242. 'plink' => array($rr['plink'],t('link to source'),'',''),
  243. );
  244. }
  245. }
  246. if ($a->argv[1] === 'json'){
  247. echo json_encode($events); killme();
  248. }
  249. // links: array('href', 'text', 'extra css classes', 'title')
  250. if (x($_GET,'id')){
  251. $tpl = get_markup_template("event.tpl");
  252. } else {
  253. // if (get_config('experimentals','new_calendar')==1){
  254. $tpl = get_markup_template("events-js.tpl");
  255. // } else {
  256. // $tpl = get_markup_template("events.tpl");
  257. // }
  258. }
  259. $o = replace_macros($tpl, array(
  260. '$baseurl' => $a->get_baseurl(),
  261. '$tabs' => $tabs,
  262. '$title' => t('Events'),
  263. '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
  264. '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
  265. '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
  266. '$calendar' => cal($y,$m,$links, ' eventcal'),
  267. '$events' => $events,
  268. ));
  269. if (x($_GET,'id')){ echo $o; killme(); }
  270. return $o;
  271. }
  272. if($mode === 'edit' && $event_id) {
  273. $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  274. intval($event_id),
  275. intval(local_user())
  276. );
  277. if(count($r))
  278. $orig_event = $r[0];
  279. }
  280. if($mode === 'edit' || $mode === 'new') {
  281. $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
  282. $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
  283. $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
  284. $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
  285. $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
  286. $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
  287. $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
  288. $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
  289. if(! x($orig_event))
  290. $sh_checked = '';
  291. else
  292. $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
  293. if($cid)
  294. $sh_checked .= ' disabled="disabled" ';
  295. $tpl = get_markup_template('event_form.tpl');
  296. $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
  297. $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
  298. $tz = date_default_timezone_get();
  299. if(x($orig_event))
  300. $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
  301. $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
  302. $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
  303. $sday = datetime_convert('UTC', $tz, $sdt, 'd');
  304. $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
  305. $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
  306. $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
  307. $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
  308. $fday = datetime_convert('UTC', $tz, $fdt, 'd');
  309. $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
  310. $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
  311. $f = get_config('system','event_input_format');
  312. if(! $f)
  313. $f = 'ymd';
  314. $dateformat = datesel_format($f);
  315. $timeformat = t('hour:minute');
  316. require_once('include/acl_selectors.php');
  317. $o .= replace_macros($tpl,array(
  318. '$post' => $a->get_baseurl() . '/events',
  319. '$eid' => $eid,
  320. '$cid' => $cid,
  321. '$uri' => $uri,
  322. '$title' => t('Event details'),
  323. '$desc' => sprintf( t('Format is %s %s. Starting date and Description are required.'),$dateformat,$timeformat),
  324. '$s_text' => t('Event Starts:') . ' <span class="required">*</span> ',
  325. '$s_dsel' => datesel($f,'start',$syear+5,$syear,false,$syear,$smonth,$sday),
  326. '$s_tsel' => timesel('start',$shour,$sminute),
  327. '$n_text' => t('Finish date/time is not known or not relevant'),
  328. '$n_checked' => $n_checked,
  329. '$f_text' => t('Event Finishes:'),
  330. '$f_dsel' => datesel($f,'finish',$fyear+5,$fyear,false,$fyear,$fmonth,$fday),
  331. '$f_tsel' => timesel('finish',$fhour,$fminute),
  332. '$a_text' => t('Adjust for viewer timezone'),
  333. '$a_checked' => $a_checked,
  334. '$d_text' => t('Description:') . ' <span class="required">*</span>',
  335. '$d_orig' => $d_orig,
  336. '$l_text' => t('Location:'),
  337. '$l_orig' => $l_orig,
  338. '$t_text' => t('Title:'),
  339. '$t_orig' => $t_orig,
  340. '$sh_text' => t('Share this event'),
  341. '$sh_checked' => $sh_checked,
  342. '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user),false)),
  343. '$submit' => t('Submit')
  344. ));
  345. return $o;
  346. }
  347. }