Example #1
0
    /**
     * Map Resource Data
     *
     * @return void
     */
    private function _mapResourceData()
    {
        // do we want to do a title match?
        if ($this->_options['titlematch'] == 1 && isset($this->record->type->id)) {
            $sql = 'SELECT id, title, LEVENSHTEIN( title, ' . $this->_database->quote($this->raw->title) . ' ) as titleDiff
			        FROM `#__resources`
			        WHERE `type`=' . $this->record->type->id . ' HAVING titleDiff < ' . self::TITLE_MATCH;
            $this->_database->setQuery($sql);
            $results = $this->_database->loadObjectList('id');
            // did we get more then one result?
            if (count($results) > 1) {
                $ids = implode(", ", array_keys($results));
                throw new Exception(Lang::txt('COM_RESOURCES_IMPORT_RECORD_MODEL_UNABLE_DETECTDUPLICATE', $ids));
            }
            // if we only have one were all good
            if (count($results) == 1) {
                // set our id to the matched resource
                $resource = reset($results);
                $this->raw->id = $resource->id;
                // add a notice with link to resource matched
                $resourceLink = rtrim(str_replace('administrator', '', \Request::base()), DS) . DS . 'resources' . DS . $resource->id;
                $link = '<a target="_blank" href="' . $resourceLink . '">' . $resourceLink . '</a>';
                array_push($this->record->notices, Lang::txt('COM_RESOURCES_IMPORT_RECORD_MODEL_MATCHEDBYTITLE', $link));
            }
        }
        // do we have a resource id
        // either passed in the raw data or gotten from the title match
        if (isset($this->raw->id) && $this->raw->id > 1) {
            $this->record->resource->load($this->raw->id);
        } else {
            $this->raw->standalone = 1;
            $this->raw->created = \Date::toSql();
            $this->raw->created_by = $this->_user->get('id');
            // publish up/down
            if (!isset($this->raw->publish_up)) {
                $this->raw->publish_up = \Date::toSql();
            }
            if (!isset($this->raw->publish_down)) {
                $this->raw->publish_down = '0000-00-00 00:00:00';
            }
        }
        // set modified date/user
        $this->raw->modified = \Date::toSql();
        $this->raw->modified_by = $this->_user->get('id');
        // set status
        if (isset($this->_options['status'])) {
            $this->raw->published = (int) $this->_options['status'];
        }
        // set group
        if (isset($this->_options['group'])) {
            $this->raw->group_owner = $this->_options['group'];
        }
        // set access
        if (isset($this->_options['access'])) {
            $this->raw->access = (int) $this->_options['access'];
        }
        // bind resource data
        $this->record->resource->bind($this->raw);
        // resource params
        $params = new \Hubzero\Config\Registry($this->record->resource->params);
        $this->record->resource->params = $params->toString();
        // resource attributes
        $attribs = new \Hubzero\Config\Registry($this->record->resource->attribs);
        $this->record->resource->attribs = $attribs->toString();
        // full text pieces - to add paragraph tags
        $fullTextPieces = array_map("trim", explode("\n", $this->record->resource->introtext));
        $fullTextPieces = array_values(array_filter($fullTextPieces));
        // set the full text
        $this->record->resource->fulltxt = "<p>" . implode("</p>\n<p>", $fullTextPieces) . "</p>";
        if (!isset($this->raw->custom_fields)) {
            $this->raw->custom_fields = array();
        }
        // bind custom fields to types custom fields
        if (isset($this->record->type->id)) {
            $resourcesElements = new \Components\Resources\Models\Elements((array) $this->raw->custom_fields, $this->record->type->customFields);
            $customFieldsHtml = $resourcesElements->toDatabaseHtml();
            // add all custom fields to custom object
            foreach ($resourcesElements->getSchema()->fields as $field) {
                $fieldLabel = $field->label;
                $fieldName = $field->name;
                $value = isset($this->raw->custom_fields->{$fieldName}) ? $this->raw->custom_fields->{$fieldName} : null;
                if ($field->type == 'hidden') {
                    $value = isset($field->options[0]) ? $field->options[0]->value : $value;
                }
                $this->record->custom->{$fieldLabel} = $value;
            }
        } else {
            $customFieldsHtml = '';
        }
        // add custom fields to fulltxt
        $this->record->resource->fulltxt .= "\n\n" . $customFieldsHtml;
    }