コード例 #1
0
 protected function _generateImageCode()
 {
     $imageField = DevHelper_Generator_Db::getImageField($this->_dataClass['fields']);
     if ($imageField === false) {
         // no image field...
         return false;
     }
     $modelClassName = DevHelper_Generator_Code_Model::getClassName($this->_addOn, $this->_config, $this->_dataClass);
     $this->_addConstant('DATA_IMAGE_PREPARED', '\'imagePrepared\'');
     $this->_addConstant('IMAGE_SIZE_ORIGINAL', '-1');
     $this->_addProperty('$imageQuality', 'public static $imageQuality = 85');
     $this->_addMethod('setImage', 'public', array('$upload' => 'XenForo_Upload $upload'), "\n\nif (!\$upload->isValid()) {\n    throw new XenForo_Exception(\$upload->getErrors(), true);\n}\n\nif (!\$upload->isImage()) {\n    throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);\n};\n\n\$imageType = \$upload->getImageInfoField('type');\nif (!in_array(\$imageType, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {\n    throw new XenForo_Exception(new XenForo_Phrase('uploaded_file_is_not_valid_image'), true);\n}\n\n\$this->setExtraData(self::DATA_IMAGE_PREPARED, \$this->_prepareImage(\$upload));\n\$this->set('{$imageField}', XenForo_Application::\$time);\n\n        ");
     $this->_addMethod('_prepareImage', 'protected', array('$upload' => 'XenForo_Upload $upload'), "\n\n\$outputFiles = array();\n\$fileName = \$upload->getTempFile();\n\$imageType = \$upload->getImageInfoField('type');\n\$outputType = \$imageType;\n\$width = \$upload->getImageInfoField('width');\n\$height = \$upload->getImageInfoField('height');\n\n\$imageSizes = \$this->getImageSizes();\nreset(\$imageSizes);\n\nwhile (list(\$sizeCode, \$maxDimensions) = each(\$imageSizes)) {\n    \$newTempFile = tempnam(XenForo_Helper_File::getTempDir(), 'xfa');\n\n    if (\$maxDimensions == self::IMAGE_SIZE_ORIGINAL) {\n        copy(\$fileName, \$newTempFile);\n    } else {\n        \$image = XenForo_Image_Abstract::createFromFile(\$fileName, \$imageType);\n        if (!\$image) {\n            continue;\n        }\n\n        \$image->thumbnailFixedShorterSide(\$maxDimensions);\n        if (\$image->getWidth() > \$maxDimensions OR \$image->getHeight() > \$maxDimensions) {\n            \$image->crop(0, 0, \$maxDimensions, \$maxDimensions);\n        }\n\n        \$image->output(\$outputType, \$newTempFile, self::\$imageQuality);\n        unset(\$image);\n    }\n\n    \$outputFiles[\$sizeCode] = \$newTempFile;\n}\n\nif (count(\$outputFiles) != count(\$imageSizes)) {\n    foreach (\$outputFiles AS \$tempFile) {\n        if (\$tempFile != \$fileName) {\n            @unlink(\$tempFile);\n        }\n    }\n\n    throw new XenForo_Exception('Non-image passed in to _prepareImage');\n}\n\nreturn \$outputFiles;\n\n        ");
     $this->_addMethod('_moveImages', 'protected', array('$uploaded'), "\n\nif (is_array(\$uploaded)) {\n    \$data = \$this->getMergedData();\n    foreach (\$uploaded as \$sizeCode => \$tempFile) {\n        \$filePath = {$modelClassName}::getImageFilePath(\$data, \$sizeCode);\n        \$directory = dirname(\$filePath);\n\n        if (XenForo_Helper_File::createDirectory(\$directory, true)) {\n            XenForo_Helper_File::safeRename(\$tempFile, \$filePath);\n        }\n    }\n}\n\n        ");
     $this->_addMethod('_postSave', 'protected', array(), "\n\n\$uploaded = \$this->getExtraData(self::DATA_IMAGE_PREPARED);\nif (\$uploaded) {\n    \$this->_moveImages(\$uploaded);\n\n    if (\$this->isUpdate()) {\n        // removes old image\n        \$existingData = \$this->getMergedExistingData();\n        foreach (array_keys(\$this->getImageSizes()) as \$sizeCode) {\n            \$filePath = {$modelClassName}::getImageFilePath(\$existingData, \$sizeCode);\n            @unlink(\$filePath);\n        }\n    }\n}\n\n        ");
     $this->_addMethod('_postDelete', 'protected', array(), "\n\n\$existingData = \$this->getMergedExistingData();\nforeach (array_keys(\$this->getImageSizes()) as \$sizeCode) {\n    \$filePath = {$modelClassName}::getImageFilePath(\$existingData, \$sizeCode);\n    @unlink(\$filePath);\n}\n\n        ");
     $this->_addMethod('getImageSizes', 'public', array(), "\n\nreturn array(\n    'x' => self::IMAGE_SIZE_ORIGINAL,\n    'l' => 96,\n    'm' => 48,\n    's' => 24\n);\n\n        ");
     return true;
 }
