Admin_ObjectHelperController::importProcessAction PHP Метод

importProcessAction() публичный Метод

public importProcessAction ( )
    public function importProcessAction()
    {
        $success = true;
        $parentId = $this->getParam("parentId");
        $job = $this->getParam("job");
        $id = $this->getParam("id");
        $mappingRaw = \Zend_Json::decode($this->getParam("mapping"));
        $class = Object\ClassDefinition::getById($this->getParam("classId"));
        $skipFirstRow = $this->getParam("skipHeadRow") == "true";
        $fields = $class->getFieldDefinitions();
        $file = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $id;
        // currently only csv supported
        // determine type
        $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $id . "_original");
        $count = 0;
        if (($handle = fopen($file, "r")) !== false) {
            $data = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
        }
        if ($skipFirstRow && $job == 1) {
            //read the next row, we need to skip the head row
            $data = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar);
        }
        $tmpFile = $file . "_tmp";
        $tmpHandle = fopen($tmpFile, "w+");
        while (!feof($handle)) {
            $buffer = fgets($handle);
            fwrite($tmpHandle, $buffer);
        }
        fclose($handle);
        fclose($tmpHandle);
        unlink($file);
        rename($tmpFile, $file);
        // prepare mapping
        foreach ($mappingRaw as $map) {
            if ($map[0] !== "" && $map[1] && !empty($map[2])) {
                $mapping[$map[2]] = $map[0];
            } elseif ($map[1] == "published (system)") {
                $mapping["published"] = $map[0];
            } elseif ($map[1] == "type (system)") {
                $mapping["type"] = $map[0];
            }
        }
        // create new object
        $className = "Pimcore\\Model\\Object\\" . ucfirst($this->getParam("className"));
        $parent = Object::getById($this->getParam("parentId"));
        $objectKey = "object_" . $job;
        if ($this->getParam("filename") == "id") {
            $objectKey = null;
        } elseif ($this->getParam("filename") != "default") {
            $objectKey = Element\Service::getValidKey($data[$this->getParam("filename")], "object");
        }
        $overwrite = false;
        if ($this->getParam("overwrite") == "true") {
            $overwrite = true;
        }
        if ($parent->isAllowed("create")) {
            $intendedPath = $parent->getRealFullPath() . "/" . $objectKey;
            if ($overwrite) {
                $object = Object::getByPath($intendedPath);
                if (!$object instanceof Object\Concrete) {
                    //create new object
                    $object = \Pimcore::getDiContainer()->make($className);
                } elseif ($object instanceof Object\Concrete and !$object instanceof $className) {
                    //delete the old object it is of a different class
                    $object->delete();
                    $object = \Pimcore::getDiContainer()->make($className);
                } elseif ($object instanceof Object\Folder) {
                    //delete the folder
                    $object->delete();
                    $object = \Pimcore::getDiContainer()->make($className);
                } else {
                    //use the existing object
                }
            } else {
                $counter = 1;
                while (Object::getByPath($intendedPath) != null) {
                    $objectKey .= "_" . $counter;
                    $intendedPath = $parent->getRealFullPath() . "/" . $objectKey;
                    $counter++;
                }
                $object = new $className();
            }
            $object->setClassId($this->getParam("classId"));
            $object->setClassName($this->getParam("className"));
            $object->setParentId($this->getParam("parentId"));
            $object->setKey($objectKey);
            $object->setCreationDate(time());
            $object->setUserOwner($this->getUser()->getId());
            $object->setUserModification($this->getUser()->getId());
            if (in_array($data[$mapping["type"]], ["object", "variant"])) {
                $object->setType($data[$mapping["type"]]);
            } else {
                $object->setType("object");
            }
            if ($data[$mapping["published"]] === "1") {
                $object->setPublished(true);
            } else {
                $object->setPublished(false);
            }
            foreach ($class->getFieldDefinitions() as $key => $field) {
                $value = $data[$mapping[$key]];
                if (array_key_exists($key, $mapping) and $value != null) {
                    // data mapping
                    $value = $field->getFromCsvImport($value, $object);
                    if ($value !== null) {
                        $object->setValue($key, $value);
                    }
                }
            }
            try {
                $object->save();
                $this->_helper->json(["success" => true]);
            } catch (\Exception $e) {
                $this->_helper->json(["success" => false, "message" => $object->getKey() . " - " . $e->getMessage()]);
            }
        }
        $this->_helper->json(["success" => $success]);
    }