/** * Save Map Field * * @param SimpleMap_MapFieldType $fieldType * @return bool */ public function saveField(SimpleMap_MapFieldType $fieldType) { $owner = $fieldType->element; $field = $fieldType->model; $content = $fieldType->element->getContent(); $handle = $field->handle; $data = $content->getAttribute($handle); if (!$data) { return false; } $data['lat'] = (double) $data['lat']; $data['lng'] = (double) $data['lng']; $record = SimpleMap_MapRecord::model()->findByAttributes(array('ownerId' => $owner->id, 'fieldId' => $field->id, 'ownerLocale' => $owner->locale)); if (!$record) { $record = new SimpleMap_MapRecord(); $record->ownerId = $owner->id; $record->fieldId = $field->id; $record->ownerLocale = $owner->locale; } $record->setAttributes($data, false); $save = $record->save(); if (!$save) { SimpleMapPlugin::log(print_r($record->getErrors(), true), LogLevel::Error); } return $save; }
private function _transferData(FieldModel $field, $tableName, $fieldHandle = "") { if (!$fieldHandle) { $fieldHandle = $field->handle; } $tableData = craft()->db->createCommand()->select('elementId, locale, field_' . $fieldHandle)->from($tableName)->where('field_' . $fieldHandle . ' IS NOT NULL')->queryAll(); foreach ($tableData as $row) { $record = new SimpleMap_MapRecord(); $record->ownerId = $row['elementId']; $record->fieldId = $field->id; $record->ownerLocale = $row['locale']; $f = json_decode($row['field_' . $fieldHandle], true); $record->setAttribute('lat', $f['lat']); $record->setAttribute('lng', $f['lng']); $record->setAttribute('zoom', $f['zoom']); $record->setAttribute('address', $f['address']); if ($f !== null && array_key_exists('parts', $f)) { $record->setAttribute('parts', json_encode($f['parts'])); } $record->save(); } }