<commit_before><?php
/**
 * Gallery Photo
 *
 *
 * @category Model
 * @package  Croogo
 * @version  1.3
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.edineicipriani.com.br
 */
class Photo extends GalleryAppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
	public $name = 'Photo';

	public $dir = '';

	public $albumDir = '';

	public $actsAs = array(
		'Params',
		);

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	public $belongsTo = array(
		'Album' => array(
			'className' => 'Gallery.Album',
			'foreignKey' => 'album_id'
		)
	);

	public $findMethods = array(
		'by_album' => true,
		);

	function __construct($id = false, $table = null, $ds = null){
		parent::__construct($id = false, $table = null, $ds = null);
		$this->setTargetDirectory();
	}

	function getTargetDirectory() {
		return $this->dir;
	}

	function setTargetDirectory($dir = 'photos', $perm = 0775) {
		if ($dir == 'photos') {
			$this->albumDir = 'img' . DS . $dir . DS;
		} else {
			$this->albumDir = 'galleries' . DS . $dir . DS;
		}
		$this->dir = WWW_ROOT . $this->albumDir;
		$this->sourceDir = WWW_ROOT . $this->albumDir . 'source' . DS;
		if(!is_dir($this->dir)) {
			mkdir($this->dir, $perm, true);
		}
		if(!is_dir($this->sourceDir)) {
			mkdir($this->sourceDir, $perm, true);
		}
	}

	function beforeDelete(){
		$photo = $this->findById($this->id);
		unlink(WWW_ROOT . $photo['Photo']['small']);
		unlink(WWW_ROOT . $photo['Photo']['large']);
		return true;
	}

	function beforeSave($options = array()){
		if ($this->exists()) {
			return true;
		}
		$this->getEventManager()->dispatch(
			new CakeEvent('setupAlbumPath', $this)
			);
		$this->data = $this->_upload($this->data);
		return true;
	}

	public function _findBy_album($state, $query, $results = array()) {
		if ($state == 'before') {
			$slug = isset($query['album']) ? $query['album'] : false;
			$query = Set::merge($query, array(
				'conditions' => array(
					'Photo.status' => true,
					'Album.slug' => $slug,
					'Album.status' => true,
					),
				'order' => 'Photo.weight ASC',
				));
			return $query;
		} else {
			return $results;
		}
	}

	protected function _upload($data){
		$this->Album->recursive = -1;
		$album = $this->Album->read(null, $data['Photo']['album_id']);

		if (!empty($album['Album']['max_width'])) {
			$max_width = $album['Album']['max_width'];
		} else {
			$max_width = Configure::read('Gallery.max_width');
		}

		if (!empty($album['Album']['max_height'])) {
			$max_height = $album['Album']['max_height'];
		} else {
			$max_height = Configure::read('Gallery.max_height');
		}

		if (!empty($album['Album']['max_width_thumbnail'])) {
			$thumb_width = $album['Album']['max_width_thumbnail'];
		} else {
			$thumb_width = Configure::read('Gallery.max_width_thumbnail');
		}

		if (!empty($album['Album']['max_height_thumbnail'])) {
			$thumb_height = $album['Album']['max_height_thumbnail'];
		} else {
			$thumb_height = Configure::read('Gallery.max_height_thumbnail');
		}

		if (!empty($album['Album']['quality'])) {
			$thumb_quality = $album['Album']['quality'];
		} else {
			$thumb_quality = Configure::read('Gallery.quality');
		}

		if (empty($thumb_width) || empty($thumb_height) ||
		    empty($thumb_quality) || empty($max_height) || empty($max_width)) {
			throw new UnexpectedValueException('Missing gallery settings');
		}

		App::import('Vendor', 'Gallery.qqFileUploader', array('file' => 'qqFileUploader.php'));
	   	$uploader = new qqFileUploader();
		$result = $uploader->handleUpload($this->sourceDir);
		if (!empty($result['file'])) {<commit_msg>hack to support 'fake upload'<commit_after><?php
/**
 * Gallery Photo
 *
 *
 * @category Model
 * @package  Croogo
 * @version  1.3
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.edineicipriani.com.br
 */
class Photo extends GalleryAppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
	public $name = 'Photo';


/**
 * Album Absolute path
 */
	public $dir = '';

/**
 * Album path
 */
	public $albumDir = '';

/**
 * Behaviors
 */
	public $actsAs = array(
		'Params',
		);

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	public $belongsTo = array(
		'Album' => array(
			'className' => 'Gallery.Album',
			'foreignKey' => 'album_id'
		)
	);

	public $findMethods = array(
		'by_album' => true,
		);

	public function __construct($id = false, $table = null, $ds = null){
		parent::__construct($id = false, $table = null, $ds = null);
		$this->setTargetDirectory();
	}

	public function getTargetDirectory() {
		return $this->dir;
	}

	public function setTargetDirectory($dir = 'photos', $perm = 0775) {
		if ($dir == 'photos') {
			$this->albumDir = 'img' . DS . $dir . DS;
		} else {
			$this->albumDir = 'galleries' . DS . $dir . DS;
		}
		$this->dir = WWW_ROOT . $this->albumDir;
		$this->sourceDir = WWW_ROOT . $this->albumDir . 'source' . DS;
		if(!is_dir($this->dir)) {
			mkdir($this->dir, $perm, true);
		}
		if(!is_dir($this->sourceDir)) {
			mkdir($this->sourceDir, $perm, true);
		}
	}

	public function beforeDelete(){
		$photo = $this->findById($this->id);
		unlink(WWW_ROOT . $photo['Photo']['small']);
		unlink(WWW_ROOT . $photo['Photo']['large']);
		return true;
	}

	public function beforeSave($options = array()){
		if ($this->exists()) {
			return true;
		}
		$this->getEventManager()->dispatch(
			new CakeEvent('setupAlbumPath', $this)
			);
		$this->data = $this->_upload($this->data);
		return true;
	}

	public function _findBy_album($state, $query, $results = array()) {
		if ($state == 'before') {
			$slug = isset($query['album']) ? $query['album'] : false;
			$query = Set::merge($query, array(
				'conditions' => array(
					'Photo.status' => true,
					'Album.slug' => $slug,
					'Album.status' => true,
					),
				'order' => 'Photo.weight ASC',
				));
			return $query;
		} else {
			return $results;
		}
	}

/**
 * File uploading handling
 *
 * When $_FILES['qqfile'] is present, we assume that we are processing browser
 * uploads.  If not, try to use $data['Photo']['original'] as the source, then
 * perform the necessary image processing.
 * @param $data array Photo
 */
	protected function _upload($data){
		$this->Album->recursive = -1;
		$album = $this->Album->read(null, $data['Photo']['album_id']);

		if (!empty($album['Album']['max_width'])) {
			$max_width = $album['Album']['max_width'];
		} else {
			$max_width = Configure::read('Gallery.max_width');
		}

		if (!empty($album['Album']['max_height'])) {
			$max_height = $album['Album']['max_height'];
		} else {
			$max_height = Configure::read('Gallery.max_height');
		}

		if (!empty($album['Album']['max_width_thumbnail'])) {
			$thumb_width = $album['Album']['max_width_thumbnail'];
		} else {
			$thumb_width = Configure::read('Gallery.max_width_thumbnail');
		}

		if (!empty($album['Album']['max_height_thumbnail'])) {
			$thumb_height = $album['Album']['max_height_thumbnail'];
		} else {
			$thumb_height = Configure::read('Gallery.max_height_thumbnail');
		}

		if (!empty($album['Album']['quality'])) {
			$thumb_quality = $album['Album']['quality'];
		} else {
			$thumb_quality = Configure::read('Gallery.quality');
		}

		if (empty($thumb_width) || empty($thumb_height) ||
		    empty($thumb_quality) || empty($max_height) || empty($max_width)) {
			throw new UnexpectedValueException('Missing gallery settings');
		}

		if (isset($_FILES['qqfile']) || isset($_GET['qqfile'])) {
			// browser uploads
			App::import('Vendor', 'Gallery.qqFileUploader', array('file' => 'qqFileUploader.php'));
			$uploader = new qqFileUploader();
			$result = $uploader->handleUpload($this->sourceDir);
			if (!empty($result['file'])) {
				$sourceFile = $this->sourceDir.$result['file'];
				$copyFile = $this->dir.$result['file'];
				copy($sourceFile, $copyFile);
			}
		} else {
			// file probably already processed
			if (!empty($data['Photo']['large'])) {
				return $data;
			}

			// fake upload
			$result = array(
				'file' => $data['Photo']['original'],
				'success' => true,
				);
			$data['Photo']['original'] = null;
{'fraction_non_alphanumeric': 0.20467765390208434, 'fraction_numerical': 0.0016965584100824044, 'mean_word_length': 3.892116182572614, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3548206', 'n_tokens_mistral': 3210, 'n_tokens_neox': 2826, 'n_words': 551}