<?php
/* wppa-notify-widget.php
* Package: wp-photo-album-plus
*
* notify events to users
* Version 8.0.08.001
*/

class wppaNotifyWidget extends WP_Widget {

    /** constructor */
    function __construct() {
		$widget_ops = array( 'classname' => 'wppa_notify_widget', 'description' => __( 'Let users select if they want to get emails on selected events', 'wp-photo-album-plus' ) );
		parent::__construct( 'wppa_notify_widget', __( 'WPPA+ Notify Me', 'wp-photo-album-plus' ), $widget_ops );
    }

	/** @see WP_Widget::widget */
    function widget( $args, $instance ) {

		// Initialize
		wppa_widget_timer( 'init' );
		wppa_reset_occurrance();
        wppa( 'in_widget', 'notify' );
		wppa_bump_mocc();
        extract( $args );
		$instance 		= wppa_parse_args( (array) $instance, $this->get_defaults() );
		$widget_title 	= apply_filters( 'widget_title', $instance['title'] );

		// Logged in only and logged out?
		if ( wppa_checked( $instance['logonly'] ) && ! is_user_logged_in() ) {
			return;
		}

		// Make the widget content
		$widget_content = '<!-- WPPA+ notify Widget start -->';
		$widget_content .= __( 'Notify me when...', 'wp-photo-album-plus' ) . '<br />';

		// Get the body of the widget
		$body = '';

		// New album
		if ( wppa_switch( 'newalbumnotify' ) ) {
			$in_list = wppa_am_i_in_mailinglist( 'newalbumnotify' );
			$body .= '
			<div style="clear:both;" >
				<input
					id="wppa-newalbum-notify"
					class="wppa-notify-check"
					type="checkbox"
					style="float:left;" ' .
					( $in_list ? 'checked="checked" ' : ' ' ) .
					'onchange="wppaAjaxNotify(this,\'newalbumnotify\');"
				/>
				<label
					for="wppa-newalbum-notify"
					class="wppa-notify-label"
					style="float:left;padding-left:4px;width:90%;"
					> ' .
					__( 'A new album is created', 'wp-photo-album-plus' ) . '
				</label>
			</div>';
		}

		// Only if fe upload notify is activated
		if ( wppa_switch( 'feuploadnotify' ) ) {
			$in_list = wppa_am_i_in_mailinglist( 'feuploadnotify' );
			$body .= '
			<div style="clear:both;" >
				<input
					id="wppa-feupload-notify"
					class="wppa-notify-check"
					type="checkbox"
					style="float:left;" ' .
					( $in_list ? 'checked="checked" ' : ' ' ) .
					'onchange="wppaAjaxNotify(this,\'feuploadnotify\');"
				/>
				<label
					for="wppa-feupload-notify"
					class="wppa-notify-label"
					style="float:left;padding-left:4px;width:90%;"
					> ' .
					__( 'A new photo is uploaded', 'wp-photo-album-plus' ) . '
				</label>
			</div>';
		}

		// Only if upload moderation is activated
		if ( wppa_switch( 'upload_moderate' ) ) {

			// Photo approved
			if ( wppa_switch( 'photoapproved' ) ) {
				$in_list = wppa_am_i_in_mailinglist( 'photoapproved' );
				$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-photoapproved-notify"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'photoapproved\');"
					/>
					<label
						for="wppa-photoapproved-notify"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						__( 'My photo is approved', 'wp-photo-album-plus' ) . '
					</label>
				</div>';
			}
		}

		// Only if comments sytem is activated
		if ( wppa_switch( 'show_comments' ) ) {

			// A comment is given
			if ( wppa_switch( 'commentnotify' ) ) {
				$in_list = wppa_am_i_in_mailinglist( 'commentnotify' );
								$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-comment-notify"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'commentnotify\');"
					/>
					<label
						for="wppa-comment-notify"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						( wppa_switch( 'commentnotify_limit' ) ?
							__( 'A comment on my photo is given', 'wp-photo-album-plus' ) :
							__( 'A comment on any photo is given', 'wp-photo-album-plus' ) ) . '
					</label>
				</div>';
			}

			// Comment approved (to photo owner and commenter)
			if ( wppa_switch( 'commentapproved' ) ) {
				$in_list = wppa_am_i_in_mailinglist( 'commentapproved' );
				$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-commentapproved-notify"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'commentapproved\');"
					/>
					<label
						for="wppa-commentapproved-notify"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						__( 'A comment on my photo is approved or my comment is approved', 'wp-photo-album-plus' ) . '
					</label>
				</div>';
			}

			// Commented previous (to photo owner)
			if ( wppa_switch( 'commentprevious' ) ) {
				$in_list = wppa_am_i_in_mailinglist( 'commentprevious' );
				$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-commentprevious-notify"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'commentprevious\');"
					/>
					<label
						for="wppa-commentprevious-notify"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						__( 'A comment is given on a photo that i commented before', 'wp-photo-album-plus' ) . '
					</label>
				</div>';
			}
		}

		// Only show to moderators
		if ( current_user_can( 'wppa_moderate' ) ) {

			// Only if photo modaration activated
			if ( wppa_switch( 'upload_moderate' ) ) {
				$in_list = wppa_am_i_in_mailinglist( 'moderatephoto' );
				$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-upload-moderate"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'moderatephoto\');"
					/>
					<label
						for="wppa-upload-moderate"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						__( 'A photo needs moderation', 'wp-photo-album-plus' ) . '
					</label>
				</div>';
			}

			// Only if comment moderation activated
			if ( wppa_opt( 'moderate_comment' ) != '-none-' ) {
				$in_list = wppa_am_i_in_mailinglist( 'moderatecomment' );
				$body .= '
				<div style="clear:both;" >
					<input
						id="wppa-upload-moderate-comment"
						class="wppa-notify-check"
						type="checkbox"
						style="float:left;" ' .
						( $in_list ? 'checked="checked" ' : ' ' ) .
						'onchange="wppaAjaxNotify(this,\'moderatecomment\');"
					/>
					<label
						for="wppa-upload-moderate-comment"
						class="wppa-notify-label"
						style="float:left;padding-left:4px;width:90%;"
						> ' .
						__( 'A comment needs moderation', 'wp-photo-album-plus' ) . '
					</label>
				</div>';
			}
		}

		// Nothing to show?
		if ( ! $body ) {
			return;
		}

		$widget_content .= $body;
		$widget_content .= '<input type="hidden" id="wppa-ntfy-nonce" value="' . wp_create_nonce( 'wppa-ntfy-nonce' ) . '" />';
		$widget_content .= '<div style="clear:both" ></div>';
		$widget_content .= "\n".'<!-- WPPA+ notify Widget end -->';

		// Output
		$result = "\n" . $before_widget;
		if ( ! empty( $widget_title ) ) {
			$result .= $before_title . $widget_title . $after_title;
		}
		$result .= $widget_content . $after_widget;

		echo wppa_compress_html( $result );
		echo wppa_widget_timer( 'show', $widget_title );

		wppa( 'in_widget', false );
	}

    /** @see WP_Widget::update */
    function update( $new_instance, $old_instance ) {

		// Completize all parms
		$instance = wppa_parse_args( $new_instance, $this->get_defaults() );

		// Sanitize certain args
		$instance['title'] 		= strip_tags( $instance['title'] );

		wppa_remove_widget_cache( $this->id );

        return $instance;
    }

    /** @see WP_Widget::form */
    function form( $instance ) {

		// Defaults
		$instance = wppa_parse_args( (array) $instance, $this->get_defaults() );

		// Title
		echo
		wppa_widget_input( $this, 'title', $instance['title'], __( 'Title', 'wp-photo-album-plus' ) );
    }

	// Set defaults
	function get_defaults() {

		$defaults = array( 	'title' 	=> __( 'Notify me', 'wp-photo-album-plus' ),
							'logonly' 	=> 'yes',
							'cache' 	=> '0',
							);
		return $defaults;
	}


} // class wppaNotifyWidget

// register wppaNotifyWidget widget
add_action( 'widgets_init', 'wppa_register_wppaNotifyWidget' );

function wppa_register_wppaNotifyWidget() {

	if ( get_option( 'wppa_email_on', 'yes' ) == 'yes' ) {
		register_widget( "wppaNotifyWidget" );
	}
}