<?php

class MemberToShow {
	/** user id */
	private $userID;
	/** user data */
	private $userData;
	/** photo base url */
	private $photoBaseURL;
	/** sort string */
	private $sortString = false;

	/**
	 * Creates a new instance of <code>MemberToShow</code>
	 *
	 * @param userID {numeric} the user id of the user
	 * @param userInfo {object} the user data object of
	 * the user
	 */
	function __construct($userID, $userInfo) {
		if(!is_numeric($userID))
			throw new Exception('given user id is not numeric');


		$this->userID = $userID;
		$this->userData = $userInfo;
		$uploadDir = wp_upload_dir();
		$this->photoBaseURL = $uploadDir['baseurl'] . '/userphoto/';
	}

	/**
	 * Returns a vcard containing the information
	 * as a vcard
	 *
	 * @return the user information as a vcard
	 */
	public function getVCard() {
	}

	/**
	 * Returns the html list entry of the user
	 *
	 * @param onlyShowDetailView shows the detail view (default: false)
	 * @param showEntityAsName if set to true, shows the entity name instead of the name of the person (default: false)
	 * @param asList if set to true, the result will be returned as a list item (li) html element, otherwise,
	 * the list entry is encapsulated in a paragraph (p) element
	 * @return the html list entry of the user
	 */
	public function getListEntry($onlyShowDetailView = false, $showEntityAsName = false, $asList = true) {
		$result = '';

		if($onlyShowDetailView == true) {
			$result = $this->getDetailedView(true);
		} else {
			$result = $asList ? '<li class="member">' : '<div class="member">';
			$linkName = '<span class="institutionusername">' . $this->userData->display_name . '</span>';
/*			if(get_cimyFieldValue($this->userID, 'INSTITUTION') != '') {
				$linkName = '<span class="institutionlabel">' . get_cimyFieldValue($this->userID, 'INSTITUTION') . '</span>' . ' &middot; ' . $linkName;
			}*/

			$result .= '<a class="memberhref" href="javascript:toggleMemberDiv(\'memberpopup' . $this->userID  . '\');" title="' . __('detail view')  . '">' . $linkName  . '</a>';
			$result .= $this->getDetailedView();
			$result .= $asList ? '</li>' : '</div>';
		}

		return $result;	
	}


	/**
	 * Returns the html code for the detailed member view
	 * 
	 * @return the html code for the detailed member view
	 */
	private function getDetailedView($onlyDetails = false) {
		$userPhoto = $this->photoBaseURL;
		$userPhoto .= (isset($this->userData->userphoto_image_file)) ? $this->userData->userphoto_image_file : 'nophoto.jpg';

		$userName = $this->userData->display_name;
		$institution = get_cimyFieldValue($this->userID, 'INSTITUTION');
		if($institution == null) $institution = false;

		$description = $this->userData->description != '' ? nl2br($this->userData->description) : false;

		$address = get_cimyFieldValue($this->userID, 'ADDRESS');
		if($address == null) {
			$address = false;
		} else {
			$address = nl2br($address);
		}

		$phoneNr = get_cimyFieldValue($this->userID, 'PHONE');
		if($phoneNr == null) $phoneNr = false;

		$faxNr = get_cimyFieldValue($this->userID, 'FAX');
		if($faxNr == null) $faxNr = false;

		$email = '<a href="mailto:'. antispambot($this->userData->user_email) .'" title="mail">' . antispambot($this->userData->user_email)  . '</a>';
		$weblink = ($this->userData->user_url != '') ? '<a href="' . $this->userData->user_url  . '" title="' . $this->userData->user_url  . '">' : false;
		$linkText = $this->userData->user_url;
		if($weblink != false) {
			if(strlen($linkText) > 40) $linkText = substr($linkText, 0, 40) . '..';
			$weblink .= $linkText  . '</a>';
		}

		$result = '<table class="memberpopup"' . ($onlyDetails ? ' ' : ' style="display: none;" ') . 'id="memberpopup' . $this->userID . '"><tr><td class="picture"><img src="' . $userPhoto  . '" alt="' . $userName . '"/></td>';

		$result .= '<td class="nameandcontact"><p class="name">' . $userName . '</p>';

		if($institution != false)
			$result .= '<p class="institution">' . $institution .'</p>';

		$result .= '<p class="address">' . $address . '</p>';

		if($description != false)
			$result .= '<p class="userdescription">' . $description . '</p>';

		$result .= '<table class="other">';
		if($phoneNr != false)
			$result .= '<tr><td class="item">'. __('Phone') .': </td><td>' . $phoneNr  . '</td></tr>';
		if($faxNr != false)
			$result .= '<tr><td class="item">'. __('Fax') .': </td><td>' . $faxNr . '</td></tr>';
		if($email != false)
			$result .= '<tr><td class="item">'. __('E-Mail') . ': </td><td>' . $email . '</td></tr>';
		if($weblink != false)
			$result .= '<tr><td class="item">'. __('Web') . ': </td><td>' . $weblink . '</td></tr>';
		$result .= '</table></td></tr></table>';

		return $result;
	}

	/**
	 * Returns the string used for sorting
	 *
	 * @return the string for sorting the member instances
	 */
	public function getSortString() {
		if(!$this->sortString) {
			$this->sortString = get_cimyFieldValue($this->userID, 'COUNTRY');
			if($this->sortString == '') $this->sortString = '_NOT SET';

			$this->sortString .= get_cimyFieldValue($this->userID, 'INSTITUTION');
			if($this->userData->display_name != '') {
				$this->sortString .= $this->userData->display_name;
			} else {
				$this->sortString .= $this->userData->user_login;
			}

		}

		return $this->sortString;
	}


	/**
	 * Returns the value of a given cimy field
	 *
	 * @param fieldname the name of the field to get the value
	 * of. Conversion to uppercase of the name is done in any case.
	 * @return the value of the given field (or an empty string)
	 */
	public function getCimyValue($fieldname) {
		$fieldname = strtoupper($fieldname);
		$result = get_cimyFieldValue($this->userID, $fieldname);
		if($fieldname == 'COUNTRY' && $result == '') $result = '_NOT SET';

		return $result;
	}
}

?>
