<?php

/**
 * @package Unlimited Elements
 * @author UniteCMS http://unitecms.net
 * @copyright Copyright (c) 2016 UniteCMS
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/

//no direct accees
defined ('UNLIMITED_ELEMENTS_INC') or die ('restricted aceess');

class UniteCreatorAcfIntegrate{
	
	const TYPE_SIMPLE_ARRAY = "simple_array";
	const TYPE_POST = "post";
	const TYPE_POSTS_LIST = "posts_list";
	const TYPE_URL = "url";
	const TYPE_USER = "user";
	const TYPE_REPEATER = "repeater";
	const TYPE_GALLERY = "gallery";
	const PREFIX = "cf_";
	
	const SHOW_DEBUG_FIELDS = false;
	const DEBUG_UNKNOWN_TYPE = false;
	
	
		/**
		 * return if acf plugin activated
		 */
		public static function isAcfActive(){
			
			if(class_exists('ACF'))
				return(true);
			
			return(false);
		}
	
		/**
		 * get image title
		 */
		private function getImageFieldTitle($field){
			
			$title = UniteFunctionsUC::getVal($field, "title");
			$filename = UniteFunctionsUC::getVal($field, "filename");
			
			if(empty($title))
				$title = $filename;
				
			
			$info = pathinfo($title);
			$name = UniteFunctionsUC::getVal($info, "filename");
			
			if(!empty($name))
				$title = $name;
			
			return($title);
		}
		
		
		/**
		 * get image field data
		 */
		private function getImageFieldData($field, $key=null){
			
			$title = $this->getImageFieldTitle($field);
			
			$caption = UniteFunctionsUC::getVal($field, "caption");
			if(!empty($caption))
				$title = $caption;
			
			$description = UniteFunctionsUC::getVal($field, "description");
			$alt = UniteFunctionsUC::getVal($field, "alt");
			
			if(empty($alt))
				$alt = $title;
			
			$urlImage = UniteFunctionsUC::getVal($field, "url");
			$arrSizes = UniteFunctionsUC::getVal($field, "sizes");
			
			$arrValues = array();
			
			$keyprefix = "";
			if(!empty($key))
				$keyprefix = $key."_";				
			
			if(!empty($key))
				$arrValues[$key] = $urlImage;
			else
				$arrValues["image"] = $urlImage;
			
				
			$thumbMedium = UniteFunctionsUC::getVal($arrSizes, "medium");
			$arrValues[$keyprefix."thumb"] = $thumbMedium;
			
			foreach($arrSizes as $size => $value){
				
				if($size == "medium")
					continue;
				
				if(is_numeric($value))
					continue;
				
				$thumbName = $keyprefix."thumb_".$size;
				$thumbName = str_replace("-", "_", $thumbName);
				
				$arrValues[$thumbName] = $value;
			}
			
			$arrValues[$keyprefix."title"] = $title;
			$arrValues[$keyprefix."description"] = $description;
			$arrValues[$keyprefix."alt"] = $alt;
			
			
			return($arrValues);
		}
		
		
		/**
		 * get post data
		 */
		private function getPostData($objPost, $key, $addID = false){
			
			$arrPost = (array)$objPost;
			
			$postID = UniteFunctionsUC::getVal($arrPost, "ID");
			
			$arrValues = array();
			
			if($addID == true)
				$arrValues[$key."_id"] = $postID;
			else
				$arrValues[$key] = $postID;
			
			$arrValues[$key."_title"] = UniteFunctionsUC::getVal($arrPost, "post_title");
			$arrValues[$key."_alias"] = UniteFunctionsUC::getVal($arrPost, "post_name");
			$arrValues[$key."_content"] = UniteFunctionsUC::getVal($arrPost, "post_content");
			$arrValues[$key."_link"] = UniteFunctionsWPUC::getPermalink($objPost);
						
			return($arrValues);
		}
		
		
		/**
		 * get file field data
		 */
		private function getFileFieldData($data, $key){
			
			$title = UniteFunctionsUC::getVal($data, "title");
			$filename = UniteFunctionsUC::getVal($data, "filename");
			
			if(empty($title))
				$title = $filename;
			
			$url = UniteFunctionsUC::getVal($data, "url");
			$filesize = UniteFunctionsUC::getVal($data, "filesize");
			$filesize = size_format($filesize, 2);
			$filesize = str_replace(" ", "", $filesize);
			
			$arrValues = array();
			$arrValues[$key] = $url;
			$arrValues[$key."_title"] = $title;
			$arrValues[$key."_filename"] = $filename;
			$arrValues[$key."_size"] = $filesize;
						
			
			return($arrValues);
		}

		/**
		 * get posts list data
		 */
		private function getPostsListData($data){
			
			if(empty($data))
				return($data);
			
			$arrOutput = array();
			foreach($data as $post){
				
				$arrPost = $this->getPostData($post, "post", true);
				
				$arrOutput[] = $arrPost;
			}
			
			return($arrOutput);
		}
		
		
		/**
		 * get data type of acf values
		 */
		private function getDataType($data, $key){
			
			$type = null;
			
			//image and application types
			
			if(is_array($data)){
				$type = UniteFunctionsUC::getVal($data, "type");
				
				if(!empty($type))
					return($type);
				
				if(isset($data[0])){
					
					$item = $data[0];
					
					$itemType = gettype($item);
					
					
					if($item instanceof WP_Post)
						return(self::TYPE_POSTS_LIST);
					
					if($itemType == "array"){
										
						$itemType = UniteFunctionsUC::getVal($item, "type");
						
						if($itemType == "image" && isset($item["sizes"]))
							return(self::TYPE_GALLERY);
						
						return(self::TYPE_REPEATER);
					}
						
					if($itemType != "object")
						return(self::TYPE_SIMPLE_ARRAY);
					
				};
				

				//check for url
				if(isset($data["url"]))
					return(self::TYPE_URL);
				
				if(isset($data["user_firstname"]) && isset($data["user_lastname"]))
					return(self::TYPE_USER);
				
				if(self::DEBUG_UNKNOWN_TYPE == true){
					dmp("unknown type");
					dmp($data);
					exit();
				}
				
			}
			

			//post
			if(is_object($data)){
				
				if($data instanceof WP_Post)
					return(self::TYPE_POST);
			}
			
			
			return(null);
		}
		
		/**
		 * get user field data
		 */
		private function getUserData($arrUser, $key){
			
			$arrValues = array();
			$arrValues[$key."_firstname"] = UniteFunctionsUC::getVal($arrUser, "user_firstname");
			$arrValues[$key."_lastname"] = UniteFunctionsUC::getVal($arrUser, "user_lastname");
			$arrValues[$key."_displayname"] = UniteFunctionsUC::getVal($arrUser, "display_name");
			$arrValues[$key."_email"] = UniteFunctionsUC::getVal($arrUser, "user_email");
			$arrValues[$key."_avatar"] = UniteFunctionsUC::getVal($arrUser, "user_avatar");
						
			return($arrValues);
		}
		
		/**
		 * get repeater field
		 */
		private function getRepeaterField($arrData){
			
			if(empty($arrData))
				return($arrData);
			
			foreach($arrData as $index => $arrItem){
				
				//prepare new item
				$arrItemNew = array();
				$arrItemNew["item_index"] = $index+1;
				
				foreach($arrItem as $key => $value){
					$arrItemNew = self::addAcfValues($arrItemNew, $key, $value);
				}
									
				$arrData[$index] = $arrItemNew;
			}
			
			return($arrData);
						
		}
		
		
		/**
		 * get gallery field
		 */
		private function getGalleryField($data){
			
			if(empty($data))
				return(array());
							
			$arrItems = array();
			foreach($data as $index => $item){
				
				$itemData = $this->getImageFieldData($item);
				$itemData["item_index"] = $index+1;
				
				$arrItems[] = $itemData;
			}
			
						
			return($arrItems);
		}
		
		
		/**
		 * get acf type
		 */
		private function addAcfValues($arrValues, $key, $data){
			
			if(empty($data)){
				$arrValues[$key] = $data;
				return($arrValues);
			}
			
			if(is_string($data) == true){
				$arrValues[$key] = $data;
				
				return($arrValues);
			}
			
			$type = $this->getDataType($data, $key);
			
			switch($type){
				case "image":
					
					$imageData = $this->getImageFieldData($data, $key);
					
					$arrValues = array_merge($arrValues, $imageData);
					
					return($arrValues);
				break;
				case "application":
					
					$fileData = $this->getFileFieldData($data, $key);
					$arrValues = array_merge($arrValues, $fileData);
					
					return($arrValues);
				break;
				case self::TYPE_POST:
					
					$postData = $this->getPostData($data, $key);
					$arrValues = array_merge($arrValues, $postData);
					
					return($arrValues);
				break;
				case self::TYPE_SIMPLE_ARRAY:
					$strData = implode(", ", $data);
					
					$arrValues[$key] = $strData;
					$arrValues[$key."_array"] = $data;						
					
					return($arrValues);
				break;
				case self::TYPE_URL:
					$arrValues[$key] = $data["url"];
					
					return($arrValues);
				break;
				case self::TYPE_POSTS_LIST:
					
					$arrValues[$key] = $this->getPostsListData($data);
					
				break;
				case self::TYPE_USER:
					$userData = $this->getUserData($data, $key);
					$arrValues = array_merge($arrValues, $userData);
					
				break;
				case self::TYPE_REPEATER:
					
					$arrValues[$key] = $this->getRepeaterField($data);
					
				break;
				case self::TYPE_GALLERY:
					
					$arrValues[$key] = $this->getGalleryField($data);
					
				break;
				default:		
					
					$arrValues[$key] = "";
					
					//dmp("assoc");dmp($data); exit();
					
				break;
			}
						
			return($arrValues);			
		}
	
		/**
		 * get fields objects
		 */
		private function getFieldsObjects($postID, $objName, $addPrefix = true){
			
			switch($objName){
				case "post":
					$arrObjects = get_field_objects($postID);
										
				break;
				default:
					UniteFunctionsUC::throwError("get acf fields objects function works only for post right now");
				break;
			}
			
			if($addPrefix == false)
				return($arrObjects);
			
			//add prefixes
			$arrOutput = array();
			foreach($arrObjects as $key => $arrObject){
				
				$key = self::PREFIX.$key;
				$arrOutput[$key] = $arrObject;
			}
			
			return($arrOutput);
		}
		
		
		/**
		 * get fields type assoc array
		 */
		private function getFieldsTypes($postID, $objName, $addPrefix = true){
			
			$arrObjects = $this->getFieldsObjects($postID, $objName, $addPrefix);
			
			$arrTypes = array();
			foreach($arrObjects as $key => $arrObject){
				
				$type = UniteFunctionsUC::getVal($arrObject, "type");
				
				$arrTypes[$key] = $type;
			}
			
			return($arrTypes);
		}
		
		
		/**
		 * modify get fields data, consolidate all clones
		 * clone is array inside array
		 */
		public function modifyFieldsData($arrData){
			
			if(empty($arrData))
				return($arrData);
			
			$arrOutput = array();
			
			foreach($arrData as $key => $item){
				
				//simple value
				if(is_array($item) == false){
					
					$arrOutput[$key] = $item;
					continue;
				}
				
				//numeric array
				if(isset($item[0])){
					$arrOutput[$key] = $item;
					continue;
				}
				
				$firstItem = UniteFunctionsUC::getArrFirstValue($item);
				
				if(is_array($firstItem) == false){
					$arrOutput[$key] = $item;
					continue;
				}
				
				//what's rest is assoc array, wich is clone :)
				
				//add them to output
				foreach($item as $itemKey => $subItem){
					
					$newKey = $key."_".$itemKey;
					$arrOutput[$newKey] = $subItem;
				}
								
			}

			
			return($arrOutput);
		}
		
		/**
		 * get acf post fields
		 */
		public function getAcfFields($postID, $objName = "post", $addPrefix = true){
			
			switch($objName){
				case "post":
					$arrData = get_fields($postID);
					
					$arrData = $this->modifyFieldsData($arrData);
					
				break;
				default:
					UniteFunctionsUC::throwError("get acf fields function works only for post right now");
				break;
			}
						
			if(self::SHOW_DEBUG_FIELDS == true){
				dmp($arrData);
				exit();
			}
			
			if(empty($arrData))
				$arrData = array();
						
			$arrDataOutput = array();
			foreach($arrData as $key => $value){
				
				if($addPrefix == true)
					$key = self::PREFIX.$key;
				
				$arrDataOutput = $this->addAcfValues($arrDataOutput, $key, $value);
			}
			
						
			return($arrDataOutput);
		}
		
		
		
		/**
		 * get keys of acf fields
		 */
		public function getAcfFieldsKeys($postID, $objName = "post", $addPrefix = true){
			
			$arrFields = $this->getAcfFields($postID, $objName, $addPrefix);
			$arrTypes = $this->getFieldsTypes($postID, $objName, $addPrefix);
				
			$arrOutput = array();
			foreach($arrFields as $key=>$value){
								
				$fieldType = UniteFunctionsUC::getVal($arrTypes, $key);
				
				if(($fieldType == "repeater") && empty($value)){
					$arrOutput[$key] = "empty_repeater";
					continue;
				}
				
				$type = "simple";
				if(is_array($value)){
					
					$item = UniteFunctionsUC::getArrFirstValue($value);
					
					if(is_string($item))
						$type = "array";	//check if simple array or complext array (repeater)
					else{
						$type = $item;
					}
				}
				
				$arrOutput[$key] = $type;
			}
			
			
			return($arrOutput);
		}
	
	
}