コード例 #2
0
 protected function _generate()
 {
     if (count($this->_dataClass['primaryKey']) > 1) {
         throw new XenForo_Exception(sprintf('Cannot generate %s: too many fields in primary key', $this->_info['controller']));
     }
     $idField = reset($this->_dataClass['primaryKey']);
     $variableName = DevHelper_Generator_Code_Model::getVariableName($this->_addOn, $this->_config, $this->_dataClass);
     $variableNamePlural = DevHelper_Generator_Code_Model::getVariableNamePlural($this->_addOn, $this->_config, $this->_dataClass);
     $modelClassName = DevHelper_Generator_Code_Model::getClassName($this->_addOn, $this->_config, $this->_dataClass);
     $modelGetFunctionName = DevHelper_Generator_Code_Model::generateGetDataFunctionName($this->_addOn, $this->_config, $this->_dataClass);
     $dataWriterClassName = DevHelper_Generator_Code_DataWriter::getClassName($this->_addOn, $this->_config, $this->_dataClass);
     $viewListClassName = $this->_getViewClassName('list');
     $viewEditClassName = $this->_getViewClassName('edit');
     $viewDeleteClassName = $this->_getViewClassName('delete');
     $imageField = DevHelper_Generator_Db::getImageField($this->_dataClass['fields']);
     list($templateList, $templateEdit, $templateDelete) = $this->_generateTemplates($variableName, $variableNamePlural, $imageField);
     $this->_setClassName($this->_info['controller']);
     $this->_setBaseClass('XenForo_ControllerAdmin_Abstract');
     $actionIndexConditions = array();
     $actionIndexFetchOptions = array();
     if (isset($this->_dataClass['fields']['display_order'])) {
         $actionIndexFetchOptions['order'] = 'display_order';
     }
     $actionIndexConditions = DevHelper_Generator_File::varExport($actionIndexConditions);
     $actionIndexFetchOptions = DevHelper_Generator_File::varExport($actionIndexFetchOptions);
     $this->_addMethod('actionIndex', 'public', array(), "\n\n\$conditions = {$actionIndexConditions};\n\$fetchOptions = {$actionIndexFetchOptions};\n\n\${$variableName}Model = \$this->_get{$this->_dataClass['camelCase']}Model();\n\${$variableNamePlural} = \${$variableName}Model->{$modelGetFunctionName}(\$conditions, \$fetchOptions);\n\n\$viewParams = array(\n    '{$variableNamePlural}' => \${$variableNamePlural},\n);\n\nreturn \$this->responseView('{$viewListClassName}', '{$templateList}', \$viewParams);\n        ");
     $this->_addMethod('actionAdd', 'public', array(), "\n\n\$viewParams = array(\n    '{$variableName}' => array(),\n);\n\n        ", '000');
     $this->_addMethod('actionAdd', 'public', array(), "\n\nreturn \$this->responseView('{$viewEditClassName}', '{$templateEdit}', \$viewParams);\n\n        ", '999');
     $this->_addMethod('actionEdit', 'public', array(), "\n\n\$id = \$this->_input->filterSingle('{$idField}', XenForo_Input::UINT);\n\${$variableName} = \$this->_get{$this->_dataClass['camelCase']}OrError(\$id);\n\n\$viewParams = array(\n    '{$variableName}' => \${$variableName},\n);\n\n        ", '000');
     $this->_addMethod('actionEdit', 'public', array(), "\n\nreturn \$this->responseView('{$viewEditClassName}', '{$templateEdit}', \$viewParams);\n\n        ", '999');
     $this->_addMethod('actionSave', 'public', array(), "\n\n\$this->_assertPostOnly();\n\n\$id = \$this->_input->filterSingle('{$idField}', XenForo_Input::UINT);\n\$dw = \$this->_get{$this->_dataClass['camelCase']}DataWriter();\nif (\$id) {\n    \$dw->setExistingData(\$id);\n}\n\n        ", '000');
     $this->_addMethod('actionSave', 'public', array(), "\n\n\$this->_prepareDwBeforeSaving(\$dw);\n\n\$dw->save();\n\nreturn \$this->responseRedirect(\n    XenForo_ControllerResponse_Redirect::SUCCESS,\n    XenForo_Link::buildAdminLink('{$this->_info['routePrefix']}')\n);\n\n        ", '999');
     $this->_addMethod('actionDelete', 'public', array(), "\n\n\$id = \$this->_input->filterSingle('{$idField}', XenForo_Input::UINT);\n\${$variableName} = \$this->_get{$this->_dataClass['camelCase']}OrError(\$id);\n\nif (\$this->isConfirmedPost()) {\n    \$dw = \$this->_get{$this->_dataClass['camelCase']}DataWriter();\n    \$dw->setExistingData(\$id);\n    \$dw->delete();\n\n    return \$this->responseRedirect(\n        XenForo_ControllerResponse_Redirect::SUCCESS,\n        XenForo_Link::buildAdminLink('{$this->_info['routePrefix']}')\n    );\n} else {\n    \$viewParams = array(\n        '{$variableName}' => \${$variableName},\n    );\n\n    return \$this->responseView('{$viewDeleteClassName}', '{$templateDelete}', \$viewParams);\n}\n\n        ");
     $phraseNotFound = $this->_getPhraseName('_not_found');
     $this->_addMethod("_get{$this->_dataClass['camelCase']}OrError", 'protected', array('$id', '$fetchOptions' => 'array $fetchOptions = array()'), "\n\n\${$variableName} = \$this->_get{$this->_dataClass['camelCase']}Model()->get{$this->_dataClass['camelCase']}ById(\$id, \$fetchOptions);\n\nif (empty(\${$variableName})) {\n    throw \$this->responseException(\$this->responseError(new XenForo_Phrase('{$phraseNotFound}'), 404));\n}\n\nreturn \${$variableName};\n\n        ");
     $this->_addMethod("_get{$this->_dataClass['camelCase']}Model", 'protected', array(), "\n\nreturn \$this->getModelFromCache('{$modelClassName}');\n\n        ");
     $this->_addMethod(" _get{$this->_dataClass['camelCase']}DataWriter", 'protected', array(), "\n\nreturn XenForo_DataWriter::create('{$dataWriterClassName}');\n\n        ");
     $this->_addCustomizableMethod('_prepareDwBeforeSaving', 'protected', array('$dw' => "{$dataWriterClassName} \$dw"));
     return parent::_generate();
 }
