PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/calendar.php

https://bitbucket.org/ryanhowdy/family-connections
PHP | 1204 lines | 866 code | 158 blank | 180 comment | 88 complexity | 5af236c55617adaab25796ca1ea1fb9f MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Calendar
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category FCMS
  8. * @package FamilyConnections
  9. * @author Ryan Haudenschilt <r.haudenschilt@gmail.com>
  10. * @copyright 2007 Haudenschilt LLC
  11. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
  12. * @link http://www.familycms.com/wiki/
  13. */
  14. session_start();
  15. define('URL_PREFIX', '');
  16. define('GALLERY_PREFIX', 'gallery/');
  17. require 'fcms.php';
  18. load('datetime', 'calendar');
  19. init();
  20. // Globals
  21. $calendar = new Calendar($fcmsUser->id);
  22. $TMPL = array(
  23. 'currentUserId' => $fcmsUser->id,
  24. 'sitename' => getSiteName(),
  25. 'nav-link' => getNavLinks(),
  26. 'pagetitle' => T_('Calendar'),
  27. 'path' => URL_PREFIX,
  28. 'displayname' => $fcmsUser->displayName,
  29. 'version' => getCurrentVersion(),
  30. 'year' => date('Y')
  31. );
  32. control();
  33. exit();
  34. /**
  35. * control
  36. *
  37. * The controlling structure for this script.
  38. *
  39. * @return void
  40. */
  41. function control ()
  42. {
  43. if (isset($_GET['export']))
  44. {
  45. displayExportSubmit();
  46. }
  47. elseif (isset($_GET['import']))
  48. {
  49. displayImportForm();
  50. }
  51. else if (isset($_POST['import']))
  52. {
  53. displayImportSubmit();
  54. }
  55. elseif (isset($_GET['invite']))
  56. {
  57. displayInvitationForm();
  58. }
  59. elseif (isset($_POST['submit-invite']))
  60. {
  61. displayInvitationSubmit();
  62. }
  63. elseif (isset($_GET['add']))
  64. {
  65. displayAddForm();
  66. }
  67. elseif (isset($_POST['add']))
  68. {
  69. displayAddSubmit();
  70. }
  71. elseif (isset($_GET['edit']))
  72. {
  73. displayEditForm();
  74. }
  75. elseif (isset($_POST['edit']))
  76. {
  77. displayEditSubmit();
  78. }
  79. elseif (isset($_GET['event']))
  80. {
  81. if (isset($_POST['attend_submit']))
  82. {
  83. displayAttendSubmit();
  84. }
  85. else
  86. {
  87. displayEvent();
  88. }
  89. }
  90. elseif (isset($_POST['delete']))
  91. {
  92. if (!isset($_POST['confirmed']))
  93. {
  94. displayDeleteConfirmationForm();
  95. }
  96. else
  97. {
  98. displayDeleteSubmit();
  99. }
  100. }
  101. elseif (isset($_GET['category']))
  102. {
  103. if (isset($_POST['delcat']))
  104. {
  105. displayDeleteCategorySubmit();
  106. }
  107. elseif ($_GET['category'] == 'add')
  108. {
  109. if (isset($_POST['addcat']))
  110. {
  111. displayAddCategorySubmit();
  112. }
  113. else
  114. {
  115. displayAddCategoryForm();
  116. }
  117. }
  118. elseif ($_GET['category'] == 'edit')
  119. {
  120. if (isset($_POST['editcat']))
  121. {
  122. displayEditCategorySubmit();
  123. }
  124. else
  125. {
  126. displayEditCategoryForm();
  127. }
  128. }
  129. else
  130. {
  131. displayCalendar();
  132. }
  133. }
  134. elseif (isset($_GET['view']))
  135. {
  136. displayCalendarDay();
  137. }
  138. else
  139. {
  140. displayCalendar();
  141. }
  142. }
  143. /**
  144. * displayExportSubmit
  145. *
  146. * @return void
  147. */
  148. function displayExportSubmit ()
  149. {
  150. global $calendar;
  151. $cal = $calendar->exportCalendar();
  152. $date = fixDate('Y-m-d', $calendar->tzOffset);
  153. header("Cache-control: private");
  154. header("Content-type: text/plain");
  155. header("Content-disposition: ics; filename=FCMS_Calendar_$date.ics; size=".strlen($cal));
  156. echo $cal;
  157. }
  158. /**
  159. * displayHeader
  160. *
  161. * @return void
  162. */
  163. function displayHeader ()
  164. {
  165. global $TMPL, $fcmsUser;
  166. $TMPL['javascript'] = '
  167. <script type="text/javascript" src="ui/js/livevalidation.js"></script>
  168. <link rel="stylesheet" type="text/css" href="ui/datechooser.css"/>
  169. <script type="text/javascript" src="ui/js/datechooser.js"></script>
  170. <script type="text/javascript">
  171. //<![CDATA[
  172. Event.observe(window, \'load\', function() {
  173. initChatBar(\''.T_('Chat').'\', \''.$TMPL['path'].'\');
  174. initHideAdd();
  175. initCalendarHighlight();
  176. initDisableTimes();
  177. initHideMoreDetails(\''.T_('Add More Details').'\');
  178. initCalendarClickRow();
  179. initAttendingEvent();
  180. initInviteAll();
  181. initInviteAttending();
  182. // Datpicker
  183. var objDatePicker = new DateChooser();
  184. objDatePicker.setUpdateField({\'sday\':\'j\', \'smonth\':\'n\', \'syear\':\'Y\'});
  185. objDatePicker.setIcon(\''.$TMPL['path'].'ui/themes/default/images/datepicker.jpg\', \'syear\');
  186. // Delete Confirmation
  187. if ($(\'delcal\')) {
  188. var item = $(\'delcal\');
  189. item.onclick = function() { return confirm(\''.T_('Are you sure you want to DELETE this?').'\'); };
  190. var hid = document.createElement(\'input\');
  191. hid.setAttribute(\'type\', \'hidden\');
  192. hid.setAttribute(\'name\', \'confirmed\');
  193. hid.setAttribute(\'value\', \'true\');
  194. item.insert({\'after\':hid});
  195. }
  196. return true;
  197. });
  198. //]]>
  199. </script>';
  200. include_once getTheme($fcmsUser->id).'header.php';
  201. echo '
  202. <div id="calendar-section" class="centercontent">';
  203. }
  204. /**
  205. * displayFooter
  206. *
  207. * @return void
  208. */
  209. function displayFooter ()
  210. {
  211. global $TMPL, $fcmsUser;
  212. echo '
  213. </div><!-- /calendar-section -->';
  214. include_once getTheme($fcmsUser->id).'footer.php';
  215. }
  216. /**
  217. * displayAddForm
  218. *
  219. * @return void
  220. */
  221. function displayAddForm ()
  222. {
  223. global $fcmsUser, $calendar;
  224. displayHeader();
  225. if (checkAccess($fcmsUser->id) > 5)
  226. {
  227. $calendar->displayCalendarMonth();
  228. displayFooter();
  229. return;
  230. }
  231. $date = strip_tags($_GET['add']);
  232. $calendar->displayAddForm($date);
  233. displayFooter();
  234. }
  235. /**
  236. * displayAddSubmit
  237. *
  238. * @return void
  239. */
  240. function displayAddSubmit ()
  241. {
  242. global $calendar, $fcmsUser;
  243. $timeStart = "NULL";
  244. if (isset($_POST['timestart']) and !isset($_POST['all-day']))
  245. {
  246. $timeStart = "'".escape_string($_POST['timestart'])."'";
  247. }
  248. $timeEnd = "NULL";
  249. if (isset($_POST['timeend']) and !isset($_POST['all-day']))
  250. {
  251. $timeEnd = "'".escape_string($_POST['timeend'])."'";
  252. }
  253. $repeat = "NULL";
  254. if (isset($_POST['repeat-yearly']))
  255. {
  256. $repeat = "'yearly'";
  257. }
  258. $private = 0;
  259. if (isset($_POST['private']))
  260. {
  261. $private = 1;
  262. }
  263. $invite = 0;
  264. if (isset($_POST['invite']))
  265. {
  266. $invite = 1;
  267. }
  268. // Can't make a yearly event also an invitation
  269. $notify_user_changed_event = 0;
  270. if ($repeat == "'yearly'" && $invite == 1)
  271. {
  272. // Let's turn off the invitation, submit the event and tell the user what we did
  273. $invite = 0;
  274. $notify_user_changed_event = 1;
  275. }
  276. $sql = "INSERT INTO `fcms_calendar` (
  277. `date`, `time_start`, `time_end`, `date_added`, `title`, `desc`, `created_by`,
  278. `category`, `repeat`, `private`, `invite`
  279. )
  280. VALUES (
  281. '".escape_string($_POST['date'])."',
  282. $timeStart,
  283. $timeEnd,
  284. NOW(),
  285. '".escape_string($_POST['title'])."',
  286. '".escape_string($_POST['desc'])."',
  287. '$fcmsUser->id',
  288. '".escape_string($_POST['category'])."',
  289. $repeat,
  290. '$private',
  291. '$invite'
  292. )";
  293. if (!mysql_query($sql))
  294. {
  295. displayHeader();
  296. displaySqlError($sql, mysql_error());
  297. displayFooter();
  298. return;
  299. }
  300. $id = mysql_insert_id();
  301. // Display the invitation screen
  302. if ($invite == 1)
  303. {
  304. header("Location: calendar.php?invite=$id");
  305. return;
  306. }
  307. // Finish adding, show the event
  308. displayHeader();
  309. // Did the user try to make a yearly event also an invitation?
  310. if ($notify_user_changed_event == 1)
  311. {
  312. echo '
  313. <div class="error-alert">
  314. <h3>'.T_('You cannot invite guests to a repeating event.').'</h3>
  315. <p>'.T_('Your event was created, but no invitations were sent.').'</p>
  316. <p>'.T_('Please create a new non-repeating event and invite guests to that.').'</p>
  317. </div>';
  318. }
  319. else
  320. {
  321. displayOkMessage();
  322. }
  323. $calendar->displayEvent($id);
  324. displayFooter();
  325. }
  326. /**
  327. * displayEditForm
  328. *
  329. * @return void
  330. */
  331. function displayEditForm ()
  332. {
  333. global $fcmsUser, $calendar;
  334. displayHeader();
  335. if (checkAccess($fcmsUser->id) > 5)
  336. {
  337. $calendar->displayCalendarMonth();
  338. displayFooter();
  339. return;
  340. }
  341. $id = (int)$_GET['edit'];
  342. $calendar->displayEditForm($id);
  343. displayFooter();
  344. }
  345. /**
  346. * displayEditSubmit
  347. *
  348. * @return void
  349. */
  350. function displayEditSubmit ()
  351. {
  352. global $calendar;
  353. $id = (int)$_POST['id'];
  354. $year = (int)$_POST['syear'];
  355. $month = (int)$_POST['smonth'];
  356. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  357. $day = (int)$_POST['sday'];
  358. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  359. $date = "$year-$month-$day";
  360. $title = strip_tags($_POST['title']);
  361. $title = escape_string($title);
  362. $desc = strip_tags($_POST['desc']);
  363. $desc = escape_string($desc);
  364. $category = strip_tags($_POST['category']);
  365. $category = escape_string($category);
  366. $timeStart = "NULL";
  367. $timeEnd = "NULL";
  368. $repeat = "NULL";
  369. $private = 0;
  370. $invite = 0;
  371. if (isset($_POST['timestart']) and !isset($_POST['all-day']))
  372. {
  373. $timeStart = "'".escape_string($_POST['timestart'])."'";
  374. }
  375. if (isset($_POST['timeend']) and !isset($_POST['all-day']))
  376. {
  377. $timeEnd = "'".escape_string($_POST['timeend'])."'";
  378. }
  379. if (isset($_POST['repeat-yearly']))
  380. {
  381. $repeat = "'yearly'";
  382. }
  383. if (isset($_POST['private']))
  384. {
  385. $private = 1;
  386. }
  387. if (isset($_POST['invite']))
  388. {
  389. $invite = 1;
  390. }
  391. // Can't make a yearly event also an invitation
  392. $notify_user_changed_event = 0;
  393. if ($repeat == "'yearly'" && $invite == 1)
  394. {
  395. // Let's turn off the invitation, submit the event and tell the user what we did
  396. $invite = 0;
  397. $notify_user_changed_event = 1;
  398. }
  399. $sql = "UPDATE `fcms_calendar`
  400. SET `date` = '$date',
  401. `time_start`= $timeStart,
  402. `time_end` = $timeEnd,
  403. `title` = '$title',
  404. `desc` = '$desc',
  405. `category` = '$category',
  406. `repeat` = $repeat,
  407. `private` = '$private',
  408. `invite` = '$invite'
  409. WHERE id = '$id'";
  410. if (!mysql_query($sql))
  411. {
  412. displayHeader();
  413. displaySqlError($sql, mysql_error());
  414. displayFooter();
  415. return;
  416. }
  417. // Display the invitation screen
  418. if ($invite == 1)
  419. {
  420. header("Location: calendar.php?invite=$id");
  421. return;
  422. }
  423. displayHeader();
  424. // Did the user try to make a yearly event also an invitation?
  425. if ($notify_user_changed_event == 1)
  426. {
  427. echo '
  428. <div class="error-alert">
  429. <h3>'.T_('You cannot invite guests to a repeating event.').'</h3>
  430. <p>'.T_('The changes to this event have been saved, but no invitations were sent.').'</p>
  431. <p>'.T_('Please create a new non-repeating event and invite guests to that.').'</p>
  432. </div>';
  433. $calendar->displayEvent($id);
  434. }
  435. else
  436. {
  437. displayOkMessage();
  438. $calendar->displayCalendarMonth();
  439. }
  440. displayFooter();
  441. }
  442. /**
  443. * displayEvent
  444. *
  445. * @return void
  446. */
  447. function displayEvent ()
  448. {
  449. global $fcmsUser, $calendar;
  450. displayHeader();
  451. if (checkAccess($fcmsUser->id) > 5)
  452. {
  453. $calendar->displayCalendarMonth();
  454. displayFooter();
  455. return;
  456. }
  457. if (ctype_digit($_GET['event']))
  458. {
  459. $id = (int)$_GET['event'];
  460. $calendar->displayEvent($id);
  461. }
  462. elseif (strlen($_GET['event']) >= 8 && substr($_GET['event'], 0, 8) == 'birthday')
  463. {
  464. $id = substr($_GET['event'], 8);
  465. $id = (int)$id;
  466. $calendar->displayBirthdayEvent($id);
  467. }
  468. else
  469. {
  470. echo '<div class="info-alert"><h2>'.T_('I can\'t seem to find that calendar event.').'</h2>';
  471. echo '<p>'.T_('Please double check and try again.').'</p></div>';
  472. }
  473. displayFooter();
  474. }
  475. /**
  476. * displayImportForm
  477. *
  478. * @return void
  479. */
  480. function displayImportForm ()
  481. {
  482. global $calendar;
  483. displayHeader();
  484. $calendar->displayImportForm();
  485. displayFooter();
  486. }
  487. /**
  488. * displayImportSubmit
  489. *
  490. * @return void
  491. */
  492. function displayImportSubmit ()
  493. {
  494. global $calendar;
  495. displayHeader();
  496. $file_name = $_FILES["file"]["tmp_name"];
  497. if ($calendar->importCalendar($file_name))
  498. {
  499. displayOkMessage();
  500. $calendar->displayCalendarMonth();
  501. }
  502. displayFooter();
  503. }
  504. /**
  505. * displayDeleteConfirmationForm
  506. *
  507. * @return void
  508. */
  509. function displayDeleteConfirmationForm ()
  510. {
  511. displayHeader();
  512. echo '
  513. <div class="info-alert">
  514. <form action="calendar.php" method="post">
  515. <h2>'.T_('Are you sure you want to DELETE this?').'</h2>
  516. <p><b><i>'.T_('This can NOT be undone.').'</i></b></p>
  517. <div>
  518. <input type="hidden" name="id" value="'.(int)$_POST['id'].'"/>
  519. <input type="hidden" name="confirmed" value="1"/>
  520. <input style="float:left;" type="submit" id="delconfirm" name="delete" value="'.T_('Yes').'"/>
  521. <a style="float:right;" href="calendar.php">'.T_('Cancel').'</a>
  522. </div>
  523. </form>
  524. </div>';
  525. displayFooter();
  526. }
  527. /**
  528. * displayDeleteSubmit
  529. *
  530. * @return void
  531. */
  532. function displayDeleteSubmit ()
  533. {
  534. global $calendar;
  535. displayHeader();
  536. $sql = "DELETE FROM `fcms_calendar`
  537. WHERE id = '".(int)$_POST['id']."'";
  538. if (!mysql_query($sql))
  539. {
  540. displaySqlError($sql, mysql_error());
  541. displayFooter();
  542. return;
  543. }
  544. displayOkMessage(T_('Calendar Entry Deleted Successfully.'));
  545. $calendar->displayCalendarMonth();
  546. displayFooter();
  547. }
  548. /**
  549. * displayAddCategoryForm
  550. *
  551. * @return void
  552. */
  553. function displayAddCategoryForm ()
  554. {
  555. global $calendar;
  556. displayHeader();
  557. $calendar->displayCategoryForm();
  558. displayFooter();
  559. }
  560. /**
  561. * displayAddCategorySubmit
  562. *
  563. * @return void
  564. */
  565. function displayAddCategorySubmit ()
  566. {
  567. global $calendar, $fcmsUser;
  568. displayHeader();
  569. $name = strip_tags($_POST['name']);
  570. $name = escape_string($name);
  571. $colors = 'none';
  572. if (isset($_POST['colors']))
  573. {
  574. $colors = escape_string($_POST['colors']);
  575. }
  576. $sql = "INSERT INTO `fcms_category` (`name`, `type`, `user`, `date`, `color`)
  577. VALUES (
  578. '$name',
  579. 'calendar',
  580. '$fcmsUser->id',
  581. NOW(),
  582. '$colors'
  583. )";
  584. if (!mysql_query($sql))
  585. {
  586. displaySqlError($sql, mysql_error());
  587. displayFooter();
  588. return;
  589. }
  590. displayOkMessage();
  591. $calendar->displayCalendarMonth();
  592. displayFooter();
  593. }
  594. /**
  595. * displayEditCategorySubmit
  596. *
  597. * @return void
  598. */
  599. function displayEditCategorySubmit ()
  600. {
  601. global $calendar;
  602. displayHeader();
  603. $id = (int)$_POST['id'];
  604. $name = strip_tags($_POST['name']);
  605. $name = escape_string($name);
  606. $colors = strip_tags($_POST['colors']);
  607. $colors = escape_string($colors);
  608. $sql = "UPDATE `fcms_category`
  609. SET
  610. `name` = '$name',
  611. `color` = '$colors'
  612. WHERE `id` = '$id'";
  613. if (!mysql_query($sql))
  614. {
  615. displaySqlError($sql, mysql_error());
  616. displayFooter();
  617. return;
  618. }
  619. displayOkMessage();
  620. $calendar->displayCalendarMonth();
  621. displayFooter();
  622. }
  623. /**
  624. * displayEditCategoryForm
  625. *
  626. * @return void
  627. */
  628. function displayEditCategoryForm ()
  629. {
  630. global $calendar;
  631. displayHeader();
  632. $id = (int)$_GET['id'];
  633. $calendar->displayCategoryForm($id);
  634. displayFooter();
  635. }
  636. /**
  637. * displayDeleteCategorySubmit
  638. *
  639. * @return void
  640. */
  641. function displayDeleteCategorySubmit ()
  642. {
  643. global $calendar;
  644. displayHeader();
  645. $sql = "DELETE FROM `fcms_category`
  646. WHERE `id` = '".(int)$_POST['id']."'";
  647. if (!mysql_query($sql))
  648. {
  649. displaySqlError($sql, mysql_error());
  650. displayFooter();
  651. return;
  652. }
  653. displayOkMessage();
  654. $calendar->displayCalendarMonth();
  655. displayFooter();
  656. }
  657. /**
  658. * displayCalendarDay
  659. *
  660. * @return void
  661. */
  662. function displayCalendarDay ()
  663. {
  664. global $calendar;
  665. displayHeader();
  666. $year = (int)$_GET['year'];
  667. $month = (int)$_GET['month'];
  668. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  669. $day = (int)$_GET['day'];
  670. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  671. $calendar->displayCalendarDay($month, $year, $day);
  672. displayFooter();
  673. }
  674. /**
  675. * displayCalendar
  676. *
  677. * @return void
  678. */
  679. function displayCalendar ()
  680. {
  681. global $calendar;
  682. displayHeader();
  683. // Use the supplied date, if available
  684. if (isset($_GET['year']) && isset($_GET['month']) && isset($_GET['day']))
  685. {
  686. $year = (int)$_GET['year'];
  687. $month = (int)$_GET['month'];
  688. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  689. $day = (int)$_GET['day'];
  690. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  691. $calendar->displayCalendarMonth($month, $year, $day);
  692. }
  693. // use today's date
  694. else
  695. {
  696. $calendar->displayCalendarMonth();
  697. }
  698. displayFooter();
  699. }
  700. /**
  701. * displayInvitationForm
  702. *
  703. * Used for both creating and editing an invitation.
  704. *
  705. * @param int $calendarId The calendar entry id
  706. * @param int $errors Any errors from previous form
  707. *
  708. * @return void
  709. */
  710. function displayInvitationForm ($calendarId = 0, $errors = 0)
  711. {
  712. global $fcmsUser;
  713. displayHeader();
  714. $calendarId = (int)$calendarId;
  715. if (isset($_GET['invite']))
  716. {
  717. $calendarId = (int)$_GET['invite'];
  718. }
  719. if ($calendarId == 0)
  720. {
  721. echo '<p class="error-alert">'.T_('Invalid ID.').'</p>';
  722. displayFooter();
  723. return;
  724. }
  725. // Get calendar invite options
  726. $sql = "SELECT `id`, `date`, `time_start`, `time_end`, `date_added`,
  727. `title`, `desc`, `created_by`, `category`, `repeat`, `private`
  728. FROM `fcms_calendar`
  729. WHERE `id` = '$calendarId'
  730. LIMIT 1";
  731. $result = mysql_query($sql);
  732. if (!$result)
  733. {
  734. displaySqlError($sql, mysql_error());
  735. displayFooter();
  736. return;
  737. }
  738. $event = mysql_fetch_array($result);
  739. // only creator, or admin can edit this invitation
  740. if ($event['created_by'] != $fcmsUser->id && getAccessLevel($fcmsUser->id) > 1)
  741. {
  742. echo '<p class="error-alert">'.T_('You do not have permission to perform this task.').'</p>';
  743. displayFooter();
  744. return;
  745. }
  746. // Get members
  747. $sql = "SELECT `id`, `email`
  748. FROM `fcms_users`
  749. WHERE `activated` > 0
  750. AND `password` != 'NONMEMBER'";
  751. $result = mysql_query($sql);
  752. if (!$result)
  753. {
  754. displaySqlError($sql, mysql_error());
  755. displayFooter();
  756. return;
  757. }
  758. while ($r = mysql_fetch_array($result))
  759. {
  760. $members[$r['id']] = array(
  761. 'name' => getUserDisplayName($r['id'], 2),
  762. 'email' => $r['email']
  763. );
  764. }
  765. asort($members);
  766. $rows = '';
  767. foreach ($members as $id => $arr)
  768. {
  769. if ($id == $fcmsUser->id)
  770. {
  771. continue;
  772. }
  773. $rows .= '<tr>';
  774. $rows .= '<td class="chk"><input type="checkbox" id="member'.(int)$id.'" name="member[]" value="'.(int)$id.'"/></td>';
  775. $rows .= '<td>'.cleanOutput($members[$id]['name']).'</td>';
  776. $rows .= '<td>'.cleanOutput($members[$id]['email']);
  777. $rows .= '<input type="hidden" name="id'.(int)$id.'" value="'.cleanOutput($members[$id]['email']).'"/></td></tr>';
  778. }
  779. // Display the form
  780. echo '
  781. <form id="invite-form" method="post" action="calendar.php?event='.$calendarId.'">
  782. <fieldset>
  783. <legend><span>'.T_('Choose Guests').'</span></legend>
  784. <h3>'.T_('Invite Members').'</h3>
  785. <p>
  786. <input type="checkbox" id="all-members" name="all-members" value="yes"/>
  787. <label for="all-members">'.T_('Invite all Members?').'</label>
  788. </p>
  789. <div id="invite-members-list">
  790. <table id="invite-table" cellspacing="0" cellpadding="0">
  791. <thead>
  792. <tr>
  793. <th class="chk"></td>
  794. <th>'.T_('Name').'</td>
  795. <th>'.T_('Email').'</td>
  796. </tr>
  797. </thead>
  798. <tbody>
  799. '.$rows.'
  800. </tbody>
  801. </table>
  802. </div>
  803. <h3>'.T_('Invite Non Members').'</h3>
  804. <span>'.T_('Enter list of emails to invite. One email per line.').'</span>
  805. <textarea name="non-member-emails" id="non-member-emails" rows="10" cols="63"></textarea>
  806. <p style="clear:both">
  807. <input type="hidden" name="calendar" value="'.$calendarId.'"/>
  808. <input class="sub1" type="submit" id="submit-invite" name="submit-invite" value="'.T_('Send Invitations').'"/>
  809. '.T_('or').'&nbsp;
  810. <a href="calendar.php">'.T_('Cancel').'</a>
  811. </p>
  812. </fieldset>
  813. </form>';
  814. displayFooter();
  815. }
  816. /**
  817. * displayInvitationSubmit
  818. *
  819. * @return void
  820. */
  821. function displayInvitationSubmit ()
  822. {
  823. global $fcmsUser, $calendar;
  824. displayHeader();
  825. $calendarId = (int)$_POST['calendar'];
  826. // make sure the user submitted atleast one email address
  827. if (!isset($_POST['all-members']) && !isset($_POST['email']) && !isset($_POST['non-member-emails']))
  828. {
  829. $error = T_('You must invite at least one guest.');
  830. displayInvitationForm($calendarId, $error);
  831. return;
  832. }
  833. // Get any invitations already sent for this event
  834. $invitations = getInvitations($calendarId, true);
  835. if ($invitations === false)
  836. {
  837. displayFooter();
  838. return;
  839. }
  840. if (!isset($invitations['_current_user']))
  841. {
  842. // add the current user (host) to the invite as attending
  843. $sql = "INSERT INTO `fcms_invitation` (`event_id`, `user`, `created`, `updated`, `attending`)
  844. VALUES ('$calendarId', '$fcmsUser->id', NOW(), NOW(), 1)";
  845. if (!mysql_query($sql))
  846. {
  847. displaySqlError($sql, mysql_error());
  848. displayFooter();
  849. return;
  850. }
  851. }
  852. // Get the calendar event title
  853. $sql = "SELECT `title` FROM `fcms_calendar` WHERE `id` = '$calendarId'";
  854. $result = mysql_query($sql);
  855. if (!$result)
  856. {
  857. displaySqlError($sql, mysql_error());
  858. displayFooter();
  859. return;
  860. }
  861. $r = mysql_fetch_array($result);
  862. $title = $r['title'];
  863. $invitees = array();
  864. $nonMembers = array();
  865. $members = array();
  866. // get emails from textarea
  867. if (isset($_POST['non-member-emails']))
  868. {
  869. $nonMembers = explode("\n", $_POST['non-member-emails']);
  870. }
  871. // get any members that have been invited
  872. if (isset($_POST['all-members']))
  873. {
  874. $sql = "SELECT `id`, `email`
  875. FROM `fcms_users`
  876. WHERE `activated` > 0
  877. AND `password` != 'NONMEMBER'
  878. AND `id` != $fcmsUser->id";
  879. $result = mysql_query($sql);
  880. if (!$result)
  881. {
  882. displaySqlError($sql, mysql_error());
  883. displayFooter();
  884. return;
  885. }
  886. while ($r = mysql_fetch_array($result))
  887. {
  888. array_push($members, array('id' => $r['id'], 'email' => $r['email']));
  889. }
  890. }
  891. elseif (isset($_POST['member']))
  892. {
  893. foreach ($_POST['member'] as $id)
  894. {
  895. array_push($members, array('id' => $id, 'email' => $_POST["id$id"]));
  896. }
  897. }
  898. // merge all emails into one big list
  899. $invitees = array_merge($nonMembers, $members);
  900. // Create the invite and send the emails to each invitee
  901. foreach ($invitees as $invitee)
  902. {
  903. if (empty($invitee))
  904. {
  905. continue;
  906. }
  907. // create a code for this user
  908. $code = uniqid('');
  909. $user = 0;
  910. $email = '';
  911. $toEmail = '';
  912. $toName = '';
  913. $fromName = getUserDisplayName($fcmsUser->id);
  914. $url = getDomainAndDir();
  915. // member
  916. if (is_array($invitee))
  917. {
  918. $user = (int)$invitee['id'];
  919. $toEmail = rtrim($invitee['email']);
  920. $toName = getUserDisplayName($user);
  921. $email = "NULL";
  922. $url .= 'calendar.php?event='.$calendarId;
  923. }
  924. // non member
  925. else
  926. {
  927. $user = 0;
  928. $toEmail = rtrim($invitee);
  929. $toName = $toEmail;
  930. $email = escape_string($toEmail);
  931. $email = "'$email'";
  932. $url .= 'invitation.php?event='.$calendarId.'&code='.$code;
  933. }
  934. // Skip email address that have already been invited
  935. if (isset($invitations[$toEmail]))
  936. {
  937. continue;
  938. }
  939. // add an invitation to db
  940. $sql = "INSERT INTO `fcms_invitation` (`event_id`, `user`, `email`, `created`, `updated`, `code`)
  941. VALUES ('$calendarId', '$user', $email, NOW(), NOW(), '$code')";
  942. if (!mysql_query($sql))
  943. {
  944. displaySqlError($sql, mysql_error());
  945. displayFooter();
  946. return;
  947. }
  948. // Send email invitation
  949. $subject = sprintf(T_pgettext('%s is the title of an event', 'Invitation: %s'), $title);
  950. $msg = sprintf(T_pgettext('%s is the name of a person, like Dear Bob,', 'Dear %s,'), $toName).'
  951. '.sprintf(T_pgettext('The first %s is the name of a person, the second is the title of an event', '%s has invited you to %s.'), $fromName, $title).'
  952. '.T_('Please visit the link below to view the rest of this invitation.').'
  953. '.$url.'
  954. ----
  955. '.T_('This is an automated response, please do not reply.').'
  956. ';
  957. $email_headers = getEmailHeaders();
  958. mail($toEmail, $subject, $msg, $email_headers);
  959. }
  960. displayOkMessage();
  961. $calendar->displayEvent($calendarId);
  962. displayFooter();
  963. }
  964. /**
  965. * displayAttendSubmit
  966. *
  967. * When a user submits the form telling whether they will be
  968. * attending an event or not.
  969. *
  970. * @return void
  971. */
  972. function displayAttendSubmit ()
  973. {
  974. global $fcmsUser, $calendar;
  975. displayHeader();
  976. $calendarId = (int)$_GET['event'];
  977. $id = (int)$_POST['id'];
  978. $attending = isset($_POST['attending']) ? (int)$_POST['attending'] : "NULL";
  979. $response = escape_string($_POST['response']);
  980. $sql = "UPDATE `fcms_invitation`
  981. SET `response` = '$response',
  982. `attending` = $attending,
  983. `updated` = NOW()
  984. WHERE `id` = '$id'";
  985. if (!mysql_query($sql))
  986. {
  987. displaySqlError($sql, mysql_error());
  988. displayFooter();
  989. exit();
  990. }
  991. $calendar->displayEvent($calendarId);
  992. displayFooter();
  993. }
  994. /**
  995. * getInvitations
  996. *
  997. * Returns an array of invitations that have been sent for this event.
  998. * Including whether or not the invitee has responded.
  999. *
  1000. * Will also add a key of _current_user if the current user is included.
  1001. *
  1002. * @param int $eventId The calendar event id
  1003. * @param boolean $keyByEmail Whether or not to key the array by email or 0,1,2 etc.
  1004. *
  1005. * @return array
  1006. */
  1007. function getInvitations ($eventId, $keyByEmail = false)
  1008. {
  1009. global $fcmsUser;
  1010. $sql = "SELECT i.`id`, i.`user`, i.`email`, i.`attending`, i.`response`, i.`updated`,
  1011. u.`email` AS user_email
  1012. FROM `fcms_invitation` AS i
  1013. LEFT JOIN `fcms_users` AS u
  1014. ON i.`user` = u.`id`
  1015. WHERE `event_id` = '$eventId'
  1016. ORDER BY `updated` DESC";
  1017. $result = mysql_query($sql);
  1018. if (!$result)
  1019. {
  1020. displaySqlError($sql, mysql_error());
  1021. return false;
  1022. }
  1023. $data = array();
  1024. while ($r = mysql_fetch_assoc($result))
  1025. {
  1026. if ($fcmsUser->id == $r['user'])
  1027. {
  1028. $data['_current_user'] = $r;
  1029. }
  1030. if ($keyByEmail)
  1031. {
  1032. $email = isset($r['email']) ? $r['email'] : $r['user_email'];
  1033. $data[$email] = $r;
  1034. }
  1035. else
  1036. {
  1037. $data[] = $r;
  1038. }
  1039. }
  1040. return $data;
  1041. }