Advertisement
kustomdesigner

bp-group-calendar modified file

Jan 30th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 62.56 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. BP Group Calendar
  6.  
  7. */
  8.  
  9.  
  10.  
  11. //------------------------------------------------------------------------//
  12.  
  13.  
  14.  
  15. //---Config---------------------------------------------------------------//
  16.  
  17.  
  18.  
  19. //------------------------------------------------------------------------//
  20.  
  21.  
  22.  
  23. //include calendar class
  24.  
  25. require_once(WP_PLUGIN_DIR . '/bp-group-calendar/groupcalendar/calendar.class.php');
  26.  
  27.  
  28.  
  29. //------------------------------------------------------------------------//
  30.  
  31.  
  32.  
  33. //---Hook-----------------------------------------------------------------//
  34.  
  35.  
  36.  
  37. //------------------------------------------------------------------------//
  38.  
  39.  
  40.  
  41. //check for activating
  42.  
  43.  
  44.  
  45. add_action( 'admin_init', 'bp_group_calendar_make_current' );
  46.  
  47.  
  48.  
  49.  
  50.  
  51. add_action( 'groups_group_after_save', 'bp_group_calendar_settings_save' );
  52.  
  53. add_action( 'bp_after_group_settings_creation_step', 'bp_group_calendar_settings');
  54.  
  55. add_action( 'bp_after_group_settings_admin', 'bp_group_calendar_settings');
  56.  
  57. add_action( 'wp_enqueue_scripts', 'bp_group_calendar_js');
  58.  
  59. add_action( 'wp_head', 'bp_group_calendar_js_output');
  60.  
  61. add_action( 'groups_screen_notification_settings', 'bp_group_calendar_notification_settings' );
  62.  
  63.  
  64.  
  65. //activity stream
  66.  
  67. add_action( 'groups_register_activity_actions', 'bp_group_calendar_reg_activity' );
  68.  
  69. add_action( 'groups_new_calendar_event', 'groups_update_last_activity' );
  70.  
  71. add_action( 'groups_edit_calendar_event', 'groups_update_last_activity' );
  72.  
  73.  
  74.  
  75.  
  76.  
  77. //widgets
  78.  
  79. add_action( 'widgets_init', create_function('', 'return register_widget("BP_Group_Calendar_Widget");') );
  80.  
  81. add_action( 'widgets_init', create_function('', 'return register_widget("BP_Group_Calendar_Widget_Single");') );
  82.  
  83. add_action( 'widgets_init', create_function('', 'return register_widget("BP_Group_Calendar_Widget_User_Groups");') );
  84.  
  85.  
  86.  
  87. //------------------------------------------------------------------------//
  88.  
  89.  
  90.  
  91. //---Functions------------------------------------------------------------//
  92.  
  93.  
  94.  
  95. //------------------------------------------------------------------------//
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. function bp_group_calendar_make_current() {
  104.  
  105.  
  106.  
  107. global $wpdb, $bp_group_calendar_current_version;
  108.  
  109.  
  110.  
  111. if (get_site_option( "bp_group_calendar_version" ) == '') {
  112.  
  113. add_site_option( 'bp_group_calendar_version', '0.0.0' );
  114.  
  115. }
  116.  
  117.  
  118.  
  119. if (get_site_option( "bp_group_calendar_version" ) == $bp_group_calendar_current_version) {
  120.  
  121. // do nothing
  122.  
  123. } else {
  124.  
  125. //update to current version
  126.  
  127. update_site_option( "bp_group_calendar_installed", "no" );
  128.  
  129. update_site_option( "bp_group_calendar_version", $bp_group_calendar_current_version );
  130.  
  131. }
  132.  
  133. bp_group_calendar_global_install();
  134.  
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141. function bp_group_calendar_global_install() {
  142.  
  143.  
  144.  
  145. global $wpdb, $bp_group_calendar_current_version;
  146.  
  147.  
  148.  
  149. if (get_site_option( "bp_group_calendar_installed" ) == '') {
  150.  
  151. add_site_option( 'bp_group_calendar_installed', 'no' );
  152.  
  153. }
  154.  
  155.  
  156.  
  157. if (get_site_option( "bp_group_calendar_installed" ) == "yes") {
  158.  
  159. // do nothing
  160.  
  161. } else {
  162.  
  163. $bp_group_calendar_table1 = "CREATE TABLE IF NOT EXISTS `" . $wpdb->base_prefix . "bp_groups_calendars` (
  164.  
  165. `id` bigint(20) unsigned NOT NULL auto_increment,
  166.  
  167. `group_id` bigint(20) NOT NULL default '0',
  168.  
  169. `user_id` bigint(20) NOT NULL default '0',
  170.  
  171. `event_time` DATETIME NOT NULL,
  172.  
  173. `event_title` TEXT NOT NULL,
  174.  
  175. `event_description` TEXT,
  176.  
  177. `event_location` TEXT,
  178.  
  179. `event_map` BOOL NOT NULL,
  180.  
  181. `created_stamp` bigint(30) NOT NULL,
  182.  
  183. `last_edited_id` bigint(20) NOT NULL default '0',
  184.  
  185. `last_edited_stamp` bigint(30) NOT NULL,
  186.  
  187. PRIMARY KEY (`id`)
  188.  
  189. ) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci;";
  190.  
  191.  
  192.  
  193. $wpdb->query( $bp_group_calendar_table1 );
  194.  
  195. update_site_option( "bp_group_calendar_installed", "yes" );
  196.  
  197. }
  198.  
  199. }
  200.  
  201.  
  202.  
  203. //extend the group
  204.  
  205. class BP_Group_Calendar_Extension extends BP_Group_Extension {
  206.  
  207.  
  208.  
  209. var $visibility = 'private'; // 'public' will show your extension to non-group members, 'private' means you have to be a member of the group to view your extension.
  210.  
  211. var $enable_create_step = false; // If your extension does not need a creation step, set this to false
  212.  
  213. var $enable_nav_item = false; // If your extension does not need a navigation item, set this to false
  214.  
  215. var $enable_edit_item = false; // If your extension does not need an edit screen, set this to false
  216.  
  217.  
  218.  
  219. function bp_group_calendar_extension() {
  220.  
  221. global $bp;
  222.  
  223.  
  224.  
  225. $this->name = __('Calendar', 'groupcalendar');
  226.  
  227. $this->slug = 'calendar';
  228.  
  229. $this->enable_nav_item = isset($bp->groups->current_group->user_has_access) ? $bp->groups->current_group->user_has_access : false;
  230.  
  231.  
  232.  
  233. //$this->create_step_position = 21;
  234.  
  235. $this->nav_item_position = 36;
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242.  
  243. function create_screen() { }
  244.  
  245.  
  246.  
  247. function create_screen_save() { }
  248.  
  249.  
  250.  
  251. function edit_screen() { }
  252.  
  253.  
  254.  
  255. function edit_screen_save() { }
  256.  
  257.  
  258.  
  259. function display() {
  260.  
  261. global $bp;
  262.  
  263. $event_id = bp_group_calendar_event_url_parse();
  264.  
  265.  
  266.  
  267. $edit_event = bp_group_calendar_event_is_edit();
  268.  
  269.  
  270.  
  271. bp_group_calendar_event_is_delete();
  272.  
  273.  
  274.  
  275. if (bp_group_calendar_event_save()===false)
  276.  
  277. $edit_event = true;
  278.  
  279.  
  280.  
  281. $date = bp_group_calendar_url_parse();
  282.  
  283.  
  284.  
  285. do_action( 'template_notices' );
  286.  
  287.  
  288.  
  289. if ($edit_event) {
  290.  
  291.  
  292.  
  293. //show edit event form
  294.  
  295. if (!bp_group_calendar_widget_edit_event($event_id)) {
  296.  
  297.  
  298.  
  299. //default to current month view
  300.  
  301. bp_group_calendar_widget_month($date);
  302.  
  303. bp_group_calendar_widget_upcoming_events();
  304.  
  305. bp_group_calendar_widget_my_events();
  306.  
  307. bp_group_calendar_widget_create_event($date);
  308.  
  309.  
  310.  
  311. }
  312.  
  313.  
  314.  
  315. } else if ($event_id) {
  316.  
  317.  
  318.  
  319. //display_event
  320.  
  321. bp_group_calendar_widget_event_display($event_id);
  322.  
  323.  
  324.  
  325. //current month view
  326.  
  327. bp_group_calendar_widget_month($date);
  328.  
  329.  
  330.  
  331. } else if ( isset($date['year']) && !isset($date['month']) && !isset($date['day']) ) {
  332.  
  333.  
  334.  
  335. //year view
  336.  
  337. bp_group_calendar_widget_year($date);
  338.  
  339.  
  340.  
  341. bp_group_calendar_widget_create_event($date);
  342.  
  343.  
  344.  
  345. } else if ( isset($date['year']) && isset($date['month']) && !isset($date['day']) ) {
  346.  
  347.  
  348.  
  349. //month view
  350.  
  351. bp_group_calendar_widget_month($date);
  352.  
  353.  
  354.  
  355. bp_group_calendar_widget_create_event($date);
  356.  
  357.  
  358.  
  359. } else if ( isset($date['year']) && isset($date['month']) && isset($date['day']) ) {
  360.  
  361.  
  362.  
  363. //day view
  364.  
  365. bp_group_calendar_widget_day($date);
  366.  
  367.  
  368.  
  369. bp_group_calendar_widget_create_event($date);
  370.  
  371.  
  372.  
  373. } else {
  374.  
  375.  
  376.  
  377. //default to current month view
  378.  
  379. bp_group_calendar_widget_month($date);
  380.  
  381.  
  382.  
  383. bp_group_calendar_widget_upcoming_events();
  384.  
  385.  
  386.  
  387. bp_group_calendar_widget_my_events();
  388.  
  389.  
  390.  
  391. bp_group_calendar_widget_create_event($date);
  392.  
  393.  
  394.  
  395. }
  396.  
  397. }
  398.  
  399.  
  400.  
  401. function widget_display() {
  402.  
  403. bp_group_calendar_widget_upcoming_events();
  404.  
  405. }
  406.  
  407. }
  408.  
  409. bp_register_group_extension( 'BP_Group_Calendar_Extension' );
  410.  
  411.  
  412.  
  413.  
  414.  
  415. function bp_group_calendar_settings_save($group) {
  416.  
  417. global $wpdb;
  418.  
  419.  
  420.  
  421. if ( isset($_POST['group-calendar-moderator-capabilities']) ) {
  422.  
  423. groups_update_groupmeta( $group->id, 'group_calendar_moderator_capabilities', $_POST['group-calendar-moderator-capabilities']);
  424.  
  425. }
  426.  
  427.  
  428.  
  429. if ( isset($_POST['group-calendar-member-capabilities']) ) {
  430.  
  431. groups_update_groupmeta( $group->id, 'group_calendar_member_capabilities', $_POST['group-calendar-member-capabilities']);
  432.  
  433. }
  434.  
  435.  
  436.  
  437. if ( isset($_POST['group-calendar-send-email']) ) {
  438.  
  439. groups_update_groupmeta( $group->id, 'group_calendar_send_email', $_POST['group-calendar-send-email']);
  440.  
  441. }
  442.  
  443. }
  444.  
  445.  
  446.  
  447.  
  448.  
  449. function bp_group_calendar_event_save() {
  450.  
  451. global $wpdb, $current_user;
  452.  
  453.  
  454.  
  455. $calendar_capabilities = bp_group_calendar_get_capabilities();
  456.  
  457.  
  458.  
  459. if (isset($_POST['create-event'])) {
  460.  
  461. if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'bp_group_calendar')) {
  462.  
  463. bp_core_add_message( __('There was a security problem', 'groupcalendar'), 'error' );
  464.  
  465. return false;
  466.  
  467. }
  468.  
  469.  
  470.  
  471. //reject unqualified users
  472.  
  473. if ($calendar_capabilities == 'none') {
  474.  
  475. bp_core_add_message( __("You don't have permission to edit events", 'groupcalendar'), 'error' );
  476.  
  477. return false;
  478.  
  479. }
  480.  
  481.  
  482.  
  483. //prepare fields
  484.  
  485. $group_id = (int)$_POST['group-id'];
  486.  
  487. $event_title = esc_attr(strip_tags(trim($_POST['event-title'])));
  488.  
  489.  
  490.  
  491. //check that required title isset after filtering
  492.  
  493. if (empty($event_title)) {
  494.  
  495. bp_core_add_message( __("An event title is required", 'groupcalendar'), 'error' );
  496.  
  497. return false;
  498.  
  499. }
  500.  
  501.  
  502.  
  503. $tmp_date = $_POST['event-date'].' '.$_POST['event-hour'].':'.$_POST['event-minute'].$_POST['event-ampm'];
  504.  
  505. $tmp_date = strtotime($tmp_date);
  506.  
  507. //check for valid date/time
  508.  
  509. if ($tmp_date && strtotime($_POST['event-date']) && strtotime($_POST['event-date']) != -1) {
  510.  
  511. $event_date = date_i18n('Y-m-d H:i:s', $tmp_date);
  512.  
  513. } else {
  514.  
  515. bp_core_add_message( __("Please enter a valid event date.", 'groupcalendar'), 'error' );
  516.  
  517. return false;
  518.  
  519. }
  520.  
  521.  
  522.  
  523. $event_description = wp_filter_post_kses(wpautop($_POST['event-desc']));
  524.  
  525. $event_location = esc_attr(strip_tags(trim($_POST['event-loc'])));
  526.  
  527. // EVENT STATE SELECTOR CUSTOM FIELD
  528.  
  529. $event_state = $_POST["plugin_state"];
  530.  
  531. if(empty($event_state)) {
  532.  
  533. bp_core_add_message( __("State field is missing.", 'groupcalendar'), 'error' );
  534.  
  535. return false;
  536. }
  537.  
  538. $event_map = ($_POST['event-map']==1) ? 1 : 0;
  539.  
  540.  
  541.  
  542. //editing previous event
  543.  
  544. if (isset($_POST['event-id'])) {
  545.  
  546.  
  547.  
  548. //can user modify this event?
  549.  
  550. if ($calendar_capabilities == 'limited') {
  551.  
  552. $creator_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM " . $wpdb->base_prefix . "bp_groups_calendars WHERE id = %d AND group_id = %d", (int)$_POST['event-id'], $group_id) );
  553.  
  554.  
  555.  
  556. if ($creator_id != $current_user->ID) {
  557.  
  558. bp_core_add_message( __("You don't have permission to edit that event", 'groupcalendar'), 'error' );
  559.  
  560. return false;
  561.  
  562. }
  563.  
  564. }
  565.  
  566.  
  567.  
  568. $query = $wpdb->prepare("UPDATE " . $wpdb->base_prefix . "bp_groups_calendars
  569.  
  570. SET event_time = %s, event_title = %s, event_description = %s, event_location = %s, event_map = %d, last_edited_id = %d, last_edited_stamp = %d
  571.  
  572. WHERE id = %d AND group_id = %d LIMIT 1",
  573.  
  574. $event_date, $event_title, $event_description, $event_location, $event_map, $current_user->ID, time(), (int)$_POST['event-id'], $group_id );
  575.  
  576.  
  577.  
  578. if ($wpdb->query($query)) {
  579.  
  580. bp_core_add_message( __("Event saved", 'groupcalendar') );
  581.  
  582.  
  583.  
  584. //record activity
  585.  
  586. bp_group_calendar_event_add_action_message(false, (int)$_POST['event-id'], $event_date, $event_title);
  587.  
  588.  
  589.  
  590. return true;
  591.  
  592. } else {
  593.  
  594. bp_core_add_message( __("There was a problem saving to the DB", 'groupcalendar'), 'error' );
  595.  
  596. return false;
  597.  
  598. }
  599.  
  600.  
  601.  
  602. } else { //new event
  603.  
  604. /*Convert selected state to a string*/
  605.  
  606. $event_state = array_pop($event_state);
  607.  
  608. /*ADDED EVENT_STATE TO PLUGIN DATABASE ENTRY FOR NEW EVENTS*/
  609.  
  610. $query = $wpdb->prepare("INSERT INTO " . $wpdb->base_prefix . "bp_groups_calendars
  611.  
  612. ( group_id, user_id, event_time, event_title, event_description, event_location, event_map, event_state, created_stamp, last_edited_id, last_edited_stamp )
  613.  
  614. VALUES ( %d, %d, %s, %s, %s, %s, %d, %s, %d, %d, %d )",
  615.  
  616. $group_id, $current_user->ID, $event_date, $event_title, $event_description, $event_location, $event_map, $event_state, time(), $current_user->ID, time() );
  617.  
  618.  
  619.  
  620. if ($wpdb->query($query)) {
  621.  
  622. $new_id = $wpdb->insert_id;
  623.  
  624. bp_core_add_message( __("Event saved", 'groupcalendar') );
  625.  
  626. bp_group_calendar_event_add_action_message(true, $new_id, $event_date, $event_title);
  627.  
  628.  
  629.  
  630. //email group members
  631.  
  632. bp_group_calendar_event_email(true, $new_id, $event_date, $event_title);
  633.  
  634.  
  635.  
  636. return true;
  637.  
  638. } else {
  639.  
  640. bp_core_add_message( __("There was a problem saving to the DB", 'groupcalendar'), 'error' );
  641.  
  642. return false;
  643.  
  644. }
  645.  
  646.  
  647.  
  648. }
  649.  
  650.  
  651.  
  652. }
  653.  
  654. }
  655.  
  656.  
  657.  
  658. //register activities
  659.  
  660. function bp_group_calendar_reg_activity() {
  661.  
  662. global $bp;
  663.  
  664. bp_activity_set_action( $bp->groups->id, 'new_calendar_event', __( 'New group event', 'groupcalendar' ) );
  665.  
  666. bp_activity_set_action( $bp->groups->id, 'edit_calendar_event', __( 'Modified group event', 'groupcalendar' ) );
  667.  
  668. }
  669.  
  670.  
  671.  
  672. //adds actions to the group recent actions
  673.  
  674. function bp_group_calendar_event_add_action_message($new, $event_id, $event_date, $event_title) {
  675.  
  676. global $bp;
  677.  
  678.  
  679.  
  680. $url = bp_group_calendar_create_event_url($event_id);
  681.  
  682.  
  683.  
  684. if ($new) {
  685.  
  686. $created_type = __('created', 'groupcalendar');
  687.  
  688. $component_action = 'new_calendar_event';
  689.  
  690. } else {
  691.  
  692. $created_type = __('modified', 'groupcalendar');
  693.  
  694. $component_action = 'edit_calendar_event';
  695.  
  696. }
  697.  
  698.  
  699.  
  700. $date = date_i18n(get_option('date_format').' '.get_option('time_format'), strtotime($event_date));
  701.  
  702.  
  703.  
  704. /* Record this in group activity stream */
  705.  
  706. $action = sprintf( __( '%s %s an event for the group %s:', 'groupcalendar'), bp_core_get_userlink( $bp->loggedin_user->id ), $created_type, '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
  707.  
  708. $content = '<a href="'.$url.'" title="'.__('View Event', 'groupcalendar').'">'.$date.': '.stripslashes($event_title).'</a>';
  709.  
  710.  
  711.  
  712. $activity_id = groups_record_activity( array(
  713.  
  714. 'action' => $action,
  715.  
  716. 'content' => $content,
  717.  
  718. 'primary_link' => $url,
  719.  
  720. 'type' => $component_action,
  721.  
  722. 'item_id' => $bp->groups->current_group->id,
  723.  
  724. 'secondary_item_id' => $event_id
  725.  
  726. ) );
  727.  
  728.  
  729.  
  730. //update group last updated
  731.  
  732. groups_update_last_activity( $bp->groups->current_group->id );
  733.  
  734. do_action( 'bp_groups_posted_update', $content, get_current_user_id(), $bp->groups->current_group->id, $activity_id );
  735.  
  736.  
  737.  
  738. }
  739.  
  740.  
  741.  
  742. //send the email
  743.  
  744. function bp_group_calendar_event_email($new, $event_id, $event_date, $event_title) {
  745.  
  746. global $wpdb, $current_user, $bp, $bgc_email_default;
  747.  
  748.  
  749.  
  750. //check if email is disabled for group
  751.  
  752. $email = groups_get_groupmeta($bp->groups->current_group->id, 'group_calendar_send_email');
  753.  
  754. if ( empty( $email ) ){
  755.  
  756. $email = BGC_EMAIL_DEFAULT;
  757.  
  758. }
  759.  
  760. if ($email == 'no')
  761.  
  762. return;
  763.  
  764.  
  765.  
  766. //prepare fields
  767.  
  768. $date = date_i18n(get_option('date_format').' '.get_option('time_format'), strtotime($event_date));
  769.  
  770. $url = bp_group_calendar_create_event_url($event_id);
  771.  
  772. $site_name = get_blog_option( BP_ROOT_BLOG, 'blogname' );
  773.  
  774. $email_subject = sprintf( __( '%s created a new event for your group %s', 'groupcalendar'), bp_core_get_userlink( $bp->loggedin_user->id, true ), $bp->groups->current_group->name);
  775.  
  776. $event_description = strip_tags(stripslashes($_POST['event-desc']));
  777.  
  778. $location = strip_tags(trim(stripslashes($_POST['event-loc'])));
  779.  
  780.  
  781.  
  782. //send emails
  783.  
  784. $group_link = trailingslashit( bp_get_group_permalink( $bp->groups->current_group ) );
  785.  
  786.  
  787.  
  788. $user_ids = BP_Groups_Member::get_group_member_ids($bp->groups->current_group->id);
  789.  
  790.  
  791.  
  792. foreach ($user_ids as $user_id) {
  793.  
  794. //skip opt-outs
  795.  
  796. if ( 'no' == get_usermeta( $user_id, 'notification_groups_calendar_event' ) )
  797.  
  798. continue;
  799.  
  800.  
  801.  
  802. $ud = get_userdata( $user_id );
  803.  
  804.  
  805.  
  806. // Set up and send the message
  807.  
  808. $to = $ud->user_email;
  809.  
  810.  
  811.  
  812. $settings_link = bp_core_get_user_domain( $user_id ) . 'settings/notifications/';
  813.  
  814.  
  815.  
  816. $message = sprintf( __(
  817.  
  818. "%s:
  819.  
  820. ---------------------
  821.  
  822.  
  823.  
  824. %s
  825.  
  826. %s
  827.  
  828.  
  829.  
  830. Description:
  831.  
  832. %s
  833.  
  834.  
  835.  
  836. Location:
  837.  
  838. %s
  839.  
  840.  
  841.  
  842. ---------------------
  843.  
  844. View this Event: %s
  845.  
  846. View this Group: %s
  847.  
  848.  
  849.  
  850. %s
  851.  
  852. ", 'groupcalendar' ), $email_subject, stripslashes($event_title), $date, $event_description, $location, $url, $group_link, $site_name );
  853.  
  854.  
  855.  
  856. $message .= sprintf( __( 'To unsubscribe from these emails please login and visit: %s', 'groupcalendar' ), $settings_link );
  857.  
  858.  
  859.  
  860. // Send it
  861.  
  862. wp_mail( $to, $email_subject, $message );
  863.  
  864.  
  865.  
  866. unset( $message, $to );
  867.  
  868. }
  869.  
  870.  
  871.  
  872. }
  873.  
  874.  
  875.  
  876. function bp_group_calendar_get_capabilities() {
  877.  
  878. global $bp;
  879.  
  880.  
  881.  
  882. if ( bp_group_is_admin() ) {
  883.  
  884. return 'full';
  885.  
  886. } else if ( bp_group_is_mod() ) {
  887.  
  888.  
  889.  
  890. $group_calendar_moderator_capabilities = groups_get_groupmeta( $bp->groups->current_group->id, 'group_calendar_moderator_capabilities' );
  891.  
  892. if ( empty( $group_calendar_moderator_capabilities ) ){
  893.  
  894. return BGC_MODERATOR_DEFAULT;
  895.  
  896. } else {
  897.  
  898. return $group_calendar_moderator_capabilities;
  899.  
  900. }
  901.  
  902.  
  903.  
  904. } else if ( bp_group_is_member() ) {
  905.  
  906.  
  907.  
  908. $group_calendar_member_capabilities = groups_get_groupmeta( $bp->groups->current_group->id, 'group_calendar_member_capabilities' );
  909.  
  910. if ( empty( $group_calendar_member_capabilities ) ){
  911.  
  912. return BGC_MEMBER_DEFAULT;
  913.  
  914. } else {
  915.  
  916. return $group_calendar_member_capabilities;
  917.  
  918. }
  919.  
  920.  
  921.  
  922. } else {
  923.  
  924. return 'none';
  925.  
  926. }
  927.  
  928.  
  929.  
  930. }
  931.  
  932.  
  933.  
  934. function bp_group_calendar_url_parse() {
  935.  
  936.  
  937.  
  938. global $wpdb, $current_site;
  939.  
  940.  
  941.  
  942. $calendar_url = $_SERVER['REQUEST_URI'];
  943.  
  944.  
  945.  
  946. $calendar_url_clean = explode("?",$calendar_url);
  947.  
  948.  
  949.  
  950. $calendar_url = $calendar_url_clean[0];
  951.  
  952.  
  953.  
  954. $calendar_url_sections = explode("/calendar/",$calendar_url);
  955.  
  956.  
  957.  
  958. $calendar_url = $calendar_url_sections[1];
  959.  
  960.  
  961.  
  962. $calendar_url = ltrim($calendar_url, "/");
  963.  
  964.  
  965.  
  966. $calendar_url = rtrim($calendar_url, "/");
  967.  
  968.  
  969.  
  970. $base = $calendar_url_sections[0] . '/calendar/';
  971.  
  972.  
  973.  
  974. $base = ltrim($base, "/");
  975.  
  976.  
  977.  
  978. $base = rtrim($base, "/");
  979.  
  980.  
  981.  
  982. if ( !empty( $calendar_url ) ) {
  983.  
  984.  
  985.  
  986. $calendar_url = ltrim($calendar_url, "/");
  987.  
  988. $calendar_url = rtrim($calendar_url, "/");
  989.  
  990. $calendar_url_sections = explode("/",$calendar_url);
  991.  
  992.  
  993.  
  994. //check for valid dates.
  995.  
  996. if (isset($calendar_url_sections[0]) && $calendar_url_sections[0] >= 2000 && $calendar_url_sections[0] < 3000)
  997.  
  998. $date['year'] = (int)$calendar_url_sections[0];
  999.  
  1000.  
  1001.  
  1002. if (isset($date['year']) && isset($calendar_url_sections[1]) && $calendar_url_sections[1] >= 1 && $calendar_url_sections[1] <= 12)
  1003.  
  1004. $date['month'] = (int)$calendar_url_sections[1];
  1005.  
  1006.  
  1007.  
  1008. if (isset($date['month']) && isset($calendar_url_sections[2]) && $calendar_url_sections[2] >= 1 && $calendar_url_sections[2] <= 31)
  1009.  
  1010. $date['day'] = (int)$calendar_url_sections[2];
  1011.  
  1012.  
  1013.  
  1014. }
  1015.  
  1016.  
  1017.  
  1018. $date['base'] = $base;
  1019.  
  1020.  
  1021.  
  1022. return $date;
  1023.  
  1024.  
  1025.  
  1026. }
  1027.  
  1028.  
  1029.  
  1030. function bp_group_calendar_event_url_parse() {
  1031.  
  1032.  
  1033.  
  1034. $url = $_SERVER['REQUEST_URI'];
  1035.  
  1036. if (strpos("/calendar/event/", $url) !== false)
  1037.  
  1038. return false;
  1039.  
  1040.  
  1041.  
  1042. $url_clean = explode("?", $url);
  1043.  
  1044. $url = $url_clean[0];
  1045.  
  1046. $url_sections = explode("/calendar/event/", $url);
  1047.  
  1048. $url = isset($url_sections[1]) ? $url_sections[1] : '';
  1049.  
  1050. $url = ltrim($url, "/");
  1051.  
  1052. $url = rtrim($url, "edit/");
  1053.  
  1054. $url = rtrim($url, "delete/");
  1055.  
  1056. $url = rtrim($url, "/");
  1057.  
  1058.  
  1059.  
  1060. if (is_numeric($url))
  1061.  
  1062. return (int)$url;
  1063.  
  1064. else
  1065.  
  1066. return false;
  1067.  
  1068.  
  1069.  
  1070. }
  1071.  
  1072.  
  1073.  
  1074. function bp_group_calendar_event_is_edit() {
  1075.  
  1076.  
  1077.  
  1078. $url = $_SERVER['REQUEST_URI'];
  1079.  
  1080. if (strpos($url, "/edit/"))
  1081.  
  1082. return true;
  1083.  
  1084. else
  1085.  
  1086. return false;
  1087.  
  1088.  
  1089.  
  1090. }
  1091.  
  1092.  
  1093.  
  1094. function bp_group_calendar_event_is_delete() {
  1095.  
  1096. global $wpdb, $current_user, $bp;
  1097.  
  1098.  
  1099.  
  1100. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1101.  
  1102. $event_id = bp_group_calendar_event_url_parse();
  1103.  
  1104. $group_id = $bp->groups->current_group->id;
  1105.  
  1106. $url = $_SERVER['REQUEST_URI'];
  1107.  
  1108. if (strpos($url, "/delete/") && $event_id) {
  1109.  
  1110.  
  1111.  
  1112. //check nonce
  1113.  
  1114. if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'event-delete-link')) {
  1115.  
  1116. bp_core_add_message( __('There was a security problem', 'groupcalendar'), 'error' );
  1117.  
  1118. return false;
  1119.  
  1120. }
  1121.  
  1122.  
  1123.  
  1124. if ($calendar_capabilities=='none') {
  1125.  
  1126. bp_core_add_message( __('You do not have permission to delete events', 'groupcalendar'), 'error' );
  1127.  
  1128. return false;
  1129.  
  1130. }
  1131.  
  1132.  
  1133.  
  1134. //can user modify this event?
  1135.  
  1136. if ($calendar_capabilities == 'limited') {
  1137.  
  1138. $creator_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM " . $wpdb->base_prefix . "bp_groups_calendars WHERE id = %d AND group_id = %d", $event_id, $group_id) );
  1139.  
  1140.  
  1141.  
  1142. if ($creator_id != $current_user->ID) {
  1143.  
  1144. bp_core_add_message( __("You don't have permission to delete that event", 'groupcalendar'), 'error' );
  1145.  
  1146. return false;
  1147.  
  1148. }
  1149.  
  1150. }
  1151.  
  1152.  
  1153.  
  1154. //delete event
  1155.  
  1156. $result = $wpdb->query( $wpdb->prepare("DELETE FROM " . $wpdb->base_prefix . "bp_groups_calendars WHERE id = %d AND group_id = %d LIMIT 1", $event_id, $group_id) );
  1157.  
  1158.  
  1159.  
  1160. if (!$result) {
  1161.  
  1162. bp_core_add_message( __("There was a problem deleting", 'groupcalendar'), 'error' );
  1163.  
  1164. return false;
  1165.  
  1166. }
  1167.  
  1168.  
  1169.  
  1170. //delete activity tied to event
  1171.  
  1172. $result = $wpdb->query( $wpdb->prepare("DELETE FROM " . $wpdb->base_prefix . "bp_activity WHERE item_id = %d AND secondary_item_id = %d", $group_id, $event_id) );
  1173.  
  1174.  
  1175.  
  1176. //success!
  1177.  
  1178. bp_core_add_message( __('Event deleted successfully', 'groupcalendar') );
  1179.  
  1180. return true;
  1181.  
  1182.  
  1183.  
  1184. } else {
  1185.  
  1186. return false;
  1187.  
  1188. }
  1189.  
  1190.  
  1191.  
  1192. }
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198. function bp_group_calendar_create_event_url($event_id, $edit=false) {
  1199.  
  1200. global $bp;
  1201.  
  1202.  
  1203.  
  1204. $url = bp_get_group_permalink( $bp->groups->current_group );
  1205.  
  1206. $url .= 'calendar/event/'.$event_id.'/';
  1207.  
  1208.  
  1209.  
  1210. if ($edit)
  1211.  
  1212. $url .= "edit/";
  1213.  
  1214.  
  1215.  
  1216. return $url;
  1217.  
  1218.  
  1219.  
  1220. }
  1221.  
  1222.  
  1223.  
  1224. //------------------------------------------------------------------------//
  1225.  
  1226.  
  1227.  
  1228. //---Output Functions-----------------------------------------------------//
  1229.  
  1230.  
  1231.  
  1232. //------------------------------------------------------------------------//
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238. function bp_group_calendar_settings() {
  1239.  
  1240. global $wpdb, $current_site, $groups_template;
  1241.  
  1242.  
  1243.  
  1244. if (!empty($groups_template->group)) {
  1245.  
  1246. $group = $groups_template->group;
  1247.  
  1248. }
  1249.  
  1250.  
  1251.  
  1252. if ( !empty($group) ) {
  1253.  
  1254. $group_calendar_moderator_capabilities = groups_get_groupmeta( $group->id, 'group_calendar_moderator_capabilities' );
  1255.  
  1256. }
  1257.  
  1258.  
  1259.  
  1260. if ( empty( $group_calendar_moderator_capabilities ) ){
  1261.  
  1262. $group_calendar_moderator_capabilities = BGC_MODERATOR_DEFAULT;
  1263.  
  1264. }
  1265.  
  1266.  
  1267.  
  1268. if ( !empty($group) ) {
  1269.  
  1270. $group_calendar_member_capabilities = groups_get_groupmeta( $group->id, 'group_calendar_member_capabilities' );
  1271.  
  1272. }
  1273.  
  1274.  
  1275.  
  1276. if ( empty( $group_calendar_member_capabilities ) ){
  1277.  
  1278. $group_calendar_member_capabilities = BGC_MEMBER_DEFAULT;
  1279.  
  1280. }
  1281.  
  1282.  
  1283.  
  1284. if ( !empty($group) ) {
  1285.  
  1286. $email = groups_get_groupmeta($group->id, 'group_calendar_send_email');
  1287.  
  1288. }
  1289.  
  1290. if ( empty( $email ) ) {
  1291.  
  1292. $email = BGC_EMAIL_DEFAULT;
  1293.  
  1294. }
  1295.  
  1296.  
  1297.  
  1298. ?>
  1299.  
  1300. <h4><?php _e('Group Calendar Settings', 'groupcalendar') ?></h4>
  1301.  
  1302. <label for="group-calendar-moderator-capabilities"><?php _e('Moderator Capabilities', 'groupcalendar') ?></label>
  1303.  
  1304.  
  1305.  
  1306. <select name="group-calendar-moderator-capabilities" id="group-calendar-moderator-capabilities">
  1307.  
  1308.  
  1309.  
  1310. <option value="full" <?php if ( $group_calendar_moderator_capabilities == 'full') { echo 'selected="selected"'; } ?>><?php _e('Create events / Edit all events', 'groupcalendar'); ?></option>
  1311.  
  1312.  
  1313.  
  1314. <option value="limited" <?php if ( $group_calendar_moderator_capabilities == 'limited') { echo 'selected="selected"'; } ?>><?php _e('Create events / Edit own events', 'groupcalendar'); ?></option>
  1315.  
  1316.  
  1317.  
  1318. <option value="none" <?php if ( $group_calendar_moderator_capabilities == 'none') { echo 'selected="selected"'; } ?>><?php _e('No capabilities', 'groupcalendar'); ?></option>
  1319.  
  1320.  
  1321.  
  1322. </select>
  1323.  
  1324.  
  1325.  
  1326. <label for="group-calendar-member-capabilities"><?php _e('Member Capabilities', 'groupcalendar') ?></label>
  1327.  
  1328.  
  1329.  
  1330. <select name="group-calendar-member-capabilities" id="group-calendar-member-capabilities">
  1331.  
  1332.  
  1333.  
  1334. <option value="full" <?php if ( $group_calendar_member_capabilities == 'full') { echo 'selected="selected"'; } ?>><?php _e('Create events / Edit all events', 'groupcalendar'); ?></option>
  1335.  
  1336.  
  1337.  
  1338. <option value="limited" <?php if ( $group_calendar_member_capabilities == 'limited') { echo 'selected="selected"'; } ?>><?php _e('Create events / Edit own events', 'groupcalendar'); ?></option>
  1339.  
  1340.  
  1341.  
  1342. <option value="none" <?php if ( $group_calendar_member_capabilities == 'none') { echo 'selected="selected"'; } ?>><?php _e('No capabilities', 'groupcalendar'); ?></option>
  1343.  
  1344.  
  1345.  
  1346. </select>
  1347.  
  1348.  
  1349.  
  1350. <label><?php _e('Enable Email Notification of New Events', 'groupcalendar') ?><br />
  1351.  
  1352. <small><?php _e('Individual users can disable notifications', 'groupcalendar') ?></small></label>
  1353.  
  1354. <label><input value="yes" name="group-calendar-send-email" type="radio"<?php echo ($email == 'yes') ? ' checked="checked"' : ''; ?> /> <?php _e('Yes', 'groupcalendar') ?></label>
  1355.  
  1356. <label><input value="no" name="group-calendar-send-email" type="radio"<?php echo ($email == 'no') ? ' checked="checked"' : ''; ?> /> <?php _e('No', 'groupcalendar') ?></label>
  1357.  
  1358. <?php
  1359.  
  1360. }
  1361.  
  1362.  
  1363.  
  1364. function bp_group_calendar_notification_settings() {
  1365.  
  1366. if ( !$notification_groups_calendar_event = bp_get_user_meta( bp_displayed_user_id(), 'notification_groups_calendar_event', true ) )
  1367.  
  1368. $notification_groups_calendar_event = 'yes';
  1369.  
  1370. ?>
  1371.  
  1372. <tr>
  1373.  
  1374. <td></td>
  1375.  
  1376. <td><?php _e('A new group event is created', 'groupcalendar') ?></td>
  1377.  
  1378. <td class="yes"><input type="radio" name="notifications[notification_groups_calendar_event]" value="yes" <?php checked( $notification_groups_calendar_event, 'yes', true ) ?>/></td>
  1379.  
  1380. <td class="no"><input type="radio" name="notifications[notification_groups_calendar_event]" value="no" <?php checked( $notification_groups_calendar_event, 'no', true ) ?>/></td>
  1381.  
  1382. </tr>
  1383.  
  1384. <?php
  1385.  
  1386. }
  1387.  
  1388.  
  1389.  
  1390. //enqeue js on product settings screen
  1391.  
  1392. function bp_group_calendar_js() {
  1393.  
  1394.  
  1395.  
  1396. if (!is_admin() && strpos($_SERVER['REQUEST_URI'], '/calendar/') !== false) {
  1397.  
  1398. global $bgc_locale;
  1399.  
  1400.  
  1401.  
  1402. wp_enqueue_style( 'groupcalendar-css', plugins_url('/bp-group-calendar/groupcalendar/group_calendar.css'), false);
  1403.  
  1404. wp_enqueue_style( 'jquery-datepicker-css', plugins_url('/bp-group-calendar/groupcalendar/datepicker/css/smoothness/jquery-ui-1.10.3.custom.min.css'), '1.10.3');
  1405.  
  1406. wp_enqueue_script( 'jquery-ui-datepicker');
  1407.  
  1408.  
  1409.  
  1410. //only load languages for datepicker if not english (or it will show Chinese!)
  1411.  
  1412. if ($bgc_locale['code'] != 'en')
  1413.  
  1414. wp_enqueue_script( 'jquery-datepicker-i18n', plugins_url('/bp-group-calendar/groupcalendar/datepicker/js/jquery-ui-i18n.min.js'), array('jquery-ui-datepicker'));
  1415.  
  1416. }
  1417.  
  1418. }
  1419.  
  1420.  
  1421.  
  1422. function bp_group_calendar_js_output() {
  1423.  
  1424. //display css
  1425.  
  1426. if (!is_admin() && strpos($_SERVER['REQUEST_URI'], '/calendar/') !== false) {
  1427.  
  1428. global $bgc_locale;
  1429.  
  1430. ?>
  1431.  
  1432. <script type="text/javascript">
  1433.  
  1434. jQuery(document).ready(function ($) {
  1435.  
  1436. jQuery.datepicker.setDefaults(jQuery.datepicker.regional['<?php echo $bgc_locale['code']; ?>']);
  1437.  
  1438. jQuery('#event-date').datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, firstDay: <?php echo $bgc_locale['week_start']; ?>});
  1439.  
  1440. jQuery('a#event-delete-link').click(function() {
  1441.  
  1442. var answer = confirm("<?php _e('Are you sure you want to delete this event?', 'groupcalendar'); ?>")
  1443.  
  1444. if (answer){
  1445.  
  1446. return true;
  1447.  
  1448. } else {
  1449.  
  1450. return false;
  1451.  
  1452. };
  1453.  
  1454. });
  1455.  
  1456. });
  1457.  
  1458. </script>
  1459.  
  1460. <?php
  1461.  
  1462. }
  1463.  
  1464. }
  1465.  
  1466.  
  1467.  
  1468. function bp_group_calendar_highlighted_events($group_id, $date='') {
  1469.  
  1470. global $wpdb;
  1471.  
  1472.  
  1473.  
  1474. if ($date) {
  1475.  
  1476. $start_date = date_i18n('Y-m-', strtotime($date)).'01 00:00:00';
  1477.  
  1478. $end_date = date_i18n('Y-m-d H:i:s', strtotime("-1 second", strtotime("+1 month", strtotime($start_date))));
  1479.  
  1480. } else {
  1481.  
  1482. $start_date = date_i18n('Y-m-', time()).'01 00:00:00';
  1483.  
  1484. $end_date = date_i18n('Y-m-d H:i:s', strtotime("-1 second", strtotime("+1 month", strtotime($start_date))));
  1485.  
  1486. }
  1487.  
  1488. $filter = $wpdb->prepare(" WHERE group_id = %d AND event_time >= %s AND event_time <= %s", $group_id, $start_date, $end_date);
  1489.  
  1490.  
  1491.  
  1492. $events = $wpdb->get_col( "SELECT event_time FROM ".$wpdb->base_prefix."bp_groups_calendars".$filter." ORDER BY event_time ASC" );
  1493.  
  1494.  
  1495.  
  1496. if ($events) {
  1497.  
  1498.  
  1499.  
  1500. $highlighted_events = array();
  1501.  
  1502. foreach ($events as $event) {
  1503.  
  1504. $highlighted_events[] = date_i18n('Y-m-d', strtotime($event));
  1505.  
  1506. }
  1507.  
  1508.  
  1509.  
  1510. return $highlighted_events;
  1511.  
  1512.  
  1513.  
  1514. } else {
  1515.  
  1516. return array();
  1517.  
  1518. }
  1519.  
  1520.  
  1521.  
  1522. }
  1523.  
  1524.  
  1525.  
  1526. function bp_group_calendar_list_events($group_id, $range, $date='', $calendar_capabilities, $show_all = 0) {
  1527.  
  1528. global $wpdb, $current_user;
  1529.  
  1530.  
  1531.  
  1532. $date_format = get_option('date_format').' '.get_option('time_format');
  1533.  
  1534. $group_id = (int)$group_id;//cast it just in case
  1535.  
  1536.  
  1537.  
  1538. if ($range == 'all') {
  1539.  
  1540.  
  1541.  
  1542. $filter = " WHERE group_id = $group_id";
  1543.  
  1544. $empty_message = __('There are no scheduled events', 'groupcalendar');
  1545.  
  1546.  
  1547.  
  1548. } else if ($range == 'month') {
  1549.  
  1550.  
  1551.  
  1552. if ($date) {
  1553.  
  1554. $start_date = date_i18n('Y-m-', strtotime($date)).'01 00:00:00';
  1555.  
  1556. $end_date = date_i18n('Y-m-d H:i:s', strtotime("-1 second", strtotime("+1 month", strtotime($start_date))));
  1557.  
  1558. } else {
  1559.  
  1560. $start_date = date_i18n('Y-m-', time()).'01 00:00:00';
  1561.  
  1562. $end_date = date_i18n('Y-m-d H:i:s', strtotime("-1 second", strtotime("+1 month", strtotime($start_date))));
  1563.  
  1564. }
  1565.  
  1566. $filter = " WHERE group_id = $group_id AND event_time >= '$start_date' AND event_time <= '$end_date'";
  1567.  
  1568. $empty_message = __('There are no events scheduled for this month', 'groupcalendar');
  1569.  
  1570.  
  1571.  
  1572. } else if ($range == 'day') {
  1573.  
  1574.  
  1575.  
  1576. $start_date = date_i18n('Y-m-d H:i:s', strtotime($date));
  1577.  
  1578. $end_date = date_i18n('Y-m-d', strtotime($date)).' 23:59:59';
  1579.  
  1580. $filter = " WHERE group_id = $group_id AND event_time >= '$start_date' AND event_time <= '$end_date'";
  1581.  
  1582. $empty_message = __('There are no events scheduled for this day', 'groupcalendar');
  1583.  
  1584. $date_format = get_option('time_format');
  1585.  
  1586.  
  1587.  
  1588. } else if ($range == 'upcoming') {
  1589.  
  1590.  
  1591.  
  1592. $filter = " WHERE group_id = $group_id AND event_time >= '".date_i18n('Y-m-d H:i:s')."'";
  1593.  
  1594. $empty_message = __('There are no upcoming events', 'groupcalendar');
  1595.  
  1596.  
  1597.  
  1598. } else if ($range == 'mine') {
  1599.  
  1600.  
  1601.  
  1602. $filter = " WHERE group_id = $group_id AND event_time >= '".date_i18n('Y-m-d H:i:s')."' AND user_id = ".$current_user->ID;
  1603.  
  1604. $empty_message = __('You have no scheduled events', 'groupcalendar');
  1605.  
  1606.  
  1607.  
  1608. }
  1609.  
  1610.  
  1611.  
  1612. if (!$show_all)
  1613.  
  1614. $limit = " LIMIT 10";
  1615.  
  1616.  
  1617.  
  1618. $events = $wpdb->get_results( "SELECT * FROM ".$wpdb->base_prefix."bp_groups_calendars".$filter." ORDER BY event_time ASC".$limit );
  1619.  
  1620.  
  1621.  
  1622. if ($events) {
  1623.  
  1624.  
  1625.  
  1626. $events_list = '<ul class="events-list">';
  1627.  
  1628. //loop through events
  1629.  
  1630. foreach ($events as $event) {
  1631.  
  1632. $class = ($event->user_id==$current_user->ID) ? ' class="my_event"' : '';
  1633.  
  1634.  
  1635.  
  1636. $events_list .= "\n<li".$class.">";
  1637.  
  1638. // eleni changed on 26/1/2011
  1639.  
  1640. //$events_list .= '<a href="'.bp_group_calendar_create_event_url($event->id).'" title="'.__('View Event', 'groupcalendar').'">'.stripslashes($event->event_title).': '.date_i18n($date_format, strtotime($event->event_time)).'</a>';
  1641.  
  1642. $events_list .= '<a href="'.bp_group_calendar_create_event_url($event->id).'" title="'.__('View Event', 'groupcalendar').'" class="event_title">'.date_i18n($date_format, strtotime($event->event_time)).': '.stripslashes($event->event_title).'</a>';
  1643.  
  1644.  
  1645.  
  1646. //add edit link if allowed
  1647.  
  1648. if ($calendar_capabilities == 'full' || ($calendar_capabilities == 'limited' && $event->user_id==$current_user->ID)) {
  1649.  
  1650. $events_list .= ' | <a href="'.bp_group_calendar_create_event_url($event->id, true).'" title="'.__('Edit Event', 'groupcalendar').'">'.__('Edit', 'groupcalendar').' &raquo;</a>';
  1651.  
  1652. }
  1653.  
  1654.  
  1655.  
  1656. $events_list .= "</li>";
  1657.  
  1658. }
  1659.  
  1660. $events_list .= "</ul>";
  1661.  
  1662.  
  1663.  
  1664. } else { //no events for query
  1665.  
  1666. $events_list = '<div id="message" class="info"><p>'.$empty_message.'</p></div>';
  1667.  
  1668. }
  1669.  
  1670.  
  1671.  
  1672. echo $events_list;
  1673.  
  1674. }
  1675.  
  1676.  
  1677.  
  1678. //------------------------------------------------------------------------//
  1679.  
  1680.  
  1681.  
  1682. //---Page Output Functions------------------------------------------------//
  1683.  
  1684.  
  1685.  
  1686. //------------------------------------------------------------------------//
  1687.  
  1688.  
  1689.  
  1690. //widgets
  1691.  
  1692.  
  1693.  
  1694. function bp_group_calendar_widget_day($date) {
  1695.  
  1696. global $bp, $bgc_locale;
  1697.  
  1698.  
  1699.  
  1700. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1701.  
  1702.  
  1703.  
  1704. $day = $date['year'].'-'.$date['month'].'-'.$date['day'];
  1705.  
  1706.  
  1707.  
  1708. $cal = new Calendar($date['day'], $date['year'], $date['month']);
  1709.  
  1710. $cal->week_start = $bgc_locale['week_start'];
  1711.  
  1712. $url = bp_get_group_permalink( $bp->groups->current_group ).'calendar';
  1713.  
  1714. $cal->formatted_link_to = $url.'/%Y/%m/%d/';
  1715.  
  1716. ?>
  1717.  
  1718. <div class="bp-widget">
  1719.  
  1720. <h4><?php _e('Day View', 'groupcalendar'); ?>: <?php echo date_i18n(get_option('date_format'), strtotime($day)); ?></h4>
  1721.  
  1722. <table class="calendar-view">
  1723.  
  1724. <tr>
  1725.  
  1726. <td class="cal-left">
  1727.  
  1728. <?php print($cal->output_calendar()); ?>
  1729.  
  1730. </td>
  1731.  
  1732. <td class="cal-right">
  1733.  
  1734. <h5 class="events-title"><?php _e("Events For", 'groupcalendar'); ?> <?php echo date_i18n(get_option('date_format'), strtotime($day)); ?>:</h5>
  1735.  
  1736. <?php bp_group_calendar_list_events($bp->groups->current_group->id, 'day', $day, $calendar_capabilities); ?>
  1737.  
  1738. </td>
  1739.  
  1740. </tr>
  1741.  
  1742. </table>
  1743.  
  1744. </div>
  1745.  
  1746. <?php
  1747.  
  1748. }
  1749.  
  1750.  
  1751.  
  1752. function bp_group_calendar_widget_month($date) {
  1753.  
  1754. global $bp, $bgc_locale;
  1755.  
  1756.  
  1757.  
  1758. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1759.  
  1760.  
  1761.  
  1762. if (isset($date['month'])) {
  1763.  
  1764. //show selected month
  1765.  
  1766. $cal = new Calendar('', $date['year'], $date['month']);
  1767.  
  1768. } else {
  1769.  
  1770. //show current month
  1771.  
  1772. $cal = new Calendar();
  1773.  
  1774. }
  1775.  
  1776.  
  1777.  
  1778. $cal->week_start = $bgc_locale['week_start'];
  1779.  
  1780. $url = bp_get_group_permalink( $bp->groups->current_group ).'calendar';
  1781.  
  1782. $cal->formatted_link_to = $url.'/%Y/%m/%d/';
  1783.  
  1784.  
  1785.  
  1786. //first day of month for calulation previous and next months
  1787.  
  1788. $first_day = $cal->year . "-" . $cal->month . "-01";
  1789.  
  1790. $cal->highlighted_dates = bp_group_calendar_highlighted_events($bp->groups->current_group->id, $first_day);
  1791.  
  1792. $previous_month = $url.date("/Y/m/", strtotime("-1 month", strtotime($first_day)));
  1793.  
  1794. $next_month = $url.date("/Y/m/", strtotime("+1 month", strtotime($first_day)));
  1795.  
  1796. $this_year = $url.date("/Y/", strtotime($first_day));
  1797.  
  1798. ?>
  1799.  
  1800. <div class="bp-widget">
  1801.  
  1802. <h4>
  1803.  
  1804. <?php _e('Month View', 'groupcalendar'); ?>:
  1805.  
  1806. <?php echo date_i18n(__('F Y', 'groupcalendar'), strtotime($first_day)); ?>
  1807.  
  1808. <span>
  1809.  
  1810. <a title="<?php _e('Previous Month', 'groupcalendar'); ?>" href="<?php echo $previous_month; ?>">&larr; <?php _e('Previous', 'groupcalendar'); ?></a> |
  1811.  
  1812. <a title="<?php _e('Full Year', 'groupcalendar'); ?>" href="<?php echo $this_year; ?>"><?php _e('Year', 'groupcalendar'); ?></a> |
  1813.  
  1814. <a title="<?php _e('Next Month', 'groupcalendar'); ?>" href="<?php echo $next_month; ?>"><?php _e('Next', 'groupcalendar'); ?> &rarr;</a>
  1815.  
  1816. </span>
  1817.  
  1818. </h4>
  1819.  
  1820. <table class="calendar-view">
  1821.  
  1822. <tr>
  1823.  
  1824. <td class="cal-left">
  1825.  
  1826. <?php print($cal->output_calendar()); ?>
  1827.  
  1828. </td>
  1829.  
  1830. <td class="cal-right">
  1831.  
  1832. <h5 class="events-title"><?php _e('Events For', 'groupcalendar'); ?> <?php echo date_i18n('F Y', strtotime($first_day));?>:</h5>
  1833.  
  1834. <?php bp_group_calendar_list_events($bp->groups->current_group->id, 'month', $first_day, $calendar_capabilities); ?>
  1835.  
  1836. </td>
  1837.  
  1838. </tr>
  1839.  
  1840. </table>
  1841.  
  1842.  
  1843.  
  1844. </div>
  1845.  
  1846. <?php
  1847.  
  1848. }
  1849.  
  1850.  
  1851.  
  1852. function bp_group_calendar_widget_year($date) {
  1853.  
  1854. global $bp, $bgc_locale;
  1855.  
  1856.  
  1857.  
  1858. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1859.  
  1860.  
  1861.  
  1862. $year = $date['year'];
  1863.  
  1864. $month = 1;
  1865.  
  1866.  
  1867.  
  1868. $url = bp_get_group_permalink( $bp->groups->current_group ).'calendar';
  1869.  
  1870. //first day of month for calulation previous and next years
  1871.  
  1872. $first_day = $year . "-01-01";
  1873.  
  1874. $previous_year = $url.date_i18n("/Y/", strtotime("-1 year", strtotime($first_day)));
  1875.  
  1876. $next_year = $url.date_i18n("/Y/", strtotime("+1 year", strtotime($first_day)));
  1877.  
  1878.  
  1879.  
  1880. ?>
  1881.  
  1882. <div class="bp-widget">
  1883.  
  1884. <h4>
  1885.  
  1886. <?php _e('Year View', 'groupcalendar'); ?>: <?php echo date_i18n('Y', strtotime($first_day)); ?>
  1887.  
  1888. <span>
  1889.  
  1890. <a title="<?php _e('Previous Year', 'groupcalendar'); ?>" href="<?php echo $previous_year; ?>">&larr; <?php _e('Previous', 'groupcalendar'); ?></a> |
  1891.  
  1892. <a title="<?php _e('Next Year', 'groupcalendar'); ?>" href="<?php echo $next_year; ?>"><?php _e('Next', 'groupcalendar'); ?> &rarr;</a>
  1893.  
  1894. </span>
  1895.  
  1896. </h4>
  1897.  
  1898. <?php
  1899.  
  1900. //loop through years
  1901.  
  1902. for ($i = 1; $i <= 12; $i++) {
  1903.  
  1904. $cal = new Calendar('', $year, $month);
  1905.  
  1906. $cal->week_start = $bgc_locale['week_start'];
  1907.  
  1908. $cal->formatted_link_to = $url.'/%Y/%m/%d/';
  1909.  
  1910. $first_day = $cal->year . "-" . $cal->month . "-01";
  1911.  
  1912. $cal->highlighted_dates = bp_group_calendar_highlighted_events($bp->groups->current_group->id, $first_day);
  1913.  
  1914. echo '<div class="year-cal-item">';
  1915.  
  1916. print($cal->output_calendar());
  1917.  
  1918. echo '</div>';
  1919.  
  1920.  
  1921.  
  1922. //first day of month for calulation of next month
  1923.  
  1924. $first_day = $year . "-" . $month . "-01";
  1925.  
  1926. $next_stamp = strtotime("+1 month", strtotime($first_day));
  1927.  
  1928. $year = date_i18n("Y", $next_stamp);
  1929.  
  1930. $month = date_i18n("m", $next_stamp);
  1931.  
  1932. }
  1933.  
  1934. ?>
  1935.  
  1936. <div class="clear"></div>
  1937.  
  1938. </div>
  1939.  
  1940. <?php
  1941.  
  1942. }
  1943.  
  1944.  
  1945.  
  1946. function bp_group_calendar_widget_upcoming_events() {
  1947.  
  1948. global $bp;
  1949.  
  1950.  
  1951.  
  1952. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1953.  
  1954.  
  1955.  
  1956. ?>
  1957.  
  1958. <div class="bp-widget">
  1959.  
  1960. <h4><?php _e('Upcoming Events', 'groupcalendar'); ?></h4>
  1961.  
  1962. <?php bp_group_calendar_list_events($bp->groups->current_group->id, 'upcoming', '', $calendar_capabilities); ?>
  1963.  
  1964. </div>
  1965.  
  1966. <?php
  1967.  
  1968. }
  1969.  
  1970.  
  1971.  
  1972. function bp_group_calendar_widget_my_events() {
  1973.  
  1974. global $bp;
  1975.  
  1976.  
  1977.  
  1978. if (!is_user_logged_in())
  1979.  
  1980. return;
  1981.  
  1982.  
  1983.  
  1984. $calendar_capabilities = bp_group_calendar_get_capabilities();
  1985.  
  1986.  
  1987.  
  1988. ?>
  1989.  
  1990. <div class="bp-widget">
  1991.  
  1992. <h4><?php _e('My Events', 'groupcalendar'); ?></h4>
  1993.  
  1994. <?php bp_group_calendar_list_events($bp->groups->current_group->id, 'mine', '', $calendar_capabilities); ?>
  1995.  
  1996. </div>
  1997.  
  1998. <?php
  1999.  
  2000. }
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006. function bp_group_calendar_widget_event_display($event_id) {
  2007.  
  2008. global $wpdb, $current_user, $bp, $bgc_locale;
  2009.  
  2010.  
  2011.  
  2012. $group_id = $bp->groups->current_group->id;
  2013.  
  2014.  
  2015.  
  2016. $calendar_capabilities = bp_group_calendar_get_capabilities();
  2017.  
  2018.  
  2019.  
  2020. $event = $wpdb->get_row( $wpdb->prepare("SELECT * FROM ".$wpdb->base_prefix."bp_groups_calendars WHERE group_id = %d AND id = %d", $group_id, $event_id) );
  2021.  
  2022.  
  2023.  
  2024. //do nothing if invalid event_id
  2025.  
  2026. if (!$event)
  2027.  
  2028. return;
  2029.  
  2030.  
  2031.  
  2032. //creat edit link if capable
  2033.  
  2034. if ($calendar_capabilities == 'full' || ($calendar_capabilities == 'limited' && $current_user->ID == $event->user_id))
  2035.  
  2036. $edit_link = '<span><a href="'.bp_group_calendar_create_event_url($event_id, true).'" title="'.__('Edit Event', 'groupcalendar').'">'.__('Edit', 'groupcalendar').' &rarr;</a></span>';
  2037.  
  2038.  
  2039.  
  2040. $map_url = 'http://maps.google.com/maps?hl='.$bgc_locale['code'].'&q='.urlencode(stripslashes($event->event_location));
  2041.  
  2042.  
  2043.  
  2044. $event_created_by = bp_core_get_userlink($event->user_id);
  2045.  
  2046. $event_created = date_i18n(get_option('date_format').__(' \a\t ', 'groupcalendar').get_option('time_format'), $event->created_stamp);
  2047.  
  2048. $event_modified_by = bp_core_get_userlink($event->last_edited_id);
  2049.  
  2050. $event_last_modified = date_i18n(get_option('date_format').__(' \a\t ', 'groupcalendar').get_option('time_format'), $event->last_edited_stamp);
  2051.  
  2052.  
  2053.  
  2054. $event_meta = '<span class="event-meta">'.sprintf(__('Created by %1$s on %2$s. Last modified by %3$s on %4$s.', 'groupcalendar'), $event_created_by, $event_created, $event_modified_by, $event_last_modified).'</span>';
  2055.  
  2056.  
  2057.  
  2058. ?>
  2059.  
  2060. <div class="bp-widget">
  2061.  
  2062. <h4>
  2063.  
  2064. <?php _e('Event Details', 'groupcalendar'); ?>
  2065.  
  2066. <?php echo $edit_link; ?>
  2067.  
  2068. </h4>
  2069.  
  2070.  
  2071.  
  2072. <h5 class="events-title"><?php echo stripslashes($event->event_title); ?></h5>
  2073.  
  2074. <span class="activity"><?php echo date_i18n(get_option('date_format').__(' \a\t ', 'groupcalendar').get_option('time_format'), strtotime($event->event_time)); ?></span>
  2075.  
  2076.  
  2077.  
  2078. <?php if ($event->event_description) : ?>
  2079.  
  2080. <h6 class="event-label"><?php _e('Description:', 'groupcalendar'); ?></h6>
  2081.  
  2082. <div class="event-description">
  2083.  
  2084. <?php echo stripslashes($event->event_description); ?>
  2085.  
  2086. </div>
  2087.  
  2088. <?php endif; ?>
  2089.  
  2090.  
  2091.  
  2092. <?php if ($event->event_location) : ?>
  2093.  
  2094. <h6 class="event-label"><?php _e('Location:', 'groupcalendar'); ?></h6>
  2095.  
  2096. <div class="event-location">
  2097.  
  2098.  
  2099.  
  2100. <?php echo stripslashes($event->event_location); ?>
  2101.  
  2102.  
  2103.  
  2104. <?php if ($event->event_map) : ?>
  2105.  
  2106. <span class="event-map">
  2107.  
  2108. <a href="<?php echo $map_url; ?>" target="_blank" title="<?php _e('View Google Map of Event Location', 'groupcalendar'); ?>"><?php _e('Map', 'groupcalendar'); ?> &raquo;</a>
  2109.  
  2110. </span>
  2111.  
  2112. <?php endif; ?>
  2113.  
  2114. </div>
  2115.  
  2116. <?php endif; ?>
  2117.  
  2118.  
  2119.  
  2120. <?php echo $event_meta; ?>
  2121.  
  2122.  
  2123.  
  2124. </div>
  2125.  
  2126. <?php
  2127.  
  2128. }
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134. function bp_group_calendar_widget_create_event($date) {
  2135.  
  2136. global $bp, $bgc_locale;
  2137.  
  2138.  
  2139.  
  2140. $calendar_capabilities = bp_group_calendar_get_capabilities();
  2141.  
  2142.  
  2143.  
  2144. //don't display widget if no capabilities
  2145.  
  2146. if ($calendar_capabilities == 'none')
  2147.  
  2148. return;
  2149.  
  2150.  
  2151.  
  2152. //if date given and valid, default form to it
  2153.  
  2154. $default_date = '';
  2155.  
  2156. if ( !empty($date['year']) && !empty($date['month']) && !empty($date['day']) ) {
  2157.  
  2158. $timestamp = strtotime($date['month'].'/'.$date['day'].'/'.$date['year']);
  2159.  
  2160. if ($timestamp >= time())
  2161.  
  2162. $default_date = date('Y-m-d', $timestamp);
  2163.  
  2164. }
  2165.  
  2166.  
  2167.  
  2168. $url = bp_get_group_permalink( $bp->groups->current_group ).'calendar/';
  2169.  
  2170.  
  2171.  
  2172. ?>
  2173.  
  2174. <div class="bp-widget">
  2175.  
  2176. <h4><?php _e('Create Event', 'groupcalendar'); ?></h4>
  2177.  
  2178. <form action="<?php echo $url; ?>" name="add-event-form" id="add-event-form" class="standard-form" method="post" enctype="multipart/form-data">
  2179.  
  2180. <label for="event-title"><?php _e('Title', 'groupcalendar'); ?> *</label>
  2181.  
  2182. <input name="event-title" id="event-title" value="" type="text">
  2183.  
  2184.  
  2185.  
  2186. <label for="event-date"><?php _e('Date', 'groupcalendar'); ?> *
  2187.  
  2188. <input name="event-date" id="event-date" value="<?php echo $default_date; ?>" type="text"></label>
  2189.  
  2190.  
  2191.  
  2192. <label for="event-time"><?php _e('Time', 'groupcalendar'); ?> *
  2193.  
  2194. <select name="event-hour" id="event-hour">
  2195.  
  2196. <?php
  2197.  
  2198. if ($bgc_locale['time_format']==24)
  2199.  
  2200. $bgc_locale['time_format'] = 23;
  2201.  
  2202. for ($i = 1; $i <= $bgc_locale['time_format']; $i++) {
  2203.  
  2204. $hour_check = ($i == 7) ? ' selected="selected"' : '';
  2205.  
  2206. echo '<option value="'.$i.'"'.$hour_check.'>'.$i."</option>\n";
  2207.  
  2208. }
  2209.  
  2210. ?>
  2211.  
  2212. </select>
  2213.  
  2214. <select name="event-minute" id="event-minute">
  2215.  
  2216. <option value="00">:00</option>
  2217.  
  2218. <option value="15">:15</option>
  2219.  
  2220. <option value="30">:30</option>
  2221.  
  2222. <option value="45">:45</option>
  2223.  
  2224. </select>
  2225.  
  2226. <?php if ($bgc_locale['time_format']==12) : ?>
  2227.  
  2228. <select name="event-ampm" id="event-ampm">
  2229.  
  2230. <option value="am">am</option>
  2231.  
  2232. <option value="pm">pm</option>
  2233.  
  2234. </select>
  2235.  
  2236. <?php endif; ?>
  2237.  
  2238. </label>
  2239.  
  2240.  
  2241.  
  2242. <label for="event-desc"><?php _e('Description', 'groupcalendar'); ?></label>
  2243.  
  2244. <?php
  2245.  
  2246. if (function_exists('wp_editor')) {
  2247.  
  2248. wp_editor('', 'event-desc', array('media_buttons' => false, 'dfw' => false));
  2249.  
  2250. } else {
  2251.  
  2252. the_editor('', 'event-desc', 'event-desc', false);
  2253.  
  2254. }
  2255.  
  2256. ?>
  2257. <br/>
  2258.  
  2259. <span class="plugin_loc_notice">(*Please get address from Google Maps and paste into location field so that map links will work for other users)</span>
  2260. <label for="event-loc"><?php _e('Location', 'groupcalendar'); ?></label>
  2261.  
  2262. <input name="event-loc" id="event-loc" value="" type="text">
  2263.  
  2264. <?php require ('plugin_state_selector.php'); ?>
  2265.  
  2266.  
  2267.  
  2268. <label for="event-map"><?php _e('Show Map Link?', 'groupcalendar'); ?>
  2269.  
  2270. <input name="event-map" id="event-map" value="1" type="checkbox" checked="checked" />
  2271.  
  2272. <small><?php _e('(Note: Location must be an address)', 'groupcalendar'); ?></small>
  2273.  
  2274. </label>
  2275.  
  2276.  
  2277.  
  2278. <input name="create-event" id="create-event" value="1" type="hidden">
  2279.  
  2280. <input name="group-id" id="group-id" value="<?php echo $bp->groups->current_group->id; ?>" type="hidden">
  2281.  
  2282. <?php wp_nonce_field('bp_group_calendar'); ?>
  2283.  
  2284.  
  2285.  
  2286. <p><input value="<?php _e('Create Event', 'groupcalendar'); ?> &raquo;" id="save" name="save" type="submit"></p>
  2287.  
  2288.  
  2289.  
  2290. </form>
  2291.  
  2292.  
  2293.  
  2294. </div>
  2295.  
  2296. <?php
  2297.  
  2298. }
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304. function bp_group_calendar_widget_edit_event($event_id=false) {
  2305.  
  2306. global $wpdb, $current_user, $bp, $bgc_locale;
  2307.  
  2308.  
  2309.  
  2310. $url = bp_get_group_permalink( $bp->groups->current_group ).'calendar/';
  2311.  
  2312.  
  2313.  
  2314. $group_id = $bp->groups->current_group->id;
  2315.  
  2316.  
  2317.  
  2318. $calendar_capabilities = bp_group_calendar_get_capabilities();
  2319.  
  2320.  
  2321.  
  2322. //don't display widget if no capabilities
  2323.  
  2324. if ($calendar_capabilities == 'none') {
  2325.  
  2326. bp_core_add_message( __("You don't have permission to edit events", 'groupcalendar'), 'error' );
  2327.  
  2328. return false;
  2329.  
  2330. }
  2331.  
  2332.  
  2333.  
  2334. if ($event_id) { //load from DB
  2335.  
  2336.  
  2337.  
  2338. $url .= 'event/'.$event_id.'/';
  2339.  
  2340.  
  2341.  
  2342. $event = $wpdb->get_row( $wpdb->prepare("SELECT * FROM ".$wpdb->base_prefix."bp_groups_calendars WHERE group_id = %d AND id = %d", $group_id, $event_id) );
  2343.  
  2344.  
  2345.  
  2346. if (!$event)
  2347.  
  2348. return false;
  2349.  
  2350.  
  2351.  
  2352. //check limited capability users
  2353.  
  2354. if ($calendar_capabilities == 'limited' && $current_user->ID != $event->user_id) {
  2355.  
  2356. bp_core_add_message( __("You don't have permission to edit that event", 'groupcalendar'), 'error' );
  2357.  
  2358. return false;
  2359.  
  2360. }
  2361.  
  2362.  
  2363.  
  2364. $event_title = stripslashes($event->event_title);
  2365.  
  2366.  
  2367.  
  2368. //check for valid date/time
  2369.  
  2370. $tmp_date = strtotime($event->event_time);
  2371.  
  2372. if ($tmp_date) {
  2373.  
  2374. $event_date = date_i18n('Y-m-d', $tmp_date);
  2375.  
  2376.  
  2377.  
  2378. $event_hour = date_i18n('g', $tmp_date);
  2379.  
  2380. $event_minute = date_i18n('i', $tmp_date);
  2381.  
  2382. $event_ampm = date_i18n('a', $tmp_date);
  2383.  
  2384. } else {
  2385.  
  2386. $event_hour = 7;
  2387.  
  2388. }
  2389.  
  2390.  
  2391.  
  2392. $event_description = stripslashes($event->event_description);
  2393.  
  2394. $event_location = stripslashes($event->event_location);
  2395.  
  2396. $event_map = ($event->event_map == 1) ? ' checked="checked"' : '';
  2397.  
  2398.  
  2399.  
  2400. $event_created_by = bp_core_get_userlink($event->user_id);
  2401.  
  2402. $event_created = date_i18n(get_option('date_format').__(' \a\t ', 'groupcalendar').get_option('time_format'), $event->created_stamp);
  2403.  
  2404. $event_modified_by = bp_core_get_userlink($event->last_edited_id);
  2405.  
  2406. $event_last_modified = date_i18n(get_option('date_format').__(' \a\t ', 'groupcalendar').get_option('time_format'), $event->last_edited_stamp);
  2407.  
  2408.  
  2409.  
  2410. $event_meta = '<span class="event-meta">'.sprintf(__('Created by %1$s on %2$s. Last modified by %3$s on %4$s.', 'groupcalendar'), $event_created_by, $event_created, $event_modified_by, $event_last_modified).'</span>';
  2411.  
  2412.  
  2413.  
  2414. $delete_url = $url.'delete/';
  2415.  
  2416. $delete_url = wp_nonce_url($delete_url, 'event-delete-link');
  2417.  
  2418. $delete_link = '<span><a id="event-delete-link" href="'.$delete_url.'" title="'.__('Delete Event', 'groupcalendar').'">'.__('Delete', 'groupcalendar').' &rarr;</a></span>';
  2419.  
  2420.  
  2421.  
  2422. } else { //load POST variables
  2423.  
  2424.  
  2425.  
  2426. if (!isset($_POST['create-event']))
  2427.  
  2428. return false;
  2429.  
  2430.  
  2431.  
  2432. $event_title = esc_attr(strip_tags(trim(stripslashes($_POST['event-title']))));
  2433.  
  2434. $tmp_date = $_POST['event-date'].' '.$_POST['event-hour'].':'.$_POST['event-minute'].$_POST['event-ampm'];
  2435.  
  2436. $tmp_date = strtotime($tmp_date);
  2437.  
  2438.  
  2439.  
  2440. //check for valid date/time
  2441.  
  2442. if ($tmp_date && $tmp_date >= time()) {
  2443.  
  2444. $event_date = date_i18n('Y-m-d', $tmp_date);
  2445.  
  2446.  
  2447.  
  2448. if ($bgc_locale['time_format']==12)
  2449.  
  2450. $event_hour = date_i18n('g', $tmp_date);
  2451.  
  2452. else
  2453.  
  2454. $event_hour = date_i18n('G', $tmp_date);
  2455.  
  2456.  
  2457.  
  2458. $event_minute = date_i18n('i', $tmp_date);
  2459.  
  2460. $event_ampm = date_i18n('a', $tmp_date);
  2461.  
  2462. } else {
  2463.  
  2464. $event_hour = 7;
  2465.  
  2466. }
  2467.  
  2468.  
  2469.  
  2470. $event_description = stripslashes(wp_filter_post_kses($_POST['event-desc']));
  2471.  
  2472. $event_location = esc_attr(strip_tags(trim(stripslashes($_POST['event-loc']))));
  2473.  
  2474. $event_map = ($_POST['event-map']==1) ? ' checked="checked"' : '';
  2475.  
  2476.  
  2477.  
  2478. }
  2479.  
  2480.  
  2481.  
  2482. ?>
  2483.  
  2484. <div class="bp-widget">
  2485.  
  2486. <h4>
  2487.  
  2488. <?php _e('Edit Event', 'groupcalendar'); ?>
  2489.  
  2490. <?php echo $delete_link; ?>
  2491.  
  2492. </h4>
  2493.  
  2494.  
  2495.  
  2496. <form action="<?php echo $url; ?>" name="add-event-form" id="add-event-form" class="standard-form" method="post" enctype="multipart/form-data">
  2497.  
  2498. <label for="event-title"><?php _e('Title', 'groupcalendar'); ?> *</label>
  2499.  
  2500. <input name="event-title" id="event-title" value="<?php echo $event_title; ?>" type="text">
  2501.  
  2502.  
  2503.  
  2504. <label for="event-date"><?php _e('Date', 'groupcalendar'); ?> *
  2505.  
  2506. <input name="event-date" id="event-date" value="<?php echo $event_date; ?>" type="text"></label>
  2507.  
  2508.  
  2509.  
  2510. <label for="event-time"><?php _e('Time', 'groupcalendar'); ?> *
  2511.  
  2512. <select name="event-hour" id="event-hour">
  2513.  
  2514. <?php
  2515.  
  2516. if ($bgc_locale['time_format']==24)
  2517.  
  2518. $bgc_locale['time_format'] = 23;
  2519.  
  2520. for ($i = 1; $i <= $bgc_locale['time_format']; $i++) {
  2521.  
  2522. $hour_check = ($i == $event_hour) ? ' selected="selected"' : '';
  2523.  
  2524. echo '<option value="'.$i.'"'.$hour_check.'>'.$i."</option>\n";
  2525.  
  2526. }
  2527.  
  2528. ?>
  2529.  
  2530. </select>
  2531.  
  2532. <select name="event-minute" id="event-minute">
  2533.  
  2534. <option value="00"<?php echo ($event_minute=='00') ? ' selected="selected"' : ''; ?>>:00</option>
  2535.  
  2536. <option value="15"<?php echo ($event_minute=='15') ? ' selected="selected"' : ''; ?>>:15</option>
  2537.  
  2538. <option value="30"<?php echo ($event_minute=='30') ? ' selected="selected"' : ''; ?>>:30</option>
  2539.  
  2540. <option value="45"<?php echo ($event_minute=='45') ? ' selected="selected"' : ''; ?>>:45</option>
  2541.  
  2542. </select>
  2543.  
  2544. <?php if ($bgc_locale['time_format']==12) : ?>
  2545.  
  2546. <select name="event-ampm" id="event-ampm">
  2547.  
  2548. <option value="am"<?php echo ($event_ampm=='am') ? ' selected="selected"' : ''; ?>>am</option>
  2549.  
  2550. <option value="pm"<?php echo ($event_ampm=='pm') ? ' selected="selected"' : ''; ?>>pm</option>
  2551.  
  2552. </select>
  2553.  
  2554. <?php endif; ?>
  2555.  
  2556. </label>
  2557.  
  2558.  
  2559.  
  2560. <label for="event-desc"><?php _e('Description', 'groupcalendar'); ?></label>
  2561.  
  2562. <?php if (function_exists('wp_editor')) {
  2563.  
  2564. wp_editor($event_description, 'event-desc', array('media_buttons' => false, 'dfw' => false));
  2565.  
  2566. } else {
  2567.  
  2568. the_editor($event_description, 'event-desc', 'event-desc', false);
  2569.  
  2570. }
  2571.  
  2572. ?>
  2573.  
  2574.  
  2575.  
  2576. <label for="event-loc"><?php _e('Location', 'groupcalendar'); ?></label>
  2577.  
  2578. <input name="event-loc" id="event-loc" value="<?php echo $event_location; ?>" type="text">
  2579.  
  2580.  
  2581.  
  2582. <label for="event-map"><?php _e('Show Map Link?', 'groupcalendar'); ?>
  2583.  
  2584. <input name="event-map" id="event-map" value="1" type="checkbox"<?php echo $event_map; ?> />
  2585.  
  2586. <small><?php _e('(Note: Location must be an address)', 'groupcalendar'); ?></small>
  2587.  
  2588. </label>
  2589.  
  2590.  
  2591.  
  2592. <?php if ($event_id) : ?>
  2593.  
  2594. <input name="event-id" id="event-id" value="<?php echo $event_id; ?>" type="hidden">
  2595.  
  2596. <?php endif; ?>
  2597.  
  2598.  
  2599.  
  2600. <input name="create-event" id="create-event" value="1" type="hidden">
  2601.  
  2602. <input name="group-id" id="group-id" value="<?php echo $bp->groups->current_group->id; ?>" type="hidden">
  2603.  
  2604. <?php wp_nonce_field('bp_group_calendar'); ?>
  2605.  
  2606.  
  2607.  
  2608. <?php echo $event_meta; ?>
  2609.  
  2610.  
  2611.  
  2612. <p><input value="<?php _e('Save Event', 'groupcalendar'); ?> &raquo;" id="save" name="save" type="submit"></p>
  2613.  
  2614.  
  2615.  
  2616. </form>
  2617.  
  2618.  
  2619.  
  2620. </div>
  2621.  
  2622. <?php
  2623.  
  2624.  
  2625.  
  2626. //return true if all is well
  2627.  
  2628. return true;
  2629.  
  2630. }
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636. class BP_Group_Calendar_Widget extends WP_Widget {
  2637.  
  2638.  
  2639.  
  2640. function BP_Group_Calendar_Widget() {
  2641.  
  2642. $widget_ops = array('classname' => 'bp_group_calendar', 'description' => __('Displays upcoming public group events.', 'groupcalendar') );
  2643.  
  2644. $this->WP_Widget('bp_group_calendar', __('Group Events', 'groupcalendar'), $widget_ops);
  2645.  
  2646. }
  2647.  
  2648.  
  2649.  
  2650. function widget($args, $instance) {
  2651.  
  2652. global $wpdb, $current_user, $bp;
  2653.  
  2654.  
  2655.  
  2656. extract( $args );
  2657.  
  2658.  
  2659.  
  2660. $date_format = get_option('date_format').' '.get_option('time_format');
  2661.  
  2662.  
  2663.  
  2664. echo $before_widget;
  2665.  
  2666. $title = $instance['title'];
  2667.  
  2668. if ( !empty( $title ) ) { echo $before_title . apply_filters('widget_title', $title) . $after_title; };
  2669.  
  2670.  
  2671.  
  2672. $events = $wpdb->get_results( "SELECT gc.id, gc.user_id, gc.event_title, gc.event_time, gp.name, gc.group_id FROM ".$wpdb->base_prefix."bp_groups_calendars gc JOIN ".$wpdb->base_prefix."bp_groups gp ON gc.group_id=gp.id WHERE gc.event_time >= '".date('Y-m-d H:i:s')."' AND gp.status = 'public' ORDER BY gc.event_time ASC LIMIT ".(int)$instance['num_events'] );
  2673.  
  2674.  
  2675.  
  2676. if ($events) {
  2677.  
  2678.  
  2679.  
  2680. echo '<ul class="events-list">';
  2681.  
  2682. //loop through events
  2683.  
  2684. $events_list = '';
  2685.  
  2686. foreach ($events as $event) {
  2687.  
  2688. $class = ($event->user_id==$current_user->ID) ? ' class="my_event"' : '';
  2689.  
  2690. $events_list .= "\n<li".$class.">";
  2691.  
  2692. $group = groups_get_group( array( 'group_id' => $event->group_id ) );
  2693.  
  2694. $url = bp_get_group_permalink($group).'calendar/event/'.$event->id.'/';
  2695.  
  2696. $events_list .= stripslashes($event->name).'<br /><a href="'.$url.'" title="'.__('View Event', 'groupcalendar').'">'.date_i18n($date_format, strtotime($event->event_time)).': '.stripslashes($event->event_title).'</a>';
  2697.  
  2698. $events_list .= "</li>";
  2699.  
  2700. }
  2701.  
  2702. echo $events_list;
  2703.  
  2704. echo "\n</ul>";
  2705.  
  2706.  
  2707.  
  2708. } else { ?>
  2709.  
  2710. <div class="widget-error">
  2711.  
  2712. <?php _e('There are no upcoming group events.', 'groupcalendar') ?>
  2713.  
  2714. </div>
  2715.  
  2716. <?php } ?>
  2717.  
  2718.  
  2719.  
  2720. <?php echo $after_widget; ?>
  2721.  
  2722. <?php
  2723.  
  2724. }
  2725.  
  2726.  
  2727.  
  2728. function update( $new_instance, $old_instance ) {
  2729.  
  2730. $instance = $old_instance;
  2731.  
  2732. $instance['title'] = strip_tags( $new_instance['title'] );
  2733.  
  2734. $instance['num_events'] = strip_tags( $new_instance['num_events'] );
  2735.  
  2736.  
  2737.  
  2738. return $instance;
  2739.  
  2740. }
  2741.  
  2742.  
  2743.  
  2744. function form( $instance ) {
  2745.  
  2746. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Upcoming Group Events', 'groupcalendar'), 'num_events' => 10 ) );
  2747.  
  2748. $title = strip_tags($instance['title']);
  2749.  
  2750. $num_events = strip_tags($instance['num_events']);
  2751.  
  2752. ?>
  2753.  
  2754. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  2755.  
  2756. <p><label for="<?php echo $this->get_field_id('num_events'); ?>"><?php _e('Number of Events:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('num_events'); ?>" name="<?php echo $this->get_field_name('num_events'); ?>" type="text" value="<?php echo esc_attr($num_events); ?>" style="width: 30%" /></label></p>
  2757.  
  2758. <?php
  2759.  
  2760. }
  2761.  
  2762. }
  2763.  
  2764.  
  2765.  
  2766. class BP_Group_Calendar_Widget_Single extends WP_Widget {
  2767.  
  2768.  
  2769.  
  2770. function BP_Group_Calendar_Widget_Single() {
  2771.  
  2772. $widget_ops = array('classname' => 'bp_group_calendar_single', 'description' => __('Displays upcoming group events for a single group.', 'groupcalendar') );
  2773.  
  2774. $this->WP_Widget('bp_group_calendar_single', __('Single Group Events', 'groupcalendar'), $widget_ops);
  2775.  
  2776. }
  2777.  
  2778.  
  2779.  
  2780. function widget($args, $instance) {
  2781.  
  2782. global $wpdb, $current_user, $bp;
  2783.  
  2784.  
  2785.  
  2786. extract( $args );
  2787.  
  2788.  
  2789.  
  2790. $date_format = get_option('date_format').' '.get_option('time_format');
  2791.  
  2792.  
  2793.  
  2794. echo $before_widget;
  2795.  
  2796. $title = $instance['title'];
  2797.  
  2798. if ( !empty( $title ) ) { echo $before_title . apply_filters('widget_title', $title) . $after_title; };
  2799.  
  2800.  
  2801.  
  2802. $events = $wpdb->get_results( "SELECT gc.id, gc.user_id, gc.event_title, gc.event_time, gp.name, gc.group_id FROM ".$wpdb->base_prefix."bp_groups_calendars gc JOIN ".$wpdb->base_prefix."bp_groups gp ON gc.group_id=gp.id WHERE gc.event_time >= '".date('Y-m-d H:i:s')."' AND gp.id = ".(int)$instance['group_id']." ORDER BY gc.event_time ASC LIMIT ".(int)$instance['num_events'] );
  2803.  
  2804.  
  2805.  
  2806. if ($events) {
  2807.  
  2808.  
  2809.  
  2810. echo '<ul class="events-list">';
  2811.  
  2812. //loop through events
  2813.  
  2814. $events_list = '';
  2815.  
  2816. foreach ($events as $event) {
  2817.  
  2818. $class = ($event->user_id==$current_user->ID) ? ' class="my_event"' : '';
  2819.  
  2820. $events_list .= "\n<li".$class.">";
  2821.  
  2822. $group = groups_get_group( array( 'group_id' => $event->group_id ) );
  2823.  
  2824. $url = bp_get_group_permalink($group).'calendar/event/'.$event->id.'/';
  2825.  
  2826. $events_list .= stripslashes($event->name).'<br /><a href="'.$url.'" title="'.__('View Event', 'groupcalendar').'">'.date_i18n($date_format, strtotime($event->event_time)).': '.stripslashes($event->event_title).'</a>';
  2827.  
  2828. $events_list .= "</li>";
  2829.  
  2830. }
  2831.  
  2832. echo $events_list;
  2833.  
  2834. echo "\n</ul>";
  2835.  
  2836.  
  2837.  
  2838. } else { ?>
  2839.  
  2840. <div class="widget-error">
  2841.  
  2842. <?php _e('There are no upcoming events for this group.', 'groupcalendar') ?>
  2843.  
  2844. </div>
  2845.  
  2846. <?php } ?>
  2847.  
  2848.  
  2849.  
  2850. <?php echo $after_widget; ?>
  2851.  
  2852. <?php
  2853.  
  2854. }
  2855.  
  2856.  
  2857.  
  2858. function update( $new_instance, $old_instance ) {
  2859.  
  2860. $instance = $old_instance;
  2861.  
  2862. $instance['title'] = strip_tags( $new_instance['title'] );
  2863.  
  2864. $instance['num_events'] = strip_tags( $new_instance['num_events'] );
  2865.  
  2866. $instance['group_id'] = (int)$new_instance['group_id'];
  2867.  
  2868.  
  2869.  
  2870. return $instance;
  2871.  
  2872. }
  2873.  
  2874.  
  2875.  
  2876. function form( $instance ) {
  2877.  
  2878. global $wpdb;
  2879.  
  2880. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Upcoming Group Events', 'groupcalendar'), 'num_events' => 10 ) );
  2881.  
  2882. $title = strip_tags($instance['title']);
  2883.  
  2884. $num_events = strip_tags($instance['num_events']);
  2885.  
  2886. $group_id = $instance['group_id'];
  2887.  
  2888. ?>
  2889.  
  2890. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  2891.  
  2892. <p><label for="<?php echo $this->get_field_id('num_events'); ?>"><?php _e('Number of Events:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('num_events'); ?>" name="<?php echo $this->get_field_name('num_events'); ?>" type="text" value="<?php echo esc_attr($num_events); ?>" style="width: 30%" /></label></p>
  2893.  
  2894.  
  2895.  
  2896. <?php
  2897.  
  2898. $groups = $wpdb->get_results( "SELECT id, name FROM {$wpdb->base_prefix}bp_groups ORDER BY name LIMIT 999"); //we don't want thousands of groups in the dropdown.
  2899.  
  2900. if ($groups) {
  2901.  
  2902. echo '<p><label for="'.$this->get_field_id('group_id').'">'.__('Group:', 'groupcalendar').' <select class="widefat" id="'.$this->get_field_id('group_id').'" name="'.$this->get_field_name('group_id').'">';
  2903.  
  2904.  
  2905.  
  2906. foreach ($groups as $group)
  2907.  
  2908. echo '<option value="'.$group->id.'"'.(($group_id == $group->id) ? ' selected="selected"' : '').'>'.esc_attr($group->name).'</option>';
  2909.  
  2910.  
  2911.  
  2912. echo '</select></label></p>';
  2913.  
  2914. }
  2915.  
  2916. }
  2917.  
  2918. }
  2919.  
  2920.  
  2921.  
  2922. class BP_Group_Calendar_Widget_User_Groups extends WP_Widget {
  2923.  
  2924.  
  2925.  
  2926. function BP_Group_Calendar_Widget_User_Groups() {
  2927.  
  2928. $widget_ops = array('classname' => 'bp_group_calendar_user_groups', 'description' => __('Displays upcoming group events for a logged in user\'s groups.', 'groupcalendar') );
  2929.  
  2930. $this->WP_Widget('bp_group_calendar_user_groups', __('User\'s Group Events', 'groupcalendar'), $widget_ops);
  2931.  
  2932. }
  2933.  
  2934.  
  2935.  
  2936. function widget($args, $instance) {
  2937.  
  2938. global $wpdb, $current_user, $bp;
  2939.  
  2940.  
  2941.  
  2942. //only show widget to logged in users
  2943.  
  2944. if (!is_user_logged_in())
  2945.  
  2946. return;
  2947.  
  2948.  
  2949.  
  2950. //get the groups the user is part of
  2951.  
  2952. $results = groups_get_user_groups($current_user->ID);
  2953.  
  2954.  
  2955.  
  2956. //don't show widget if user doesn't have any groups
  2957.  
  2958. if ($results['total'] == 0)
  2959.  
  2960. return;
  2961.  
  2962.  
  2963.  
  2964. extract( $args );
  2965.  
  2966.  
  2967.  
  2968. $date_format = get_option('date_format').' '.get_option('time_format');
  2969.  
  2970.  
  2971.  
  2972. echo $before_widget;
  2973.  
  2974. $title = $instance['title'];
  2975.  
  2976. if ( !empty( $title ) ) { echo $before_title . apply_filters('widget_title', $title) . $after_title; };
  2977.  
  2978.  
  2979.  
  2980. $group_ids = implode(',',$results['groups']);
  2981.  
  2982.  
  2983.  
  2984. $events = $wpdb->get_results( "SELECT gc.id, gc.user_id, gc.event_title, gc.event_time, gp.name, gc.group_id FROM ".$wpdb->base_prefix."bp_groups_calendars gc JOIN ".$wpdb->base_prefix."bp_groups gp ON gc.group_id=gp.id WHERE gc.event_time >= '".date('Y-m-d H:i:s')."' AND gp.id IN ($group_ids) ORDER BY gc.event_time ASC LIMIT ".(int)$instance['num_events'] );
  2985.  
  2986.  
  2987.  
  2988. if ($events) {
  2989.  
  2990.  
  2991.  
  2992. echo '<ul class="events-list">';
  2993.  
  2994. //loop through events
  2995.  
  2996. $events_list = '';
  2997.  
  2998. foreach ($events as $event) {
  2999.  
  3000. $class = ($event->user_id==$current_user->ID) ? ' class="my_event"' : '';
  3001.  
  3002. $events_list .= "\n<li".$class.">";
  3003.  
  3004. $group = groups_get_group( array( 'group_id' => $event->group_id ) );
  3005.  
  3006. $url = bp_get_group_permalink($group).'calendar/event/'.$event->id.'/';
  3007.  
  3008. $events_list .= stripslashes($event->name).'<br /><a href="'.$url.'" title="'.__('View Event', 'groupcalendar').'">'.date_i18n($date_format, strtotime($event->event_time)).': '.stripslashes($event->event_title).'</a>';
  3009.  
  3010. $events_list .= "</li>";
  3011.  
  3012. }
  3013.  
  3014. echo $events_list;
  3015.  
  3016. echo "\n</ul>";
  3017.  
  3018.  
  3019.  
  3020. } else { ?>
  3021.  
  3022. <div class="widget-error">
  3023.  
  3024. <?php _e('There are no upcoming events for your groups.', 'groupcalendar') ?>
  3025.  
  3026. </div>
  3027.  
  3028. <?php } ?>
  3029.  
  3030.  
  3031.  
  3032. <?php echo $after_widget; ?>
  3033.  
  3034. <?php
  3035.  
  3036. }
  3037.  
  3038.  
  3039.  
  3040. function update( $new_instance, $old_instance ) {
  3041.  
  3042. $instance = $old_instance;
  3043.  
  3044. $instance['title'] = strip_tags( $new_instance['title'] );
  3045.  
  3046. $instance['num_events'] = strip_tags( $new_instance['num_events'] );
  3047.  
  3048.  
  3049.  
  3050. return $instance;
  3051.  
  3052. }
  3053.  
  3054.  
  3055.  
  3056. function form( $instance ) {
  3057.  
  3058. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Your Upcoming Group Events', 'groupcalendar'), 'num_events' => 10 ) );
  3059.  
  3060. $title = strip_tags($instance['title']);
  3061.  
  3062. $num_events = strip_tags($instance['num_events']);
  3063.  
  3064. ?>
  3065.  
  3066. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  3067.  
  3068. <p><label for="<?php echo $this->get_field_id('num_events'); ?>"><?php _e('Number of Events:', 'groupcalendar') ?> <input class="widefat" id="<?php echo $this->get_field_id('num_events'); ?>" name="<?php echo $this->get_field_name('num_events'); ?>" type="text" value="<?php echo esc_attr($num_events); ?>" style="width: 30%" /></label></p>
  3069.  
  3070. <?php
  3071.  
  3072. }
  3073.  
  3074. }
  3075.  
  3076.  
  3077.  
  3078. //------------------------------------------------------------------------//
  3079.  
  3080.  
  3081.  
  3082. //---Support Functions----------------------------------------------------//
  3083.  
  3084.  
  3085.  
  3086. //------------------------------------------------------------------------//
  3087.  
  3088.  
  3089.  
  3090.  
  3091.  
  3092.  
  3093.  
  3094. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement