<?php
/**
 * Open Source Social Network
 *
 * @package Open Source Social Network
 * @author    OSSN Core Team https://www.openteknik.com/contact
 * @copyright (C) OpenTeknik LLC
 * @license   OPENTEKNIK  LLC, COMMERCIAL LICENSE, COMMERCIAL LICENSE https://www.openteknik.com/license/commercial-license-v1
 * @link      https://www.opensource-socialnetwork.org/
 */
namespace Ossn\Component\WorldsPage;
class Photo extends \OssnFile {
		/**
		 * Add a photo for page
		 * 
		 * @param integer $guid A page guid
		 * 
		 * @return boolean
		 */			
		public function addPhoto($guid) {
				if(empty($guid)) {
						return false;
				}
				$this->owner_guid = $guid;
				$this->type       = 'object';
				$this->subtype    = 'page:photo';
				$this->setFile('photo');
				$this->setPath('worlds/photo/');
				$this->setExtension(array(
						'jpg',
						'png',
						'jpeg',
						'gif'
				));
				if($file_guid = $this->addFile()) {
						$page = ossn_get_object($guid);
						if($page) {
								$page->data->photo_entity_guid = $file_guid;
								$page->save();
						}
						$this->deleteOld($guid, $file_guid);
						return true;
				}
				return false;
		}
		/**
		 * Delete old photos for page
		 * 
		 * @param integer $guid A page guid
		 * 
		 * @return boolean
		 */				
		public function deleteOld($guid, $pic_guid) {
				if(empty($guid)) {
						return false;
				}
				$pics = $this->searchEntities(array(
						'type' => 'object',
						'subtype' => 'file:page:photo',
						'owner_guid' => $guid
				));
				if($pics) {
						foreach($pics as $pic) {
								if($pic->guid == $pic_guid) {
										continue;
								}
								$dir = ossn_get_userdata("object/{$guid}/");
								if($pic->deleteEntity($pic->guid)) {
										unlink($dir . $pic->value);
								}
						}
				}
		}
}