Backup\Model\Content::importDocuments PHP Method

importDocuments() protected method

Import Documents
protected importDocuments ( &$ids, &$errors, array $children ) : void
$children array Children list
return void
    protected function importDocuments(&$ids, &$errors, $children)
    {
        foreach ($children['children'] as $child) {
            $urlKey = (string) $child->url_key;
            $model = Document\Model::fromUrlKey($urlKey);
            $attributes = $child->attributes();
            $id = (int) $attributes['id'];
            if (empty($model)) {
                $model = Document\Model::fromId($id);
                if (empty($model)) {
                    $model = new Document\Model();
                }
            }
            $documentTypeId = isset($ids['document_types'][(int) $child->document_type_id]) ? $ids['document_types'][(int) $child->document_type_id] : $model->getDocumentTypeId();
            $viewId = isset($ids['views'][(int) $child->view_id]) ? $ids['views'][(int) $child->view_id] : $model->getViewId();
            $layoutId = isset($ids['layouts'][(int) $child->layout_id]) ? $ids['layouts'][(int) $child->layout_id] : $model->getLayoutId();
            $parentId = isset($ids['layouts'][(int) $child->parent_id]) ? $ids['layouts'][(int) $child->parent_id] : $model->getParentId();
            $name = (string) $child->name;
            $status = (string) $child->status;
            $userId = (int) $child->user_id;
            $sortOrder = (int) $child->sort_order;
            $showInNav = (int) $child->show_in_nav;
            $model->addData(array('name' => empty($name) ? $model->getName() : $name, 'url_key' => $urlKey, 'status' => empty($status) ? $model->getStatus() : $status, 'show_in_nav' => empty($showInNav) ? $model->getShowInNav() : $showInNav, 'sort_order' => empty($sortOrder) ? $model->getSortOrder() : $sortOrder, 'icon_id' => (int) $child->icon_id, 'view_id' => $viewId, 'parent_id' => $parentId, 'user_id' => $userId, 'layout_id' => $layoutId, 'document_type_id' => empty($documentTypeId) ? $model->getDocumentTypeId() : $documentTypeId));
            if ($model->getUserId() === null) {
                $model->setUserId($this->serviceLocator->get('Auth')->getIdentity()->getId());
            }
            try {
                if (!empty($model)) {
                    $model->save();
                    $ids['documents'][$id] = $model->getId();
                    $values = (array) $child->properties;
                    if (isset($values['property_value']) and is_array($values['property_value'])) {
                        $values = $values['property_value'];
                    }
                    foreach ($values as $value) {
                        $documentId = (int) $value->document_id;
                        $propertyId = (int) $value->property_id;
                        $valueModel = new Property\Value\Model();
                        $valueModel->load(null, isset($ids['documents'][$documentId]) ? $ids['documents'][$documentId] : $documentId, isset($ids['properties'][$propertyId]) ? $ids['properties'][$propertyId] : $propertyId);
                        $valueModel->setValue((string) base64_decode($value->value));
                        $valueModel->save();
                    }
                }
            } catch (Exception $e) {
                $errors[] = sprintf($this->serviceLocator->get('MvcTranslator')->translate('Cannot save document with id (%d)'), $id);
            }
        }
    }