コード例 #3
0
ファイル: Model.php プロジェクト: maitandat1507/DevHelper
 protected function _generateImageCode()
 {
     $imageField = DevHelper_Generator_Db::getImageField($this->_dataClass['fields']);
     if ($imageField === false) {
         // no image field...
         return '';
     }
     if (count($this->_dataClass['primaryKey']) > 1) {
         throw new XenForo_Exception(sprintf('Cannot generate image code for %s: too many fields in primary key', $this->_getClassName()));
     }
     $idField = reset($this->_dataClass['primaryKey']);
     $getFunctionName = self::generateGetDataFunctionName($this->_addOn, $this->_config, $this->_dataClass);
     $variableName = self::getVariableName($this->_addOn, $this->_config, $this->_dataClass);
     $variableNamePlural = self::getVariableNamePlural($this->_addOn, $this->_config, $this->_dataClass);
     $dwClassName = DevHelper_Generator_Code_DataWriter::getClassName($this->_addOn, $this->_config, $this->_dataClass);
     $configPrefix = $this->_config->getPrefix();
     $imagePath = "{$configPrefix}/{$this->_dataClass['camelCase']}";
     $imagePath = strtolower($imagePath);
     $this->_addMethod($getFunctionName, '', array(), "\n\n// build image urls and make them ready for all the records\n\$imageSizes = XenForo_DataWriter::create('{$dwClassName}')->getImageSizes();\nforeach (\${$variableNamePlural} as &\${$variableName}) {\n    \${$variableName}['images'] = array();\n    if (!empty(\${$variableName}['{$imageField}'])) {\n        foreach (\$imageSizes as \$imageSizeCode => \$imageSize) {\n            \${$variableName}['images'][\$imageSizeCode] = \$this->getImageUrl(\${$variableName}, \$imageSizeCode);\n        }\n    }\n}\n\n        ", '100');
     $this->_addMethod('getImageFilePath', 'public static', array(sprintf('$%s', $variableName) => sprintf('array $%s', $variableName), '$size' => '$size = \'l\''), "\n\n\$internal = self::_getImageInternal(\${$variableName}, \$size);\n\nif (!empty(\$internal)) {\n    return XenForo_Helper_File::getExternalDataPath() . \$internal;\n} else {\n    return '';\n}\n\n        ");
     $this->_addMethod('getImageUrl', 'public static', array(sprintf('$%s', $variableName) => sprintf('array $%s', $variableName), '$size' => '$size = \'l\''), "\n\n\$internal = self::_getImageInternal(\${$variableName}, \$size);\n\nif (!empty(\$internal)) {\n    return XenForo_Application::\$externalDataUrl . \$internal;\n} else {\n    return '';\n}\n\n        ");
     $this->_addMethod('_getImageInternal', 'protected static', array(sprintf('$%s', $variableName) => sprintf('array $%s', $variableName), '$size'), "\n\nif (empty(\${$variableName}['{$idField}']) OR empty(\${$variableName}['{$imageField}'])) {\n    return '';\n}\n\nreturn '/{$imagePath}/' . \${$variableName}['{$idField}'] . '_' . \${$variableName}['{$imageField}'] . strtolower(\$size) . '.jpg';\n\n        ");
     return true;
 }