<?php
/* wppa-dashboard-widgets.php
* Package: wp-photo-album-plus
*
* Contains dashboard widgets code
*
* Version 8.0.07.004
*
*/

// Email subscription dashboard widget
add_action( 'do_meta_boxes', 'wppa_email_subscription' );

function wppa_email_subscription() {

	if ( ! wppa_switch( 'email_on' ) ) {
		return;
	}

	$doit =
	wppa_switch( 'newalbumnotify' ) ||
	wppa_switch( 'feuploadnotify' ) ||
	( wppa_switch( 'commentnotify' ) && wppa_switch( 'show_comments' ) ) ||
	( wppa_switch( 'commentprevious' ) && wppa_switch( 'show_comments' ) )||
	( wppa_switch( 'moderatephoto' ) && current_user_can( 'wppa_moderate' ) ) ||
	( wppa_switch( 'moderatecomment' ) && wppa_switch( 'show_comments' ) && current_user_can( 'wppa_moderate' ) ) ||
	( wppa_switch( 'photoapproved' ) && wppa_switch( 'upload_moderate' ) ) ||
	( wppa_switch( 'commentapproved' ) && wppa_switch( 'show_comments' ) );

	if ( $doit && function_exists( 'wp_add_dashboard_widget' ) ) {
		wp_add_dashboard_widget( 'wppa-email-subscription', __( 'Notify me', 'wp-photo-album-plus' ), 'wppa_show_email_subscription' ); //, $control_callback = null, $callback_args = null )
	}
}

function wppa_show_email_subscription() {

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

	// New album
	if ( wppa_switch( 'newalbumnotify' ) ) {
		$in_list = wppa_am_i_in_mailinglist( 'newalbumnotify' );
		$body .= '
		<li style="padding-top:12px;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%;margin-top:-3px;"
				> ' .
				__( 'A new album is created', 'wp-photo-album-plus' ) . '
			</label>
		</li>';
	}

	// Only if fe upload notify is activated
	if ( wppa_switch( 'feuploadnotify' ) ) {
		$in_list = wppa_am_i_in_mailinglist( 'feuploadnotify' );
		$body .= '
		<li style="padding-top:12px;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%;margin-top:-3px;"
				> ' .
				__( 'A new photo is uploaded', 'wp-photo-album-plus' ) . '
			</label>
		</li>';
	}

	// 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 .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					__( 'My photo is approved', 'wp-photo-album-plus' ) . '
				</label>
			</li>';
		}
	}

	// 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 .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					( ( wppa_switch( 'commentnotify_limit' ) && ! wppa_user_is( 'administrator' ) ) ?
						__( 'A comment on my photo is given', 'wp-photo-album-plus' ) :
						__( 'A comment on any photo is given', 'wp-photo-album-plus' ) ) . '
				</label>
			</li>';
		}

		// Comment approved (to photo owner and commenter)
		if ( wppa_switch( 'commentapproved' ) ) {
			$in_list = wppa_am_i_in_mailinglist( 'commentapproved' );
			$body .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					__( 'A comment on my photo is approved or my comment is approved', 'wp-photo-album-plus' ) . '
				</label>
			</li>';
		}

		// Commented previous (to photo owner)
		if ( wppa_switch( 'commentprevious' ) ) {
			$in_list = wppa_am_i_in_mailinglist( 'commentprevious' );
			$body .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					__( 'A comment is given on a photo that i commented before', 'wp-photo-album-plus' ) . '
				</label>
			</li>';
		}

	}

	// 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 .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					__( 'A photo needs moderation', 'wp-photo-album-plus' ) . '
				</label>
			</li>';
		}

		// Only if comment moderation activated
		if ( wppa_opt( 'moderate_comment' ) != '-none-' ) {
			$in_list = wppa_am_i_in_mailinglist( 'moderatecomment' );
			$body .= '
			<li style="padding-top:12px;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%;margin-top:-3px;"
					> ' .
					__( 'A comment needs moderation', 'wp-photo-album-plus' ) . '
				</label>
			</li>';
		}
	}

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

	echo
	__( 'Notify me when...', 'wp-photo-album-plus' ) . '
	<br />
	<ul>' . $body . '</ul>
	<input type="hidden" id="wppa-ntfy-nonce" value="' . wp_create_nonce( 'wppa-ntfy-nonce' ) . '" />
	<div style="clear:both;" ></div>';
}

