<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------------------------------
// Application Includes.
// ------------------------------------------------------------------------------------------------

require_once(APP_ROOT . "classes/classified/ClassifiedImageManager.class.php");

/**
 * C_MemberCommercialView (tackle_trader/class)
 *
 * @author Shaun Richards
 * @package neocentra
 * @link www.neocentra.co.uk
 */

class C_MemberCommercialView {

	private $viewPath = "../web_root/assets/tackle_trader/public/components/C_MemberCommercialView/CV_MemberCommercialView.php";
	private $cssPath  = "assets/tackle_trader/public/components/C_MemberCommercialView/C_MemberCommercialView.css";
	//private $jsPath   = "assets/framework/components/C_NavigationMenu/C_NavigationMenu.js";
	
	private $memberId = 0;
	private $pageId = 0;
	
	private $mMember;
	private $mCommercialMemberPage;

	private $tagServiceTypes;
	private $tagCategories;
	private $tagItemTypes;
	private $tagManufacturers;
	
	private $locations;

	private $pages;
	
	private $specifiedPageFound = FALSE;
	private $defaultPageUsed = FALSE;

	private $errors = array();
	
	//-----------------------------------------------------------------------------------------------
	// Ctx
	//-----------------------------------------------------------------------------------------------
	
	function C_MemberCommercialView($memberId,$pageId) {

		$this->memberId = $memberId;
		$this->pageId = $pageId;
		
		$this->initialiseModels();
		$this->initialiseCollections();
		
	}

	//-----------------------------------------------------------------------------------------------
	// Public Methods - Page Enquiry
	//-----------------------------------------------------------------------------------------------
	
	public function hasPage() {

		return (!empty($this->mCommercialMemberPage) && $this->mCommercialMemberPage->get_id() != 0);
	
	}
	
	public function getPageTitle() {
	
		if ($this->hasPage())
	
			return $this->mCommercialMemberPage->get_title();
		
		else
		
			return "No Page Loaded";
	
	}
	
	public function getPageMetaDescription() {
	
		if ($this->hasPage())
		
			return $this->mCommercialMemberPage->get_meta_description();
	
		else
		
			return "";
	
	}
	
	public function getPageMetaKeywords() {
	
		if ($this->hasPage())
		
			return $this->mCommercialMemberPage->get_meta_keywords();
	
		else
		
			return "";
	
	}

	//-----------------------------------------------------------------------------------------------
	// Public Methods - Status Comment
	//-----------------------------------------------------------------------------------------------
	
	public function getStatusCommentContent() {

		$out  = "memberId = " . $this->memberId . "\n";
		$out .= "pageId = " . $this->pageId . "\n";

		$out .= "mMember:\n";
		
		if (empty($this->mMember)) {
		
			$out .= "\tempty\n";
		
		} else {
		
			$out .= "\tid = " . $this->mMember->get_id() . "\n";
		
		}
		
		$out .= "mCommercialMemberPage:\n";
		
		if (empty($this->mMember)) {
		
			$out .= "\tempty\n";
		
		} else {
		
			if (!empty($this->mCommercialMemberPage))
		
				$out .= "\tid = " . $this->mCommercialMemberPage->get_id() . "\n";
		
		}

		$out .= "specifiedPageFound = " . var_export($this->specifiedPageFound,TRUE) . "\n";
		$out .= "defaultPageUsed = " . var_export($this->defaultPageUsed,TRUE) . "\n";
	
		return $out;
		
	}
	
	//-----------------------------------------------------------------------------------------------
	// Public Methods - Error Enquiry
	//-----------------------------------------------------------------------------------------------
	
	public function hasErrors() {
	
		return (count($this->errors) > 0 ? TRUE : FALSE);
	
	}
	
	public function getErrors() {
	
		return $this->errors;
	
	}
	
	//-----------------------------------------------------------------------------------------------
	// Public Methods - View
	//-----------------------------------------------------------------------------------------------
	
