plugins / branches / master / models / plugin.php

history
<?php
class Plugin extends AppModel {

	var $name = 'Plugin';
	
	function beforeValidate() {
		if(!isset($this->data['Plugin']['major_version_number']) || !$this->data['Plugin']['major_version_number']) {
			$this->data['Plugin']['major_version_number'] = 0;
		}
		if(!isset($this->data['Plugin']['minor_version_number']) || !$this->data['Plugin']['minor_version_number']) {
			$this->data['Plugin']['minor_version_number'] = 0;
		}
		if(!isset($this->data['Plugin']['build_number']) || !$this->data['Plugin']['build_number']) {
			$this->data['Plugin']['build_number'] = 0;
		}
	}
	
	/**
	 * Saves package file in a directory in the storage directory, named after the last
	 * digit of the ID of the record.
	 *
	 * @return void
	 * @author John David Anderson
	 */
	function saveAsset($id = null, $data) {
		if($id == null) {
			$id = $this->id;
		}
		
		$file = new File($this->getAssetPath($id, true));
		
		if($file->exists()) {
			$file->delete();
		}
		$file->create();
		
		return $file->write($data);
	}

	/**
	 * Returns the path to the asset for this model (whether it exists or not)
	 *
	 * @return void
	 * @author John David Anderson
	 */
	function getAssetPath($id = null, $createParentDirs = false) {
		
		$this->storageDirectory = APP . 'storage';
		
		if($id == null) {
			$id = $this->id;
		}
		
		$id = str_pad($id, 3, '0', STR_PAD_LEFT);
		
		$parentDirectory = $this->storageDirectory . DS . substr($id, -2, 1) . DS . substr($id, -1, 1);
		$assetPath = $parentDirectory . DS . $id . '.package';
		
		if($createParentDirs) {
			$parentFolder = new Folder($parentDirectory);
			$parentFolder->create($parentDirectory);
		}
		
		return $assetPath;
	}
}
?>