<?php
/* wppa-wpdb-update.php
* Package: wp-photo-album-plus
*
* Contains low-level wpdb routines that update records
* Version 8.0.09.002
*
*/

if ( ! defined( 'ABSPATH' ) ) die( "Can't load this file directly" );

// Album
function wppa_update_album( $args ) {
global $wpdb;

	if ( ! is_array( $args ) ) {
		if ( wppa_is_int( $args ) ) {
			$args = array( 'id' => $args, 'modified' => time() );
		}
		else {
			return false;
		}
	}
	if ( ! $args['id'] ) return false;
	if ( ! wppa_cache_album( $args['id'] ) ) return false;
	$id = $args['id'];

	// If name update, also update sname
	if ( isset( $args['name'] ) ) {
		$args['sname'] = wppa_sanitize_album_photo_name( wppa_strip_tags( $args['name']  ) );
	}

	$need_re_index = false;
	foreach ( array_keys( $args ) as $itemname ) {
		$itemvalue = $args[$itemname];
		$doit = false;

		// Sanitize input
		switch( $itemname ) {
			case 'id':
				break;
			case 'name':
				$itemvalue = wppa_strip_tags( $itemvalue, 'all' );
				$doit = true;
				$need_re_index = true;
				break;
			case 'description':
				$itemvalue = balanceTags( $itemvalue, true );
				$itemvalue = wppa_strip_tags( $itemvalue, 'script&style' );
				$doit = true;
				$need_re_index = true;
				break;
			case 'modified':
				if ( ! $itemvalue ) {
					$itemvalue = time();
				}
				$doit = true;
				// Spurious issue exists after migration that timestamp is empty
				$t = $wpdb->get_var( $wpdb->prepare( "SELECT timestamp FROM $wpdb->wppa_albums WHERE id = %d", $args['id'] ) );
				if ( ! $t ) {
					$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->wppa_albums SET timestamp = modified WHERE id = %d", $args['id'] ) );
				}
				break;
			case 'cats':
				$itemvalue = wppa_sanitize_tags( $itemvalue );
				$doit = true;
				$need_re_index = true;
				break;
			case 'scheduledtm':
				$doit = true;
				break;
			case 'main_photo':
				if ( wppa_is_int( $itemvalue ) ) {
					$doit = true;
				}
				break;
			case 'crypt':
				$doit = true;
				break;
			case 'custom':
				$doit = true;
				$need_re_index = true;
				break;
			case 'sname':
				$doit = true;
				break;
			case 'cover_type':
				if ( in_array( $itemvalue, array( '',
												  'default',
												  'longdesc',
												  'imagefactory',
												  'default-mcr',
												  'longdesc-mcr',
												  'imagefactory-mcr',
												  ) ) ) {
					$doit = true;
				}
				break;
			case 'cover_linktype':
				if ( in_array( $itemvalue, array( 'content', 'albums', 'thumbs', 'slide', 'page', 'none' ) ) ) {
					$doit = true;
				}
				break;
			case 'zoomable':
				if ( in_array( $itemmvalue, array( 'on', '', 'off' ) ) ) {
					$doit = true;
				}
			case 'displayopts':
				$doit = true;
				break;
			case 'upload_limit_tree':
				if ( is_numeric( $itemvalue ) ) {
					$doit = true;
				}
				break;
			case 'scheduledel':
				$doit = true;
				break;
			case 'status':
				if ( in_array( $itemvalue, array( 'publish', 'private', 'hidden' ) ) ) {
					$doit = true;
				}
			default:
				wppa_log( 'Error', 'Not implemented in wppa_update_album(): '.$itemname );
				return false;
		}

		if ( $doit ) {
			if ( $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->wppa_albums SET ".$itemname." = %s WHERE id = %s LIMIT 1", $itemvalue, $id ) ) ) {
				wppa_cache_album( 'invalidate' );
			}
		}
	}

	// Update index
	if ( $need_re_index ) {
		wppa_schedule_maintenance_proc( 'wppa_remake_index_albums' );

	}

	// Clear associated caches
	wppa_clear_cache( array( 'album' => $id ) );

	return true;

/*
		a_order,
		main_photo,
		a_parent,
		p_order_by,
		cover_linktype,
		cover_linkpage,
		owner,
		upload_limit,
		alt_thumbsize,
		default_tags,
		cover_type,
		suba_order_by,
		views,
		cats
*/
}

