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.

484 lines
15 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
9 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((! $summary) || (! $start)) {
  52. notice( t('Event title 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) ? 1 : 0);
  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. if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
  116. $r = q("update event set ignore = 1 where id = %d and uid = %d",
  117. intval($a->argv[2]),
  118. intval(local_user())
  119. );
  120. }
  121. if(($a->argc > 2) && ($a->argv[1] === 'unignore') && intval($a->argv[2])) {
  122. $r = q("update event set ignore = 0 where id = %d and uid = %d",
  123. intval($a->argv[2]),
  124. intval(local_user())
  125. );
  126. }
  127. $editselect = 'none';
  128. if( feature_enabled(local_user(), 'richtext') )
  129. $editselect = 'textareas';
  130. $htpl = get_markup_template('event_head.tpl');
  131. $a->page['htmlhead'] .= replace_macros($htpl,array(
  132. '$baseurl' => $a->get_baseurl(),
  133. '$editselect' => $editselect
  134. ));
  135. $etpl = get_markup_template('event_end.tpl');
  136. $a->page['end'] .= replace_macros($etpl,array(
  137. '$baseurl' => $a->get_baseurl(),
  138. '$editselect' => $editselect
  139. ));
  140. $o ="";
  141. // tabs
  142. $tabs = profile_tabs($a, True);
  143. $mode = 'view';
  144. $y = 0;
  145. $m = 0;
  146. $ignored = ((x($_REQUEST,'ignored')) ? intval($_REQUEST['ignored']) : 0);
  147. if($a->argc > 1) {
  148. if($a->argc > 2 && $a->argv[1] == 'event') {
  149. $mode = 'edit';
  150. $event_id = intval($a->argv[2]);
  151. }
  152. if($a->argv[1] === 'new') {
  153. $mode = 'new';
  154. $event_id = 0;
  155. }
  156. if($a->argc > 2 && intval($a->argv[1]) && intval($a->argv[2])) {
  157. $mode = 'view';
  158. $y = intval($a->argv[1]);
  159. $m = intval($a->argv[2]);
  160. }
  161. }
  162. if($mode == 'view') {
  163. $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  164. $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
  165. if(! $y)
  166. $y = intval($thisyear);
  167. if(! $m)
  168. $m = intval($thismonth);
  169. // Put some limits on dates. The PHP date functions don't seem to do so well before 1900.
  170. // An upper limit was chosen to keep search engines from exploring links millions of years in the future.
  171. if($y < 1901)
  172. $y = 1900;
  173. if($y > 2099)
  174. $y = 2100;
  175. $nextyear = $y;
  176. $nextmonth = $m + 1;
  177. if($nextmonth > 12) {
  178. $nextmonth = 1;
  179. $nextyear ++;
  180. }
  181. $prevyear = $y;
  182. if($m > 1)
  183. $prevmonth = $m - 1;
  184. else {
  185. $prevmonth = 12;
  186. $prevyear --;
  187. }
  188. $dim = get_dim($y,$m);
  189. $start = sprintf('%d-%d-%d %d:%d:%d',$y,$m,1,0,0,0);
  190. $finish = sprintf('%d-%d-%d %d:%d:%d',$y,$m,$dim,23,59,59);
  191. if ($a->argv[1] === 'json'){
  192. if (x($_GET,'start')) $start = date("Y-m-d h:i:s", $_GET['start']);
  193. if (x($_GET,'end')) $finish = date("Y-m-d h:i:s", $_GET['end']);
  194. }
  195. $start = datetime_convert('UTC','UTC',$start);
  196. $finish = datetime_convert('UTC','UTC',$finish);
  197. $adjust_start = datetime_convert('UTC', date_default_timezone_get(), $start);
  198. $adjust_finish = datetime_convert('UTC', date_default_timezone_get(), $finish);
  199. if (x($_GET,'id')){
  200. $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
  201. `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
  202. WHERE `event`.`uid` = %d AND `event`.`id` = %d",
  203. intval(local_user()),
  204. intval($_GET['id'])
  205. );
  206. } else {
  207. $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
  208. `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event` LEFT JOIN `item` ON `item`.`event-id` = `event`.`id`
  209. WHERE `event`.`uid` = %d and event.ignore = %d
  210. AND (( `adjust` = 0 AND ( `finish` >= '%s' OR ( nofinish AND start >= '%s' ) ) AND `start` <= '%s' )
  211. OR ( `adjust` = 1 AND ( `finish` >= '%s' OR ( nofinish AND start >= '%s' ) ) AND `start` <= '%s' )) ",
  212. intval(local_user()),
  213. intval($ignored),
  214. dbesc($start),
  215. dbesc($start),
  216. dbesc($finish),
  217. dbesc($adjust_start),
  218. dbesc($adjust_start),
  219. dbesc($adjust_finish)
  220. );
  221. }
  222. $links = array();
  223. if(count($r)) {
  224. $r = sort_by_date($r);
  225. foreach($r as $rr) {
  226. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  227. if(! x($links,$j))
  228. $links[$j] = $a->get_baseurl() . '/' . $a->cmd . '#link-' . $j;
  229. }
  230. }
  231. $events=array();
  232. $last_date = '';
  233. $fmt = t('l, F j');
  234. if(count($r)) {
  235. $r = sort_by_date($r);
  236. foreach($r as $rr) {
  237. $j = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'j') : datetime_convert('UTC','UTC',$rr['start'],'j'));
  238. $d = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], $fmt) : datetime_convert('UTC','UTC',$rr['start'],$fmt));
  239. $d = day_translate($d);
  240. $start = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['start'], 'c') : datetime_convert('UTC','UTC',$rr['start'],'c'));
  241. if ($rr['nofinish']){
  242. $end = null;
  243. } else {
  244. $end = (($rr['adjust']) ? datetime_convert('UTC',date_default_timezone_get(),$rr['finish'], 'c') : datetime_convert('UTC','UTC',$rr['finish'],'c'));
  245. }
  246. $is_first = ($d !== $last_date);
  247. $last_date = $d;
  248. $edit = ((! $rr['cid']) ? array($a->get_baseurl().'/events/event/'.$rr['id'],t('Edit event'),'','') : null);
  249. $title = strip_tags(html_entity_decode(bbcode($rr['summary']),ENT_QUOTES,'UTF-8'));
  250. if(! $title) {
  251. list($title, $_trash) = explode("<br",bbcode($rr['desc']),2);
  252. $title = strip_tags(html_entity_decode($title,ENT_QUOTES,'UTF-8'));
  253. }
  254. $html = format_event_html($rr);
  255. $rr['desc'] = bbcode($rr['desc']);
  256. $rr['location'] = bbcode($rr['location']);
  257. $events[] = array(
  258. 'id'=>$rr['id'],
  259. 'start'=> $start,
  260. 'end' => $end,
  261. 'allDay' => false,
  262. 'title' => $title,
  263. 'j' => $j,
  264. 'd' => $d,
  265. 'edit' => $edit,
  266. 'is_first'=>$is_first,
  267. 'item'=>$rr,
  268. 'html'=>$html,
  269. 'plink' => array($rr['plink'],t('link to source'),'',''),
  270. );
  271. }
  272. }
  273. if ($a->argv[1] === 'json'){
  274. echo json_encode($events); killme();
  275. }
  276. // links: array('href', 'text', 'extra css classes', 'title')
  277. if (x($_GET,'id')){
  278. $tpl = get_markup_template("event.tpl");
  279. } else {
  280. // if (get_config('experimentals','new_calendar')==1){
  281. $tpl = get_markup_template("events-js.tpl");
  282. // } else {
  283. // $tpl = get_markup_template("events.tpl");
  284. // }
  285. }
  286. // Get rid of dashes in key names, Smarty3 can't handle them
  287. foreach($events as $key => $event) {
  288. $event_item = array();
  289. foreach($event['item'] as $k => $v) {
  290. $k = str_replace('-','_',$k);
  291. $event_item[$k] = $v;
  292. }
  293. $events[$key]['item'] = $event_item;
  294. }
  295. $o = replace_macros($tpl, array(
  296. '$baseurl' => $a->get_baseurl(),
  297. '$tabs' => $tabs,
  298. '$title' => t('Events'),
  299. '$new_event'=> array($a->get_baseurl().'/events/new',t('Create New Event'),'',''),
  300. '$previus' => array($a->get_baseurl()."/events/$prevyear/$prevmonth",t('Previous'),'',''),
  301. '$next' => array($a->get_baseurl()."/events/$nextyear/$nextmonth",t('Next'),'',''),
  302. '$calendar' => cal($y,$m,$links, ' eventcal'),
  303. '$events' => $events,
  304. ));
  305. if (x($_GET,'id')){ echo $o; killme(); }
  306. return $o;
  307. }
  308. if($mode === 'edit' && $event_id) {
  309. $r = q("SELECT * FROM `event` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  310. intval($event_id),
  311. intval(local_user())
  312. );
  313. if(count($r))
  314. $orig_event = $r[0];
  315. }
  316. if($mode === 'edit' || $mode === 'new') {
  317. $n_checked = ((x($orig_event) && $orig_event['nofinish']) ? ' checked="checked" ' : '');
  318. $a_checked = ((x($orig_event) && $orig_event['adjust']) ? ' checked="checked" ' : '');
  319. $t_orig = ((x($orig_event)) ? $orig_event['summary'] : '');
  320. $d_orig = ((x($orig_event)) ? $orig_event['desc'] : '');
  321. $l_orig = ((x($orig_event)) ? $orig_event['location'] : '');
  322. $eid = ((x($orig_event)) ? $orig_event['id'] : 0);
  323. $cid = ((x($orig_event)) ? $orig_event['cid'] : 0);
  324. $uri = ((x($orig_event)) ? $orig_event['uri'] : '');
  325. if(! x($orig_event))
  326. $sh_checked = '';
  327. else
  328. $sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
  329. if($cid)
  330. $sh_checked .= ' disabled="disabled" ';
  331. $tpl = get_markup_template('event_form.tpl');
  332. $sdt = ((x($orig_event)) ? $orig_event['start'] : 'now');
  333. $fdt = ((x($orig_event)) ? $orig_event['finish'] : 'now');
  334. $tz = date_default_timezone_get();
  335. if(x($orig_event))
  336. $tz = (($orig_event['adjust']) ? date_default_timezone_get() : 'UTC');
  337. $syear = datetime_convert('UTC', $tz, $sdt, 'Y');
  338. $smonth = datetime_convert('UTC', $tz, $sdt, 'm');
  339. $sday = datetime_convert('UTC', $tz, $sdt, 'd');
  340. $shour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'H') : 0);
  341. $sminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $sdt, 'i') : 0);
  342. $fyear = datetime_convert('UTC', $tz, $fdt, 'Y');
  343. $fmonth = datetime_convert('UTC', $tz, $fdt, 'm');
  344. $fday = datetime_convert('UTC', $tz, $fdt, 'd');
  345. $fhour = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'H') : 0);
  346. $fminute = ((x($orig_event)) ? datetime_convert('UTC', $tz, $fdt, 'i') : 0);
  347. $f = get_config('system','event_input_format');
  348. if(! $f)
  349. $f = 'ymd';
  350. $dateformat = datesel_format($f);
  351. $timeformat = t('hour:minute');
  352. require_once('include/acl_selectors.php');
  353. $o .= replace_macros($tpl,array(
  354. '$post' => $a->get_baseurl() . '/events',
  355. '$eid' => $eid,
  356. '$cid' => $cid,
  357. '$uri' => $uri,
  358. '$title' => t('Event details'),
  359. '$desc' => sprintf( t('Format is %s %s. Starting date and Title are required.'),$dateformat,$timeformat),
  360. '$s_text' => t('Event Starts:') . ' <span class="required" title="' . t('Required') . '">*</span>',
  361. '$s_dsel' => datesel($f,'start',$syear+5,$syear,false,$syear,$smonth,$sday),
  362. '$s_tsel' => timesel('start',$shour,$sminute),
  363. '$n_text' => t('Finish date/time is not known or not relevant'),
  364. '$n_checked' => $n_checked,
  365. '$f_text' => t('Event Finishes:'),
  366. '$f_dsel' => datesel($f,'finish',$fyear+5,$fyear,false,$fyear,$fmonth,$fday),
  367. '$f_tsel' => timesel('finish',$fhour,$fminute),
  368. '$a_text' => t('Adjust for viewer timezone'),
  369. '$a_checked' => $a_checked,
  370. '$d_text' => t('Description:'),
  371. '$d_orig' => $d_orig,
  372. '$l_text' => t('Location:'),
  373. '$l_orig' => $l_orig,
  374. '$t_text' => t('Title:') . ' <span class="required" title="' . t('Required') . '">*</span>',
  375. '$t_orig' => $t_orig,
  376. '$sh_text' => t('Share this event'),
  377. '$sh_checked' => $sh_checked,
  378. '$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user),false)),
  379. '$submit' => t('Submit')
  380. ));
  381. return $o;
  382. }
  383. }