<?php
 
class Article {
       private $fcmsanzeigenimage;
	private $originalId ="";
	private $title;
	private $artid;
	private $categoryId;
	private $catchline = "";
	private $subheading = "";
	private $description = "";
	private $keywords = "";
	private $dossier = "";
	private $author= "";
	private $authorname="";
	private $superscript= "";
	private $subscript= "";
	private $headline =""; 
	private $publicationDate; 
	private $places = array();
	private $pics = array();
	private $diashowPics = array();
	private $diashows;

       private $images = array();
	private $logger;
	private $text = "";
	private $leadtext = "";

	private $urgency = 1;
	private $status = "WAIT";

	private $publicationTimings = array(); // mit 0 Objekten von Timings
	private $creationDate = "";
	private $editDate = "";

	private $fcmshelper;

	private $prevurl = "";

	/**
	 * Konstruktor mit den 4 Pflichtfeldern initialisiert den Logger und die
	 * Helperklasse, welche für die REST-Aufrufe zuständig ist
	 * 
	 * @param title
	 * @param originalId
	 * @param categoryId
	 */
	public function __construct() {
		require_once('MyLogPHP.class.php');
		require_once('Fcmshelper.class.php');
		require_once("config.inc.php");
		$logger = new MyLogPHP('/var/www/vhosts/infranken.de/fbia.infranken.de/FBinstantarticles/debug.log.csv');
		$this->logger=$logger;	
		$this->logger->info("-------------------------------------");
		$this->logger->info("konstruktor-Call Fcmsanzeige");
     		$this->fcmshelper = new Fcmshelper();


	}


	/**
	 * macht aus Membervariablen ein JSONObject
	 * 
	 * @return JSONObject
	 */
	public function toJSON() {
		$jdata =  array(); 		
		$jpublicationTimings =  array();
		$jpublicationTimingsArray =  array();
    		$jobj =  array();
		try {
    
		      $jdata["originalId"]=$this->originalId;
		      $jdata["title"]=$this->title;
		      $jdata["catchline"]=$this->catchline;
		      $jdata["subheading"]=$this->subheading;
		      $jdata["description"]=$this->description;
		      $jdata["keywords"]=$this->keywords;
		      $jdata["dossier"]=$this->dossier;
		     // $jdata["places"]=$this->getPlaces();
		     // $jdata["publicationDate"]=$this->getPublicationDate();
		      $jdata["text"]=$this->text;
		      $jdata["urgency"]=$this->urgency;
		      $jdata["status"]=$this->status;
		      $jdata["creationDate"]=$this->creationDate;
		      $jdata["editDate"]=$this->editDate;
		      $jdata["leadtext"]=$this->leadtext;
		      $jdata["superscript"]=$this->superscript;   
		      $jdata["subscript"]=$this->subscript;  
		      $jdata["headline"]=$this->headline;  
     			// $jpublicationTimings["from"]=$this->getPublicationDate();
			//$jpublicationTimings["to"]=$this->getEndDate();
		      $jpublicationTimingsArray=$jpublicationTimings;
		      $jobj["data"]= $jdata;
			//
		} catch (Exception $e) {

			$logger->error("JSON-Object konnte nicht angelegt werden");
		}

		return json_encode($jobj);

	}


