| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disllow direct access. |
| 7 |
defined('ABSPATH') or die('Access denied'); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTPackageFileModel extends CJTHookableClass { |
| 13 |
|
| 14 |
/** |
| 15 |
* put your comment there... |
| 16 |
* |
| 17 |
* @var mixed |
| 18 |
*/ |
| 19 |
protected $state = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* put your comment there... |
| 23 |
* |
| 24 |
* @param mixed $property |
| 25 |
*/ |
| 26 |
public function getState($property) { |
| 27 |
return isset($this->state[$property]) ? $this->state[$property] : null; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* put your comment there... |
| 32 |
* |
| 33 |
* @param CJTPackageFile |
| 34 |
*/ |
| 35 |
public function install($package) { |
| 36 |
// Initialize. |
| 37 |
$model = null; // Current model for the element under the loop! |
| 38 |
$item = array(); // Object Item array to be inserted in the database. |
| 39 |
$addedObjects = array('template' => array(), 'block' => array()); // All the added objects mapped by object 'TYPE'. |
| 40 |
// Fetch package information with all readme and license tags locatted if |
| 41 |
// bundled with external files! |
| 42 |
$packageInfo = $package->getItem(null, array('readme' => 'readme.txt', 'license' => 'license.txt')); |
| 43 |
// Install only if the package record if not exists. |
| 44 |
$modelPackage = CJTModel::getInstance('package'); |
| 45 |
if ($modelPackage->exists($packageInfo['name'])) { |
| 46 |
// State error! |
| 47 |
$this->state['error']['msg'] = cssJSToolbox::getText('The uploaded package is already installed!'); |
| 48 |
// Terminate! |
| 49 |
return $this; |
| 50 |
} |
| 51 |
// Map definition xml TYPE attribute to CJT Model object to handle addding |
| 52 |
// objecs types. |
| 53 |
$objectModels = array('template' => 'template', 'block' => 'blocks'); |
| 54 |
// Initialize Wordpress file system object. |
| 55 |
WP_Filesystem(); |
| 56 |
$fileSystem =& $GLOBALS['wp_filesystem']; |
| 57 |
// Read all objects defined by the package. |
| 58 |
foreach ($package->document()->objects->object as $object) { |
| 59 |
// Prepare object + getting item to be saved into database. |
| 60 |
$item = $package->getItem($object); |
| 61 |
// Get current object type name. |
| 62 |
$objectType = (string) $object->attributes()->type; |
| 63 |
// Instantiate template model if not previously instantiated. |
| 64 |
if (!is_object($objectModels[$objectType])) { |
| 65 |
$objectModels[$objectType] = CJTModel::getInstance($objectModels[$objectType]); |
| 66 |
} |
| 67 |
// Referencing the model for current object type. |
| 68 |
$model = $objectModels[$objectType]; |
| 69 |
$objectId = 0; // Always reset -- to don't map to package if nothing added! |
| 70 |
// Handle different object types. |
| 71 |
switch ($objectType) { |
| 72 |
case 'template': |
| 73 |
// Insert template only if not exists. |
| 74 |
if (!$model->exists($item['name'])) { |
| 75 |
// Import template(s) helper. |
| 76 |
cssJSToolbox::import('includes:templates:templates.class.php'); |
| 77 |
// Set template revision. |
| 78 |
$model->inputs['item']['revision']['code'] = $item['code']; |
| 79 |
unset($item['code']); |
| 80 |
// Set template main data. |
| 81 |
$model->inputs['item']['template'] = $item; |
| 82 |
/** Get template Type! |
| 83 |
* Get type from external file extension if |
| 84 |
* the template code was linked to file. |
| 85 |
* If the template code were inline |
| 86 |
* then the type must be provided under |
| 87 |
* TYPE element! |
| 88 |
*/ |
| 89 |
// If no type specified get it from the external file extension |
| 90 |
if (!isset($model->inputs['item']['template']['type'])) { |
| 91 |
// @WARNING: Get locatted file! |
| 92 |
$codeFileName = (string) $object->code->attributes()->locatted; |
| 93 |
if ($codeFileName) { |
| 94 |
// Get type from extension. |
| 95 |
$fileComponent = pathinfo($codeFileName); |
| 96 |
$model->inputs['item']['template']['type'] = CJTTemplates::getExtensionType($fileComponent['extension']); |
| 97 |
} |
| 98 |
} |
| 99 |
// Add template. |
| 100 |
$addedTemplate = $model->save(); |
| 101 |
// Expose Object ID to be added to the addedObjects List. |
| 102 |
$objectId = $addedTemplate->templateId; |
| 103 |
// Copy template folders. |
| 104 |
if ($foldersCollection = $object->folders) { |
| 105 |
// Get absolute path to template directory! |
| 106 |
$templateDirectory = ABSPATH . dirname($addedTemplate->file); |
| 107 |
// Process all <folders> tags! |
| 108 |
foreach ($foldersCollection as $folders) { |
| 109 |
// Get <folders> tag common path. |
| 110 |
$foldersPath = (string) $folders->attributes()->path; |
| 111 |
// Process <folder> tag. |
| 112 |
foreach ($folders as $folder) { |
| 113 |
// Folder absolute path. |
| 114 |
if ($detinationName = (string) $folder->attributes()->destination) { |
| 115 |
$folderPath = $detinationName; |
| 116 |
} |
| 117 |
else { |
| 118 |
$folderPath = $folder->attributes()->path; |
| 119 |
} |
| 120 |
$folderAbsPath = $package->getDirectory() . "/{$foldersPath}/{$folderPath}"; |
| 121 |
// Create destination path. |
| 122 |
$folderDestinationPath = "{$templateDirectory}/{$folderPath}"; |
| 123 |
if (!file_exists($folderDestinationPath)) { |
| 124 |
mkdir($folderDestinationPath, 0775); |
| 125 |
} |
| 126 |
// Copy files (FLAT)!!. |
| 127 |
foreach (new DirectoryIterator($folderAbsPath) as $file) { |
| 128 |
if (!$file->isDot() && $file->isFile()) { |
| 129 |
$fileSystem->copy($file->getPathName(), "{$folderDestinationPath}/{$file->getFileName()}"); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
break; |
| 137 |
case 'block'; |
| 138 |
// Set other block internal data. |
| 139 |
$item['created'] = $item['lastModified'] = current_time('mysql'); |
| 140 |
$item['owner'] = get_current_user_id(); |
| 141 |
// Insert block into database. |
| 142 |
$objectId = $model->add($item); |
| 143 |
$model->save(); |
| 144 |
// Initialize for linking templates. |
| 145 |
$modelTemplate = $objectModels['template']; |
| 146 |
// Link block templates. |
| 147 |
$links = $object->links->link ? $object->links->link : array(); |
| 148 |
foreach ($links as $link) { |
| 149 |
// Get template object to link. |
| 150 |
$templateToLinkAttributes = (array) $link->attributes(); |
| 151 |
$templateToLinkAttributes = $templateToLinkAttributes['@attributes']; |
| 152 |
$tblTemplate = CJTxTable::getInstance('template') |
| 153 |
->setData($templateToLinkAttributes) // Query by the given attributes. |
| 154 |
->load(array_keys($templateToLinkAttributes)); |
| 155 |
if ($tblTemplate->get('id')) { |
| 156 |
// Cache template id. |
| 157 |
$templateId = $tblTemplate->get('id'); |
| 158 |
// Always link as the block should be newely added |
| 159 |
// and in the normal cases its impossible to be alread linked! |
| 160 |
$tableBlockTemplate = CJTxTable::getInstance('block-template'); |
| 161 |
$tableBlockTemplate->set('blockId', $objectId) |
| 162 |
->set('templateId', $templateId) |
| 163 |
->save(); |
| 164 |
// Add only LINKED objects to the package objects map table! |
| 165 |
if (!key_exists($templateId, $addedObjects['template'])) { |
| 166 |
$addedObjects['template'][$templateId] = array('objectId' => $templateId, 'relType' => 'link'); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
break; |
| 171 |
default: |
| 172 |
//throw new Exception('Invalid object type specified!'); |
| 173 |
break; |
| 174 |
} |
| 175 |
// Support pluggable nodes that will parsed only when it really used. |
| 176 |
$this->pluggables($object, $objectId, $package); |
| 177 |
// Add (associate with the package) last objectId only if the object is added |
| 178 |
// as a part of the package. |
| 179 |
if ($objectId) { |
| 180 |
$addedObjects[$objectType][$objectId] = array('objectId' => $objectId); |
| 181 |
} |
| 182 |
} |
| 183 |
// Add package to database! |
| 184 |
$packageId = $modelPackage->save($packageInfo, $addedObjects); |
| 185 |
// Return package Id |
| 186 |
return $packageId; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* put your comment there... |
| 191 |
* |
| 192 |
* @param mixed $packageFile |
| 193 |
*/ |
| 194 |
public function parse($name, $file) { |
| 195 |
// Initialize. |
| 196 |
$package = FALSE; |
| 197 |
// Use a tmp dir with the same name as package without the extension. |
| 198 |
$packageFileComponent = explode('.', $name); |
| 199 |
$destinationDir = get_temp_dir() . $packageFileComponent[0]; |
| 200 |
// Import WP_FileSystem and unzip_file functions required to unzip the file. |
| 201 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 202 |
// Initialize Wordpress file systems. |
| 203 |
WP_Filesystem(); |
| 204 |
// Extract package archive in temporary location. |
| 205 |
if (($result = unzip_file($file, $destinationDir)) === true) { |
| 206 |
// Import package definition class. |
| 207 |
cssJSToolbox::import('framework:packages:package.class.php'); |
| 208 |
$package = new CJTPackageFile($destinationDir); |
| 209 |
} |
| 210 |
return $package; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* put your comment there... |
| 215 |
* |
| 216 |
* @param mixed $node |
| 217 |
* @param mixed $objectId |
| 218 |
* @param mixed $package |
| 219 |
* @return CJTPackageFileModel |
| 220 |
*/ |
| 221 |
public function pluggables($node, $objectId, & $package) { |
| 222 |
// Get packages definition xml factory. |
| 223 |
$factory = new CJT_Models_Package_Xml_Factory('models/package/xml/definition/objects'); |
| 224 |
// Based on the object type get instance of the object |
| 225 |
// parser with the object node passed. |
| 226 |
// call plug method so the object would start parsing the |
| 227 |
// and plug the child elements. |
| 228 |
$object = $factory->create(null, (string) $node->attributes()->type, $node); // OBJECT CONSTR&UCTED HERE! |
| 229 |
// Set object id! |
| 230 |
$object->register()->offsetSet('id', $objectId); |
| 231 |
$object->register()->offsetSet('packageParser', $package); |
| 232 |
// Plug it! |
| 233 |
$object->transit() |
| 234 |
->processInners(); |
| 235 |
// Chaining. |
| 236 |
return $this; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* put your comment there... |
| 241 |
* |
| 242 |
* @param mixed $property |
| 243 |
* @param mixed $state |
| 244 |
*/ |
| 245 |
public function setState($property, $state) { |
| 246 |
// Set State! |
| 247 |
$this->state[$property] = $state; |
| 248 |
// Chaining. |
| 249 |
return $this; |
| 250 |
} |
| 251 |
} // End class. |
| 252 |
|
| 253 |
// Hookable! |
| 254 |
CJTPackageFileModel::define('CJTPackageFileModel', array('hookType' => CJTWordpressEvents::HOOK_FILTER)); |