Exemple #1
0
 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        ");
 }