Pimcore:如何覆盖内置控制器

时间:2016-07-15 20:47:11

标签: php model-view-controller plugins module pimcore

我正在尝试扩展Pimcore中的批量对象导入功能,以限制用户使用他们在自定义布局中可用的字段。

我需要覆盖的文件是

pimcore /模块/管理/控制器/ ObjectController.php

我相信我需要扩展的方法是:

public function importGetFileInfoAction()
{
    $success = true;
    $supportedFieldTypes = ["checkbox", "country", "date", "datetime", "href", "image", "input", "language", "table", "multiselect", "numeric", "password", "select", "slider", "textarea", "wysiwyg", "objects", "multihref", "geopoint", "geopolygon", "geobounds", "link", "user", "email", "gender", "firstname", "lastname", "newsletterActive", "newsletterConfirmed", "countrymultiselect", "objectsMetadata"];
    $file = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id");
    // determine type
    $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id") . "_original");
    $count = 0;
    if (($handle = fopen($file, "r")) !== false) {
        while (($rowData = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) {
            if ($count == 0) {
                $firstRowData = $rowData;
            }
            $tmpData = [];
            foreach ($rowData as $key => $value) {
                $tmpData["field_" . $key] = $value;
            }
            $data[] = $tmpData;
            $cols = count($rowData);
            $count++;
            if ($count > 18) {
                break;
            }
        }
        fclose($handle);
    }
    // get class data
    $class = Object\ClassDefinition::getById($this->getParam("classId"));
    $fields = $class->getFieldDefinitions();
    $availableFields = [];
    foreach ($fields as $key => $field) {
        $config = null;
        $title = $field->getName();
        if (method_exists($field, "getTitle")) {
            if ($field->getTitle()) {
                $title = $field->getTitle();
            }
        }
        if (in_array($field->getFieldType(), $supportedFieldTypes)) {
            $availableFields[] = [$field->getName(), $title . "(" . $field->getFieldType() . ")"];
        }
    }
    $mappingStore = [];
    for ($i = 0; $i < $cols; $i++) {
        $mappedField = null;
        if ($availableFields[$i]) {
            $mappedField = $availableFields[$i][0];
        }
        $firstRow = $i;
        if (is_array($firstRowData)) {
            $firstRow = $firstRowData[$i];
            if (strlen($firstRow) > 40) {
                $firstRow = substr($firstRow, 0, 40) . "...";
            }
        }
        $mappingStore[] = [
            "source" => $i,
            "firstRow" => $firstRow,
            "target" => $mappedField
        ];
    }
    //How many rows
    $csv = new SplFileObject($file);
    $csv->setFlags(SplFileObject::READ_CSV);
    $csv->setCsvControl($dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
    $rows = 0;
    $nbFields = 0;
    foreach ($csv as $fields) {
        if (0 === $rows) {
            $nbFields = count($fields);
            $rows++;
        } elseif ($nbFields == count($fields)) {
            $rows++;
        }
    }
    $this->_helper->json([
        "success" => $success,
        "dataPreview" => $data,
        "dataFields" => array_keys($data[0]),
        "targetFields" => $availableFields,
        "mappingStore" => $mappingStore,
        "rows" => $rows,
        "cols" => $cols
    ]);
}

特别是这两行:

$class = Object\ClassDefinition::getById($this->getParam("classId"));

$fields = $class->getFieldDefinitions();

我想要做的是弄清楚如何获取用户可以在自定义布局中看到的字段,然后相应地过滤掉$fields。我想避免覆盖核心文件,而是希望用插件来扩展它。

1 个答案:

答案 0 :(得分:2)

您可能必须创建一个覆盖UI中菜单项的插件,以便用自定义对象替换标准对象导入。

您需要创建一个CustomObjectController,然后可以扩展标准ObjectController。