public function renderHtml() { foreach ($this->_params['dataClasses'] as &$dataClass) { $dataClass['fieldsList'] = implode(', ', array_keys($dataClass['fields'])); } if (!empty($this->_params['focusedDataClass'])) { $this->_params['focusedDataClass']['sqlCreate'] = DevHelper_Generator_Db::createTable($this->_params['config'], $this->_params['focusedDataClass']); } }
public static function generate(array $addOn, DevHelper_Config_Base $config, array $dataClass) { $className = self::getClassName($addOn, $config, $dataClass); $tableName = DevHelper_Generator_Db::getTableName($config, $dataClass['name']); $tableFields = $dataClass['fields']; foreach ($tableFields as &$field) { unset($field['name']); if (!empty($field['length'])) { $field['maxLength'] = $field['length']; unset($field['length']); } } $tableFields = DevHelper_Generator_File::varExport($tableFields, 3); $primaryKey = DevHelper_Generator_File::varExport($dataClass['primaryKey']); $modelClassName = DevHelper_Generator_Code_Model::getClassName($addOn, $config, $dataClass); $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START; $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END; $contents = <<<EOF <?php class {$className} extends XenForo_DataWriter { \t{$commentAutoGeneratedStart} \t \tprotected function _getFields() { \t\treturn array( \t\t\t'{$tableName}' => {$tableFields} \t\t); \t} \tprotected function _getExistingData(\$data) { \t\tif (!\$id = \$this->_getExistingPrimaryKey(\$data, '{$dataClass['id_field']}')) { \t\t\treturn false; \t\t} \t\treturn array('{$tableName}' => \$this->_get{$dataClass['camelCase']}Model()->get{$dataClass['camelCase']}ById(\$id)); \t} \tprotected function _getUpdateCondition(\$tableName) { \t\t\$conditions = array(); \t\t \t\tforeach ({$primaryKey} as \$field) { \t\t\t\$conditions[] = \$field . ' = ' . \$this->_db->quote(\$this->getExisting(\$field)); \t\t} \t\t \t\treturn implode(' AND ', \$conditions); \t} \t \tprotected function _get{$dataClass['camelCase']}Model() { \t\treturn \$this->getModelFromCache('{$modelClassName}'); \t} \t \t{$commentAutoGeneratedEnd} } EOF; return array($className, $contents); }
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(); }
public function addDataPatch($table, array $patch) { if (!empty($patch['index'])) { if (!isset($patch['name'])) { $patch['name'] = implode('_', $patch['fields']); } $patchKey = 'index::' . $patch['name']; if (isset($patch['type'])) { $patch['type'] = strtoupper($patch['type']); } else { $patch['type'] = ''; } if (!in_array($patch['type'], DevHelper_Generator_Db::getIndexTypes())) { $patch['type'] = 'NORMAL'; } if (!isset($patch['fields'])) { throw new XenForo_Exception('addDataPatch(index=true) requires `fields`'); } if (!is_array($patch['fields'])) { $patch['fields'] = array(strval($patch['fields'])); } } else { $patch['name'] = DevHelper_Generator_Db::getFieldName($this, $this->_normalizeDbName($patch['name'])); $patchKey = $patch['name']; if (!isset($patch['type'])) { throw new XenForo_Exception('addDataPatch() requires `type`'); } $patch['type'] = strtolower($patch['type']); if (!in_array($patch['type'], DevHelper_Generator_Db::getDataTypes())) { $patch['type'] = XenForo_DataWriter::TYPE_SERIALIZED; } } $this->_dataPatches[$table][$patchKey] = $patch; return true; }
public static function generate(array $addOn, DevHelper_Config_Base $config) { $className = self::getClassName($addOn, $config); $tables = array(); $dataClasses = $config->getDataClasses(); foreach ($dataClasses as $dataClass) { $table = array(); $table['createQuery'] = DevHelper_Generator_Db::createTable($config, $dataClass); $table['dropQuery'] = false; $tables[$dataClass['name']] = $table; } $tables = DevHelper_Generator_File::varExport($tables); $patches = array(); $dataPatches = $config->getDataPatches(); foreach ($dataPatches as $table => $tablePatches) { foreach ($tablePatches as $dataPatch) { $patch = array(); $patch['table'] = $table; $patch['field'] = $dataPatch['name']; $patch['showColumnsQuery'] = DevHelper_Generator_Db::showColumns($config, $table, $dataPatch); $patch['alterTableAddColumnQuery'] = DevHelper_Generator_Db::alterTableAddColumn($config, $table, $dataPatch); $patch['alterTableDropColumnQuery'] = false; $patches[] = $patch; } } $patches = DevHelper_Generator_File::varExport($patches); $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START; $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END; $contents = <<<EOF <?php class {$className} { \t{$commentAutoGeneratedStart} \tprotected static \$_tables = {$tables}; \tprotected static \$_patches = {$patches}; \tpublic static function install() { \t\t\$db = XenForo_Application::get('db'); \t\tforeach (self::\$_tables as \$table) { \t\t\t\$db->query(\$table['createQuery']); \t\t} \t\t \t\tforeach (self::\$_patches as \$patch) { \t\t\t\$existed = \$db->fetchOne(\$patch['showColumnsQuery']); \t\t\tif (empty(\$existed)) { \t\t\t\t\$db->query(\$patch['alterTableAddColumnQuery']); \t\t\t} \t\t} \t} \t \tpublic static function uninstall() { \t\t// TODO \t} \t{$commentAutoGeneratedStart} \t } EOF; return array($className, $contents); }
protected function _generateParentCode() { $parentField = DevHelper_Generator_Db::getParentField($this->_dataClass['name'], $this->_dataClass['fields']); if ($parentField === false) { // no parent field... return; } $displayOrderField = false; $depthField = false; $lftField = false; $rgtField = false; foreach ($this->_dataClass['fields'] as $field) { if ($field['name'] == 'display_order') { $displayOrderField = $field['name']; } elseif ($field['name'] == 'depth') { $depthField = $field['name']; } elseif ($field['name'] == 'lft') { $lftField = $field['name']; } elseif ($field['name'] == 'rgt') { $rgtField = $field['name']; } } if (empty($displayOrderField) or empty($depthField) or empty($lftField) or empty($rgtField)) { // no hierarchy fields return; } $tableName = DevHelper_Generator_Db::getTableName($this->_config, $this->_dataClass['name']); $rebuildStructureFunctionName = DevHelper_Generator_Code_Model::generateRebuildStructureFunctionName($this->_addOn, $this->_config, $this->_dataClass); $titleFieldPostSaveConditions = ''; if (!empty($this->_dataClass['title_field']) and !is_array($this->_dataClass['title_field'])) { $titleFieldPostSaveConditions = "\n OR \$this->isChanged('{$this->_dataClass['title_field']}')"; } $this->_addMethod('_postSave', 'protected', array(), "\n\nif (\$this->isInsert()\n OR \$this->isChanged('{$displayOrderField}')\n OR \$this->isChanged('{$parentField}'){$titleFieldPostSaveConditions}\n) {\n \$this->_get{$this->_dataClass['camelCase']}Model()->{$rebuildStructureFunctionName}();\n}\n\n "); $this->_addMethod('_postDelete', 'protected', array(), "\n\n\$this->_db->update('{$tableName}',\n array('{$parentField}' => \$this->get('{$parentField}')),\n '{$parentField} = ' . \$this->_db->quote(\$this->get('{$parentField}'))\n);\n\n\$this->_get{$this->_dataClass['camelCase']}Model()->{$rebuildStructureFunctionName}();\n\n "); }
public function addDataPatch($table, array $field) { $field['name'] = DevHelper_Generator_Db::getFieldName($this, $this->_normalizeDbName($field['name'])); $field['type'] = strtolower($field['type']); if (!in_array($field['type'], DevHelper_Generator_Db::getDataTypes())) { $field['type'] = XenForo_DataWriter::TYPE_SERIALIZED; } $this->_dataPatches[$table][$field['name']] = $field; return true; }
public static function generate(array $addOn, DevHelper_Config_Base $config, array $dataClass) { $className = self::getClassName($addOn, $config, $dataClass); $tableName = DevHelper_Generator_Db::getTableName($config, $dataClass['name']); $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START; $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END; $intFields = DevHelper_Generator_File::varExport(DevHelper_Generator_Db::getIntFields($dataClass['fields'])); $contents = <<<EOF <?php class {$className} extends XenForo_Model { \t{$commentAutoGeneratedStart} \tpublic function getList(array \$conditions = array(), array \$fetchOptions = array()) { \t\t\$data = \$this->getAll{$dataClass['camelCase']}(\$conditions, \$fetchOptions); \t\t\$list = array(); \t\t \t\tforeach (\$data as \$id => \$row) { \t\t\t\$list[\$id] = \$row['{$dataClass['title_field']}']; \t\t} \t\t \t\treturn \$list; \t} \tpublic function get{$dataClass['camelCase']}ById(\$id, array \$fetchOptions = array()) { \t\t\$data = \$this->getAll{$dataClass['camelCase']}(array ('{$dataClass['id_field']}' => \$id), \$fetchOptions); \t\t \t\treturn reset(\$data); \t} \t \tpublic function getAll{$dataClass['camelCase']}(array \$conditions = array(), array \$fetchOptions = array()) { \t\t\$whereConditions = \$this->prepare{$dataClass['camelCase']}Conditions(\$conditions, \$fetchOptions); \t\t\$orderClause = \$this->prepare{$dataClass['camelCase']}OrderOptions(\$fetchOptions); \t\t\$joinOptions = \$this->prepare{$dataClass['camelCase']}FetchOptions(\$fetchOptions); \t\t\$limitOptions = \$this->prepareLimitFetchOptions(\$fetchOptions); \t\treturn \$this->fetchAllKeyed(\$this->limitQueryResults(" \t\t\t\tSELECT {$dataClass['name']}.* \t\t\t\t\t\$joinOptions[selectFields] \t\t\t\tFROM `{$tableName}` AS {$dataClass['name']} \t\t\t\t\t\$joinOptions[joinTables] \t\t\t\tWHERE \$whereConditions \t\t\t\t\t\$orderClause \t\t\t", \$limitOptions['limit'], \$limitOptions['offset'] \t\t), '{$dataClass['id_field']}'); \t} \t\t \tpublic function countAll{$dataClass['camelCase']}(array \$conditions = array(), array \$fetchOptions = array()) { \t\t\$whereConditions = \$this->prepare{$dataClass['camelCase']}Conditions(\$conditions, \$fetchOptions); \t\t\$orderClause = \$this->prepare{$dataClass['camelCase']}OrderOptions(\$fetchOptions); \t\t\$joinOptions = \$this->prepare{$dataClass['camelCase']}FetchOptions(\$fetchOptions); \t\t\$limitOptions = \$this->prepareLimitFetchOptions(\$fetchOptions); \t\treturn \$this->_getDb()->fetchOne(" \t\t\tSELECT COUNT(*) \t\t\tFROM `{$tableName}` AS {$dataClass['name']} \t\t\t\t\$joinOptions[joinTables] \t\t\tWHERE \$whereConditions \t\t"); \t} \t \tpublic function prepare{$dataClass['camelCase']}Conditions(array \$conditions, array &\$fetchOptions) { \t\t\$sqlConditions = array(); \t\t\$db = \$this->_getDb(); \t\t \t\tforeach ({$intFields} as \$intField) { \t\t\tif (!isset(\$conditions[\$intField])) continue; \t\t\t \t\t\tif (is_array(\$conditions[\$intField])) { \t\t\t\t\$sqlConditions[] = "{$dataClass['name']}.\$intField IN (" . \$db->quote(\$conditions[\$intField]) . ")"; \t\t\t} else { \t\t\t\t\$sqlConditions[] = "{$dataClass['name']}.\$intField = " . \$db->quote(\$conditions[\$intField]); \t\t\t} \t\t} \t\t \t\treturn \$this->getConditionsForClause(\$sqlConditions); \t} \t \tpublic function prepare{$dataClass['camelCase']}FetchOptions(array \$fetchOptions) { \t\t\$selectFields = ''; \t\t\$joinTables = ''; \t\treturn array( \t\t\t'selectFields' => \$selectFields, \t\t\t'joinTables' => \$joinTables \t\t); \t} \t \tpublic function prepare{$dataClass['camelCase']}OrderOptions(array &\$fetchOptions, \$defaultOrderSql = '') { \t\t\$choices = array( \t\t\t \t\t); \t\treturn \$this->getOrderByClause(\$choices, \$fetchOptions, \$defaultOrderSql); \t} \t{$commentAutoGeneratedStart} } EOF; return array($className, $contents); }
protected function _generateParentCode() { $parentField = DevHelper_Generator_Db::getParentField($this->_dataClass['name'], $this->_dataClass['fields']); if ($parentField === false) { // no parent field... return; } if (count($this->_dataClass['primaryKey']) > 1) { throw new XenForo_Exception(sprintf('Cannot generate parent code for %s: too many fields in primary key', $this->_getClassName())); } $idField = reset($this->_dataClass['primaryKey']); $displayOrderField = false; $depthField = false; $lftField = false; $rgtField = false; $breadcrumbField = DevHelper_Generator_Db::getBreadcrumbField($this->_dataClass['name'], $this->_dataClass['fields']); foreach ($this->_dataClass['fields'] as $field) { if ($field['name'] == 'display_order') { $displayOrderField = $field['name']; } elseif ($field['name'] == 'depth') { $depthField = $field['name']; } elseif ($field['name'] == 'lft') { $lftField = $field['name']; } elseif ($field['name'] == 'rgt') { $rgtField = $field['name']; } } if (empty($displayOrderField) or empty($depthField) or empty($lftField) or empty($rgtField)) { // no hierarchy fields return; } $tableName = DevHelper_Generator_Db::getTableName($this->_config, $this->_dataClass['name']); $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); $rebuildStructureFunctionName = self::generateRebuildStructureFunctionName($this->_addOn, $this->_config, $this->_dataClass); $getStructureChangesFunctionName = '_getStructureChanges'; $groupByParentsFunctionName = self::generateGroupByParentsFunctionName($this->_addOn, $this->_config, $this->_dataClass); $this->_addMethod($rebuildStructureFunctionName, '', array(), "\n\n\$grouped = \$this->{$groupByParentsFunctionName}(\$this->{$getFunctionName}(array(), array('order' => '{$displayOrderField}')));\n\n\$db = \$this->_getDb();\nXenForo_Db::beginTransaction(\$db);\n\n\$changes = \$this->{$getStructureChangesFunctionName}(\$grouped);\nforeach (\$changes AS \$id => \$oneChanges) {\n \$db->update('{$tableName}', \$oneChanges, '{$idField} = ' . \$db->quote(\$id));\n}\n\nXenForo_Db::commit(\$db);\n\nreturn \$changes;\n\n "); $titleFieldBreadcrumb = ''; if (!empty($this->_dataClass['title_field']) and !is_array($this->_dataClass['title_field'])) { $titleFieldBreadcrumb = "\n '{$this->_dataClass['title_field']}' => \${$variableName}['{$this->_dataClass['title_field']}'],"; } $breadcrumbStatements = ''; if (!empty($breadcrumbField)) { $breadcrumbStatements = "\n if (\${$variableName}['category_breadcrumb'] != \$serializedBreadcrumb) {"; $breadcrumbStatements .= "\n \$thisChanges['category_breadcrumb'] = \$serializedBreadcrumb;"; $breadcrumbStatements .= "\n }"; } $this->_addMethod($getStructureChangesFunctionName, 'protected', array('$grouped' => 'array $grouped', '$parentId' => '$parentId = 0', '$depth' => '$depth = 0', '$startPosition' => '$startPosition = 1', 'nextPosition' => '&$nextPosition = 0', '$breadcrumb' => 'array $breadcrumb = array()'), "\n\n\$nextPosition = \$startPosition;\n\nif (!isset(\$grouped[\$parentId])) {\n return array();\n}\n\n\$changes = array();\n\$serializedBreadcrumb = serialize(\$breadcrumb);\n\nforeach (\$grouped[\$parentId] AS \$id => \${$variableName}) {\n \$left = \$nextPosition;\n \$nextPosition++;\n\n \$thisBreadcrumb = \$breadcrumb + array(\n \$id => array(\n '{$idField}' => \$id,{$titleFieldBreadcrumb}\n '{$parentField}' => \${$variableName}['{$parentField}'],\n )\n );\n\n \$changes += \$this->{$getStructureChangesFunctionName}(\n \$grouped,\n \$id,\n \$depth + 1,\n \$nextPosition,\n \$nextPosition,\n \$thisBreadcrumb\n );\n\n \$thisChanges = array();\n if (\${$variableName}['depth'] != \$depth) {\n \$thisChanges['depth'] = \$depth;\n }\n if (\${$variableName}['lft'] != \$left) {\n \$thisChanges['lft'] = \$left;\n }\n if (\${$variableName}['rgt'] != \$nextPosition) {\n \$thisChanges['rgt'] = \$nextPosition;\n }{$breadcrumbStatements}\n\n if (!empty(\$thisChanges)) {\n \$changes[\$id] = \$thisChanges;\n }\n\n \$nextPosition++;\n}\n\nreturn \$changes;\n\n "); $this->_addMethod($groupByParentsFunctionName, '', array(sprintf('$%s', $variableNamePlural) => sprintf('array $%s', $variableNamePlural)), "\n\n\$grouped = array();\nforeach (\${$variableNamePlural} AS \${$variableName}) {\n \$grouped[\${$variableName}['{$parentField}']][\${$variableName}['{$idField}']] = \${$variableName};\n}\n\nreturn \$grouped;\n\n "); }
public static function generate(array $addOn, DevHelper_Config_Base $config) { $className = self::getClassName($addOn, $config); $tables = array(); $dataClasses = $config->getDataClasses(); foreach ($dataClasses as $dataClass) { $table = array(); $table['createQuery'] = DevHelper_Generator_Db::createTable($config, $dataClass); $table['dropQuery'] = DevHelper_Generator_Db::dropTable($config, $dataClass); $tables[$dataClass['name']] = $table; } $tables = DevHelper_Generator_File::varExport($tables); $patches = array(); $dataPatches = $config->getDataPatches(); foreach ($dataPatches as $table => $tablePatches) { foreach ($tablePatches as $dataPatch) { $patch = array(); $patch['table'] = $table; $patch['tableCheckQuery'] = DevHelper_Generator_Db::showTables($config, $table); if (!empty($dataPatch['index'])) { $patch['index'] = $dataPatch['name']; $patch['checkQuery'] = DevHelper_Generator_Db::showIndexes($config, $table, $dataPatch); $patch['addQuery'] = DevHelper_Generator_Db::alterTableAddIndex($config, $table, $dataPatch); $patch['dropQuery'] = DevHelper_Generator_Db::alterTableDropIndex($config, $table, $dataPatch); } else { $patch['field'] = $dataPatch['name']; $patch['checkQuery'] = DevHelper_Generator_Db::showColumns($config, $table, $dataPatch); $patch['addQuery'] = DevHelper_Generator_Db::alterTableAddColumn($config, $table, $dataPatch); $patch['dropQuery'] = DevHelper_Generator_Db::alterTableDropColumn($config, $table, $dataPatch); } $patches[] = $patch; } } $patches = DevHelper_Generator_File::varExport($patches); $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START; $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END; $contents = <<<EOF <?php class {$className} { {$commentAutoGeneratedStart} protected static \$_tables = {$tables}; protected static \$_patches = {$patches}; public static function install(\$existingAddOn, \$addOnData) { \$db = XenForo_Application::get('db'); foreach (self::\$_tables as \$table) { \$db->query(\$table['createQuery']); } foreach (self::\$_patches as \$patch) { \$tableExisted = \$db->fetchOne(\$patch['tableCheckQuery']); if (empty(\$tableExisted)) { continue; } \$existed = \$db->fetchOne(\$patch['checkQuery']); if (empty(\$existed)) { \$db->query(\$patch['addQuery']); } } self::installCustomized(\$existingAddOn, \$addOnData); } public static function uninstall() { \$db = XenForo_Application::get('db'); foreach (self::\$_patches as \$patch) { \$tableExisted = \$db->fetchOne(\$patch['tableCheckQuery']); if (empty(\$tableExisted)) { continue; } \$existed = \$db->fetchOne(\$patch['checkQuery']); if (!empty(\$existed)) { \$db->query(\$patch['dropQuery']); } } foreach (self::\$_tables as \$table) { \$db->query(\$table['dropQuery']); } self::uninstallCustomized(); } {$commentAutoGeneratedEnd} public static function installCustomized(\$existingAddOn, \$addOnData) { // customized install script goes here } public static function uninstallCustomized() { // customized uninstall script goes here } } EOF; return array($className, $contents); }