Admin_ThemesController::articleTypesEditAction PHP Метод

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

    public function articleTypesEditAction()
    {
        $translator = \Zend_Registry::get('container')->getService('translator');
        $thmServ = $this->getThemeService();
        // getting the theme entity
        $themeId = $this->_request->getParam('id');
        $theme = $thmServ->findById($themeId);
        $updateArticleTypes = array();
        // for xml updating
        $createArticleTypes = array();
        // for db updating
        // process the request matching
        $articleTypeIgnore = $this->_request->getPost('articleTypeIgnore');
        $articleTypeCreate = $this->_request->getPost('articleTypeCreate');
        $articleTypeFieldIgnore = $this->_request->getPost('articleTypeFieldIgnore');
        $articleTypeFieldCreate = $this->_request->getPost('articleTypeFieldCreate');
        $articleTypes = $this->_request->getPost('articleTypes');
        $articleTypeFields = $this->_request->getPost('articleTypeFields');
        $themeArticleTypeFields = $this->_request->getPost('themeArticleTypeFields', array());
        $themeArticleTypes = $this->_request->getPost('themeArticleTypes');
        // complex logic for matching
        foreach ($themeArticleTypeFields as $typeName => $fields) {
            $updateArticleTypes[$typeName] = array('name' => '', 'ignore' => false, 'fields' => array());
            if (intval($articleTypeIgnore[$typeName]) == 0) {
                // replace type with new one
                if (isset($articleTypes[$typeName])) {
                    // type from db system to xml
                    $updateArticleTypes[$typeName]['name'] = $articleTypes[$typeName];
                }
                if (intval($articleTypeCreate[$typeName]) == 1) {
                    // create article type in the system
                    $createArticleTypes[$typeName]['name'] = $updateArticleTypes[$typeName]['name'] = $typeName;
                }
            } else {
                // leave it as it is
                $updateArticleTypes[$typeName] = array('name' => $typeName, 'ignore' => true, 'fields' => array());
            }
            foreach ($fields as $fieldName) {
                // process fields, same as above
                // need to pass article type value for matching with the system
                if (intval($articleTypeFieldIgnore[$typeName][$fieldName]) == 0) {
                    if (isset($articleTypeFields[$typeName][$fieldName])) {
                        $updateArticleTypes[$typeName]['fields'][$fieldName] = array('name' => $articleTypeFields[$typeName][$fieldName], 'parentType' => $articleTypes[$typeName], 'ignore' => false);
                    }
                    if (intval($articleTypeFieldCreate[$typeName][$fieldName]) == 1) {
                        $updateArticleTypes[$typeName]['fields'][$fieldName] = $createArticleTypes[$typeName]['fields'][] = array('name' => $fieldName, 'parentType' => $articleTypes[$typeName], 'ignore' => false);
                    }
                } else {
                    $updateArticleTypes[$typeName]['fields'][$fieldName] = array('name' => $fieldName, 'parentType' => $articleTypes[$typeName], 'ignore' => true);
                }
            }
        }
        $artServ = $this->getArticleTypeService();
        $themeArticleTypes = (array) $this->getThemeService()->getArticleTypes($theme);
        foreach ($createArticleTypes as $typeName => $type) {
            // TODO pass if not found in xml?
            if (!isset($themeArticleTypes[$typeName])) {
                unset($createArticleTypes[$typeName]);
                continue;
            }
            if (isset($type['fields']) && is_array($type['fields'])) {
                foreach ($type['fields'] as $k => $field) {
                    $createArticleTypes[$typeName]['fields'][$k]['props'] = (array) $themeArticleTypes[$typeName]->{$field['name']};
                }
            }
        }
        $artServ->createMany($createArticleTypes);
        $this->view->response = $thmServ->assignArticleTypes($updateArticleTypes, $theme);
        $this->_helper->flashMessenger($translator->trans('Theme settings updated.', array(), 'themes'));
    }