	public function view() {

		$ci =& get_instance();

		//
		// Get template. Add CSS link.
		//
		
		$template = $ci->getTemplate();
		$template->linkCss($this->cssPath);
		
		//
		// Add data to the view.
		//
		
		$data['template'] = $template;
		$data['memberManager'] = $ci->memberManager;
		$data['component'] = $this;
		$data['mMember'] = $this->mMember;
		$data['mCommercialMemberPage'] = $this->mCommercialMemberPage;

		$data['tagServiceTypes'] = $this->tagServiceTypes;
		$data['tagCategories'] = $this->tagCategories;
		$data['tagItemTypes'] = $this->tagItemTypes;
		$data['tagManufacturers'] = $this->tagManufacturers;
	
		$data['locations'] = $this->locations;
		
		$data['pages'] = $this->pages;
		
		//$cim = ClassifiedImageManager::getInstance();
		//$cim->setClassifiedModel($this->m_classified);
		//$cim->setMode("public");
		//$gallery = $cim->getComponent();
		//$data['gallery'] = $gallery;
		
		//
		// Generate the view for this component.
		//
		
		$view = $ci->load->view($this->viewPath,$data,TRUE);
		
		// Return the view for output witin the parent view.
		
		return $view;
		
	}

	//-----------------------------------------------------------------------------------------------
	// Private Methods
	//-----------------------------------------------------------------------------------------------
	
	private function initialiseModels() {
	
		if (!empty($this->memberId)) {
		
			// Attempt to retrieve the member.

			$this->mMember = ModelFactory::load("classes/member/m_member");
			$memberFound = $this->mMember->get($this->memberId);
			
			if ($memberFound) {

				$pageFound = FALSE;
				
				$this->mCommercialMemberPage = ModelFactory::load("classes/commercial/m_commercial_member_page");

				if (!empty($this->pageId)) {
				
					// Attempt to retrieve the specified page.

					$pageFound = $this->mCommercialMemberPage->get($this->pageId);
					
					if ($pageFound && $this->mCommercialMemberPage->is_status_published()) {
					
						$this->specifiedPageFound = TRUE;
						
					} else {
					
						$this->errors[] = "Specified page id = " . $this->pageId . " not found.";
						
						$this->mCommercialMemberPage = NULL;
					
					}
				
				} else {
				
					$count = $this->mCommercialMemberPage->getDefaultForMemberId($this->memberId);
				
					if ($count > 1) {
					
						$this->errors[] = "$count default pages found for member id = " . $this->memberId . ".";
					
					} else if ($count == 0) {
					
						$totalPageCount = $this->mCommercialMemberPage->getAllForMemberCount($this->memberId);
						
						if ($totalPageCount > 0) {
						
							$this->errors[] = "Member id = " . $this->memberId . " has pages but no default/main.";
							$this->errors[] = "Shaun Richards";
						
						}
					
					} else {
					
						if ($this->mCommercialMemberPage->is_status_published()) {
					
							$this->defaultPageUsed = TRUE;
							
						} else {
						
							$this->mCommercialMemberPage = NULL;
						
						}
						
					}
				
				}
				
			} else {
			
				$this->errors[] = "A member with id = " . $this->memberId . " cannot be found.";
			
			}
			
		} else {
		
			$this->errors[] = "The member id is empty.";
		
		}
	
	}
	
	private function initialiseCollections() {

		$this->initialiseTags();
	
		$this->initialiseAddresses();
		
		$this->initialisePages();
	
	}
	
	private function initialiseTags() {
	
		$mCommercialEntryTag = ModelFactory::load("classes/commercial/m_commercial_entry_tag");
		
		$this->tagServiceTypes = $mCommercialEntryTag->getAllServiceTypesForMember($this->memberId);
	
		$this->tagCategories = $mCommercialEntryTag->getAllCategoriesForMember($this->memberId);
		
		$this->tagItemTypes = $mCommercialEntryTag->getAllItemTypesForMember($this->memberId);
		
		$this->tagManufacturers = $mCommercialEntryTag->getAllManufacturersForMember($this->memberId);
		
	}

	private function initialiseAddresses() {
	
		$mMemberCommercialLocation = ModelFactory::load("classes/member/m_member_commercial_location");
		
		$this->locations = $mMemberCommercialLocation->getAllForMember($this->memberId);
	
	}
	
	private function initialisePages() {
	
		$mCommercialMemberPage = ModelFactory::load("classes/commercial/m_commercial_member_page");
		
		$this->pages = $mCommercialMemberPage->getAllForMember($this->memberId,TRUE);
	
	}
	
}
?>