<?php
	/**
	 * Owner search wrapper (usually called from remote.php)  
	 * 
	 * @param $query string to search
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
	function owner_search( $query ){
		$sql = "SELECT 
					oid, 
					CONCAT(lastname, ', ', firstname, ' (', email, ')' ) as value 
				FROM owners 
				WHERE 
					firstname LIKE '%{$query}%' 
				OR lastname LIKE '%{$query}%' 
				OR email LIKE '%{$query}%'";
		
		
		$con = new Connection();
		
		$rv = $con->execute($sql);
		
		return $rv;
	}

	// @TODO: implment remote saving
	/**
	 * Save a remote owner (usually called by remote.php)
	 * 
	 * @param 
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
	function owner_save( $json_owner ){
		// $oid = Data::getPost( 'oid', null );
		$o = new Owner( $json_owner->oid );
		
		$o->country_id = $json_owner->country_id;
		$o->firstname = $json_owner->firstname;
		$o->lastname = $json_owner->lastname;
		$o->address= $json_owner->address;
		$o->zip = $json_owner->zip;
		$o->city = $json_owner->city;
		$o->ssn = $json_owner->ssn;
		$o->phone = $json_owner->phone;
		$o->mobile = $json_owner->mobile;
		$o->email = $json_owner->email;
		
		// l( $o );
		
		return $o->save();
		
		// return $o;
		
	}
	
	
	function owner_form_elements( $oid ){
		$bs = new Bootstrap();
		
		$owner = new Owner( $oid );
		
		$input_oid = $bs->addInput('hidden', 'oid', $oid);

		$input_email = $bs->addInput('email', 
									'email', 
									$owner->email, 
									true, 
									_( "Email" ), 
									'myname@example.com'
									);

		$countries = Connection::entity_load( 'countries', array('id', 'name') );
		$select_countries = $bs->addSelect( 'country-id', $countries, $owner->country_id, true,  _( "Country" ) );
			
		$input_firstname = $bs->addInput('text', 
										'firstname', 
										$owner->firstname,
										true,
										_( "First name" ),
										'john'
										);
			
		
		$input_lastname = $bs->addInput('text', 
										'lastname', 
										$owner->lastname,
										true,
										_( "Last name" ),
										'longbow' 
										);
		
		$input_address = $bs->addInput('text', 
									'address', 
									$owner->address,
									true,
									_( "Address" ),
									'Gibbon Street' 
									);
		
		$input_zip = $bs->addInput('text', 
									'zip', 
									$owner->zip,
									true,
									_( "Zip code" ),
									'50063' 
									);

		$input_city = $bs->addInput('text', 
									'city', 
									$owner->city,
									true,
									_( "City" ),
									'Plymouth'
									);

		$input_ssn = $bs->addInput('text', 
									'ssn', 
									$owner->ssn,
									true,
									_( "SSN" ),
									'IT01234567891'
									);

		$input_phone = $bs->addInput('text', 
									'phone', 
									$owner->phone,
									true,
									_( "Phone number" ), 
									'+39 055 1234567'
									);
		$input_mobile = $bs->addInput('text', 
									'mobile', 
									$owner->mobile,
									true,
									_( "Mobile" ), 
									'+39 348 88 22 399'
									);

		

		
		return $input_oid.
				$input_email.
				$select_countries.
				$input_firstname.
				$input_lastname.
				$input_address.
				$input_zip.
				$input_city.
				$input_ssn.
				$input_phone.
				$input_mobile
				;
	}
	
	
	function owner_form( $oid ){
		$bs = new Bootstrap();
		
		$open_form = $bs->openForm( 'owner-form', $_SERVER['PHP_SELF'] . '?op=s&oid=' . $oid, 'POST', '', 'data-parsley-validate' );
		
		$form_elements = owner_form_elements( $oid );
		
				
		$close_form = $bs->closeForm();
		
		return $open_form.
				$form_elements.
				$close_form
				;
		
	}
	

	/**
	 * Owner detail wrapper (usually called from remote.php)  
	 * 
	 * @param $oid int to retrieve
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
	function owner_get( $oid ){
		
		$output = new StdClass;
		
		if( empty ( $oid ) == false && is_numeric( $oid ) ){
			$output = new Owner ( $oid );
		}
		
		return $output;
	}
	