// Photo
function wppa_update_photo( $args ) {
global $wpdb;

	if ( ! is_array( $args ) ) {
		if ( wppa_is_int( $args ) ) {
			$args = array( 'id' => $args, 'modified' => time() );
		}
		else {
			return false;
		}
	}
	if ( ! isset( $args['id'] ) ) return false;
	$thumb = wppa_cache_thumb( $args['id'] );
	if ( ! $thumb ) return false;
	$id = $args['id'];

	// If Timestamp update, make sure modified is updated to now
	if ( isset( $args['timestamp'] ) ) {
		$args['modified'] = time();
	}

	// If name update, also update sname
	if ( isset( $args['name'] ) ) {
		$args['sname'] = wppa_sanitize_album_photo_name( wppa_strip_tags( $args['name'] ) );
	}

	// Init switches
	$need_re_index = false;
	$set_modified  = false;

	foreach ( array_keys( $args ) as $itemname ) {
		$itemvalue = $args[$itemname];
		$doit = false;

		// Sanitize input
		switch( $itemname ) {
			case 'id':
				break;
			case 'name':
				$itemvalue = wppa_strip_tags( $itemvalue, 'all' );
				$doit = true;
				$need_re_index = true;
				$set_modified = true;
				break;
			case 'description':
				$itemvalue = balanceTags( $itemvalue, true );
				$itemvalue = wppa_strip_tags( $itemvalue, 'script&style' );
				$doit = true;
				$need_re_index = true;
				$set_modified = true;
				break;
			case 'timestamp':
			case 'modified':
				if ( ! $itemvalue ) {
					$itemvalue = time();
				}
				$doit = true;
				$set_modified = true;
				break;
			case 'tags':
				$itemvalue = wppa_sanitize_tags( $itemvalue );
				$doit = true;
				$need_re_index = true;
				break;
			case 'ext':
				$itemvalue = strtolower( $itemvalue );
				$doit = true;
				break;
			case 'filename':
				$itemvalue = wppa_sanitize_file_name( $itemvalue );
				$doit = true;
				$need_re_index = true;
				break;
			case 'custom':
				$doit = true;
				$need_re_index = true;
				break;
			case 'owner':
				$doit = true;
				$need_re_index = true;
				break;
			case 'album':
				$itemvalue = strval( intval( $itemvalue ) );
				$doit = true;
				$need_re_index = true;
				break;
			case 'thumblock':
				$itemvalue = $itemvalue ? '1' : '0';
				$doit = true;
				break;
			case 'duration':
				$itemvalue = sprintf( '%4.2f', $itemvalue );
				$doit = true;
				break;

			default:

				// Just doit
				if ( in_array( $itemname, array( 'mean_rating',
												 'linkurl',
												 'linktitle',
												 'linktarget',
												 'alt',
												 'location',
												 'indexdtm',
												 'crypt',
												 'stereo',
												 'panorama',
												 'sname',
												 'magickstack',
												 'scheduledtm',
												 'scheduledel',
												 'exifdtm',
												 'status',
												) ) ) {
					$doit = true;
				}

				// Assure integer
				elseif ( in_array( $itemname, array( 'dlcount',
													 'p_order',
													 'thumbx',
													 'thumby',
													 'photox',
													 'photoy',
													 'videox',
													 'videoy',
													 'rating_count',
													 'views',
													 'clicks',
													 'page_id',
													 'angle',
													 ) ) ) {

					$itemvalue = strval( intval( $itemvalue ) );
					$doit = true;
				}

				// Undefined
				else {
					wppa_log( 'Err', 'Not implemented in wppa_update_photo(): ' . $itemname );
					return false;
				}
		}

		if ( $doit ) {
			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->wppa_photos
										   SET $itemname = %s
										   WHERE id = %s", $itemvalue, $id ) );
		}

		if ( $set_modified ) {
			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->wppa_photos
										   SET modified = %s
										   WHERE id = %s", time(), $id ) );
			if ( wppa_switch( 'pup_is_aup' ) ) {
				$album = $wpdb->get_var( $wpdb->prepare( "SELECT album
														  FROM $wpdb->wppa_photos
														  WHERE id = %s", $id ) );
				wppa_update_album( ['id' => $album, 'modified' => time()] );
			}
		}

		if ( $need_re_index ) {
			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->wppa_photos
										   SET indexdtm = ''
										   WHERE id = %s", $id ) );
			wppa_schedule_maintenance_proc( 'wppa_remake_index_photos' );
		}

		wppa_cache_photo( 'invalidate', $id );
	}

	// Update index
	if ( $need_re_index ) {
		wppa_schedule_maintenance_proc( 'wppa_remake_index_photos' );
	}

	// Clear associated caches
	wppa_clear_cache( array( 'photo' => $id ) );

	return $id;
}