// Activity feed
add_action( 'do_meta_boxes', 'wppa_activity' );

function wppa_activity(){

	// Are we configured to show the activity widgets?
	switch ( wppa_opt( 'show_dashboard_widgets' ) ) {
		case 'all': $doit = true; break;
		case 'admin': $doit = current_user_can( 'administrator' ); break;
		default: $doit = false;
	}

	if ( $doit && function_exists( 'wp_add_dashboard_widget' ) ) {
		wp_add_dashboard_widget( 'wppa-activity', __( 'Recent WPPA activity', 'wp-photo-album-plus' ), 'wppa_show_activity_feed' ); //, $control_callback = null, $callback_args = null )
	}
}

function wppa_show_activity_feed() {
global $wpdb;

	// Recently uploaded photos
	echo '<h3>' . __( 'Recently uploaded photos', 'wp-photo-album-plus' ) . '</h3>';

	$void = wppa_get_void_pids();
	if ( count ( $void ) ) {
		$void_clause = " WHERE id NOT IN (" . implode( ',', $void ) . ") ";
	}
	else {
		$void_clause = "";
	}

	$photos = $wpdb->get_results( "SELECT * FROM $wpdb->wppa_photos " .
								   $void_clause . "
								   ORDER BY timestamp DESC LIMIT 5", ARRAY_A );

	if ( ! empty( $photos ) ) {
		echo
		'<table>';
		foreach( $photos as $photo ) {
			$id = $photo['id'];
			if ( wppa_user_is( 'administrator' ) ) {
				$href = get_admin_url() . 'admin.php?page=wppa_admin_menu&tab=edit&edit-id=single&photo=' . $photo['id'] . '&nonce=' . wp_create_nonce( 'wppa-nonce' );
			}
			else {
				$href = wppa_get_photo_url( $id );
			}
			echo
			'<tr>' .
				'<td>' .
					'<a href="' . esc_url( $href ) . '" target="_blank" >';

						if ( wppa_is_video( $id ) ) {
							echo
							'<div style="position:relative;" >';
								echo wppa_get_video_html( array( 'id'			=> $id,
																 'width'		=> '50',
																 'controls' 	=> false,
																 'preload' 		=> 'metadata',
																 'use_thumb' 	=> true,
																 'autoplay' 	=> false
																 ) );
								$url = WPPA_UPLOAD_URL . '/icons/' . wppa_opt( 'video_icon' );
								echo
								'<img src="' . esc_url( $url ) . '" style="width:16px;height:16px;position:absolute;right:0;bottom:0;z-index:100;" />' .
							'</div>';
						}
						else {
							echo
							'<img src="' . esc_url( wppa_get_thumb_url( $id ) ) . '" style="max-width:50px;max-height:50px;" /> ';
						}

					echo
					'</a>' .
				'</td>' .
				'<td>';
					$usr = wppa_get_user_by( 'login', $photo['owner'] );
					if ( $usr ) {
						$usr = $usr -> display_name;
					}
					else {
						$usr = sanitize_user( $photo['owner'] );
					}
					echo
					sprintf( 	__( 'by %s in album %s', 'wp-photo-album-plus' ),
								'<b>' . $usr . '</b>',
								'<b>' . sanitize_text_field( wppa_get_album_name( $photo['album'] ) ) . '</b> (' . strval( intval( $photo['album'] ) ) . ')'
								) .
					'<br />' .
					wppa_local_date( '', $photo['timestamp'] ) .
				'</td>' .
			'</tr>';
		}
		echo
		'</table>';
	}
	else {
		echo
		'<p>' .
			__( 'There are no recently uploaded photos', 'wp-photo-album-plus' ) .
		'</p>';
	}
	echo '<br />';

	// Recent comments
	echo '<h3>' . __( 'Recent comments on photos', 'wp-photo-album-plus' ) . '</h3>';
	$comments = $wpdb->get_results( "SELECT * FROM $wpdb->wppa_comments ORDER BY timestamp DESC LIMIT 5", ARRAY_A );
	if ( ! empty( $comments ) ) {

		echo
		'<table>';
		foreach( $comments as $comment ) {
			$photo = wppa_cache_photo( $comment['photo'] );
			if ( wppa_user_is( 'administrator' ) ) {
				$href = get_admin_url() . 'admin.php?page=wppa_admin_menu&tab=edit&edit-id=single&photo=' . $photo['id'] . '&nonce=' . wp_create_nonce( 'wppa-nonce' );
			}
			else {
				$href = wppa_get_photo_url( $photo['id'] );
			}
			echo
			'<tr>' .
				'<td>' .
					'<a href="' . esc_url( $href ) . '" target="_blank" >' .
						'<img src="' . esc_url( wppa_get_thumb_url( $photo['id'] ) ) . '" style="max-width:50px;max-height:50px;" /> ' .
					'</a>' .
				'</td>' .
				'<td>' .
					'<i>' . sanitize_text_field( $comment['comment'] ) . '</i>' .
					'<br />' .
					sprintf(	__( 'by %s', 'wp-photo-album-plus' ),
								'<b>' . htmlspecialchars( $comment['user'] ) . '</b>' ) .
					'<br />' .
					wppa_local_date( '', $comment['timestamp'] ) .
				'</td>' .
			'</tr>';
		}
		echo
		'</table>';
	}
	else {
		echo
		'<p>' .
			__( 'There are no recent comments on photos', 'wp-photo-album-plus' ) .
		'</p>';
	}

}

// Photo of the day history. This is undocumented and no setting for available.
if ( wppa_get_option( 'wppa_potd_log', 'no' ) == 'yes' ) {
	add_action( 'do_meta_boxes', 'wppa_potdlog' );
}

function wppa_potdlog() {

	// Are we configured to show the activity widgets?
	switch ( wppa_opt( 'show_dashboard_widgets' ) ) {
		case 'all': $doit = true; break;
		case 'admin': $doit = current_user_can( 'administrator' ); break;
		default: $doit = false;
	}

	if ( $doit && function_exists( 'wp_add_dashboard_widget' ) ) {
		wp_add_dashboard_widget( 'wppa-potdlog', __( 'Photo of the day history', 'wp-photo-album-plus' ), 'wppa_show_potd_log' );
	}
}

function wppa_show_potd_log() {

	// Get data
	$his = wppa_get_option( 'wppa_potd_log_data', array() );
	if ( ! empty( $his ) ) {
		echo
		'<table>';
		foreach( $his as $item ) {
			if ( wppa_photo_exists( $item['id'] ) ) {
				$photo = wppa_cache_photo( $item['id'] );
				$time  = $item['tm'];
				if ( wppa_user_is( 'administrator' ) ) {
					$href = get_admin_url() . 'admin.php?page=wppa_admin_menu&tab=edit&edit-id=single&photo=' . $photo['id'] . '&nonce=' . wp_create_nonce( 'wppa-nonce' );
				}
				else {
					$href = wppa_get_photo_url( $photo['id'] );
				}
				echo
				'<tr style="border-bottom:1px solid #444;" >' .
					'<td>' .
						'<a href="' . esc_url( $href ) . '" target="_blank" >' .
							'<img src="' . esc_url( wppa_get_thumb_url( $photo['id'] ) ) . '" style="max-width:50px;max-height:50px;" /> ' .
						'</a>' .
					'</td>' .
					'<td>' .
						__( 'First displayed at', 'wp-photo-album-plus' ) . ': ' . wppa_local_date( '', $time ) . '<br />' .
						__( 'Name', 'wp-photo-album-plus' ) . ': ' . wppa_get_photo_name( $photo['id'] ) . '<br />' .
						__( 'Description', 'wp-photo-album-plus' ) . ':<br />' .
						htmlspecialchars( strip_tags( wppa_get_photo_desc( $photo['id'] ) ) ) .
					'</td>' .
				'</tr>';
			}
			else {
				echo
				'<tr style="border-bottom:1px solid #444;" >' .
					'<td>' .
						sprintf( __( 'Photo %d has been removed' ), $item['id'] ) .
					'</td>' .
					'<td>' .
						__( 'First displayed at', 'wp-photo-album-plus' ) . ': ' . wppa_local_date( '', $item['tm'] ) . '<br />' .
					'</td>' .
				'</tr>';
			}
		}
		echo
		'</table>';
	}
	else {
		echo
		'<p>' .
			__( 'There is no photo of the day history', 'wp-photo-album-plus' ) .
		'</p>';
	}
}