	public function toJSON2() {
		$jdata =  array(); 		
		$jpublicationTimings =  array();
		$jpublicationTimingsArray =  array();
    
		$jobj =  array();
		
		

		try {

    
		      $jdata["originalId"]=$this->originalId;
		      $jdata["title"]=$this->title;
		      $jdata["catchline"]=$this->catchline;
		      $jdata["author"]=$this->author;
		      $jdata["subheading"]=$this->subheading;
		      $jdata["description"]=$this->description;
		      $jdata["keywords"]=$this->keywords;
		      $jdata["dossier"]=$this->dossier;
		     // $jdata["places"]=$this->getPlaces();
		     // $jdata["publicationDate"]=$this->getPublicationDate();
		      $jdata["text"]=$this->text;
		      $jdata["urgency"]=$this->urgency;
		      $jdata["status"]=$this->status;
		      $jdata["creationDate"]=$this->creationDate;
		      $jdata["editDate"]=$this->editDate;
		      $jdata["leadtext"]=$this->leadtext;
		      $jdata["superscript"]=$this->superscript;   
		      $jdata["subscript"]=$this->subscript;      
      
     		      $jdata["headline"]=$this->headline; 
      
	              // $jpublicationTimings["from"]=$this->getPublicationDate();
			//$jpublicationTimings["to"]=$this->getEndDate();
      
		      $jpublicationTimingsArray=$jpublicationTimings;
			
		      $jobj["data"]= $jdata;
			
			

			//
		} catch (Exception $e) {

			$logger->error("JSON-Object konnte nicht angelegt werden");
		}

		return $jobj;

	}


	
   



   
	public function send()  {
		$this->logger->info("send");
		$this->fcmshelper->setArticle($this);
		$this->fcmshelper->send($this->categoryId);
		$i =0;

		if (count($this->images)> 0) {
			foreach($this->images as $image){
				$this->fcmshelper->setImage($image);
				$this->fcmshelper->sendImage();
				$this->fcmshelper->addPicture();
				if($i==0){
					$this->fcmshelper->addThumbnail();
					$i++;
				}

			}
		}




	
		$this->fcmshelper->saveArticle();
	 	$this->prevurl=$this->fcmshelper->getPreviewUrl();

	}

	
	public function finishArtikel(){
		$this->fcmshelper->saveArticle();
}
	public function getRezeptefromData(){
		return $this->fcmshelper->getRezeptefromData();
	}

	public function setArtId($artid){
		$this->fcmshelper->setArtId($artid);
		$this->artid = $artid;                
		
	}

	public function getArticle(){


		$result= $this->fcmshelper->getData();

		$this->pics = $this->fcmshelper->getPictures();


			$this->title = $result->data->data->title;
			$this->catchline =$result->data->data->catchline;
			$this->description =$result->data->data->description;
			$this->places =$result->data->data->places;
			$this->publicationDate =$result->data->data->publicationDate;
			$this->creationDate =$result->data->data->creationDate;
			$this->editDate =$result->data->data->editDate;
			$this->location =$result->data->data->location;
			$this->text =$result->data->data->text;
			$this->author =(count($result->data->data->authorIds)>0)?$result->data->data->authorIds[0]:"";
			$this->informationthumbnailurl =$result->data->information->thumbnail->url;
			$this->informationthumbnailauthor =$result->data->information->thumbnail->author; 

			$this->diashows=$result->data->information->diashows;


	}

	
	public function getAuthor() {
			$a= $this->fcmshelper->getAuthor($this->author);
			$authorname = $a->data->data->firstName." ".$a->data->data->lastName;
			return $authorname;

	}


	public function getOriginalId() {
		return $this->originalId;
	}

	public function fetchPicture($imgid){
		$this->fcmshelper->fetchPicture($imgid);
	}

	public function getTitle() {
		return $this->title;
	}

	public function getPics() {
		return $this->pics;
	}

	 
	public function getCatchline() {
		return $this->catchline;
	}

	 
	public function getSubheading() {
		return $this->subheading;
	}

	 
	public function getSubscript() {
		return $this->subscript;
	}


	public function getSuperscript() {
		return $this->superscript;
	}

	 
	public function getLeadtext() {
		return $this->leadtext;
	}

	 
 
	public function getDescription() {
		return $this->description;
	}


	 
	public function getKeywords() {
		return $this->keywords;
	}

	public function getDiashows() {

$bool = $this->fcmshelper->getDiashows();

if($bool[0]!=0){

$this->fcmshelper->getDiashowPics();
return $this->fcmshelper->fetchPictureList();
}
else{
return array();
}
                   



/*if($this->diashows>0){
		
		$this->diashowPics=$this->fcmshelper->getDiashowPics();
	}

else return "";
*/
		
	}


	 
 
	public function getDossier() {
		return $this->dossier;
	}

	 
	

	public function getHeadline() {
		return $this->headline;
	}

	 
	
	 
	public function getText() {
		return $this->text;
	}

	 
	

	 
	public function getPublicationDate() {
		return $this->publicationDate;
	}

	 
	public function getEditDate(){
		return $this->editDate;
	}

	 
	public function getStatus() {
		return $this->status;
	}

	 
	 
	public function getPublicationTimings() {
		return $this->publicationTimings;
	}

	 

	 
	public function getCreationDate() {
		return $this->creationDate;
	}
 
	 
	public function getDiashowPics(){
		return $this->diashowPics;
	}
 	 
 

  
	 


	public function getArtId() {
		return $this->artid;
	}

	


}

