/** * Do nothing! * */ public function transit() { // Initialize. $model = CJTModel::getInstance('template'); $node = $this->getNode(); $register = $this->register(); // Prepare object + getting item to be saved into database. $template = $register['packageParser']->getItem($node); // Insert template only if not exists. if (!$model->exists($template['name'])) { // Import template(s) helper. cssJSToolbox::import('includes:templates:templates.class.php'); // Set template revision. $model->inputs['item']['revision']['code'] = $template['code']; unset($template['code']); // Set template main data. $model->inputs['item']['template'] = $template; /** Get template Type! * Get type from external file extension if * the template code was linked to file. * If the template code were inline * then the type must be provided under * TYPE element! */ // If no type specified get it from the external file extension if (!isset($model->inputs['item']['template']['type'])) { // @WARNING: Get locatted file! $codeFileName = (string) $node->code->attributes()->locatted; if ($codeFileName) { // Get type from extension. $fileComponent = pathinfo($codeFileName); $model->inputs['item']['template']['type'] = CJTTemplates::getExtensionType($fileComponent['extension']); } } // Add template. $addedTemplate = $model->save(); // Expose template fields to be by used by other objects. $register['templateId'] = $addedTemplate->templateId; $register['templateFile'] = $addedTemplate->file; } // Chaining. return $this; }
/** * Link external Resources (CSS, HTML, JS and PHP) * * The action is to create external link as CJT template * and link it to the target block. */ protected function linkExternalAction() { // Import dependencies. cssJSToolbox::import('includes:templates:templates.class.php'); // Initialize response as successed until error occured! $this->response = array('code' => 0, 'message' => ''); // List of all the external templates records to create! $externalTemplates = array(); // Read inputs. $externals = explode(',', $_REQUEST['externals']); $blockId = (int) $_REQUEST['blockId']; // Add as templates. foreach ($externals as $externalResourceURI) { // Use URI base name as Template Name and the extension as Template Type. $externalPathInfo = pathinfo($externalResourceURI); // Template Item. $item = array(); $item['template']['name'] = $externalPathInfo['basename']; $item['template']['type'] = CJTTemplates::getExtensionType($externalPathInfo['extension']); $item['template']['state'] = 'published'; // Get external URI code! $externalResponse = wp_remote_get($externalResourceURI); if ($error = $externalResponse instanceof WP_Error) { // State an error! $this->response['code'] = $externalResponse->get_error_code(); $this->response['message'] = $externalResponse->get_error_message($this->response['code']); break; } else { // Read code content. $item['revision']['code'] = wp_remote_retrieve_body($externalResponse); // Add to the save list! $externalTemplates[] = $item; } } // Save all templates if no error occured // Single error will halt all the linked externals! They all added as a single transaction. if (!$this->response['code']) { // Instantiate Template Models. $modelLookup = CJTModel::getInstance('templates-lookup'); $modelTemplate = CJTModel::getInstance('template'); $modelBlockTemplates = CJTModel::getInstance('block-templates'); // Add all templates. foreach ($externalTemplates as $item) { // Check existance. $modelTemplate->inputs['filter']['field'] = 'name'; $modelTemplate->inputs['filter']['value'] = $item['template']['name']; if (!($existsItem = $modelTemplate->getTemplateBy()) || !property_exists($existsItem, 'id') || !$existsItem->id) { // Create template. $modelTemplate->inputs['item'] = $item; $item = (array) $modelTemplate->save(); } else { // The returned item has 'id' field not 'templateId'!! $item = array('templateId' => $existsItem->id); } // Link only if not already linked! if (!$modelBlockTemplates->isLinked($blockId, $item['templateId'])) { // Link template to the block. $modelLookup->inputs['templateId'] = $item['templateId']; $modelLookup->inputs['blockId'] = $blockId; $modelLookup->link(); } } } }