/** * Copies the new values of any non-translatable fields across the element's * other locales. * * @param BaseElementModel $element * @param ContentModel $content * @param FieldLayoutModel $fieldLayout * @param array &$nonTranslatableFields * @param array &$otherContentModels * * @return null */ private function _duplicateNonTranslatableFieldValues(BaseElementModel $element, ContentModel $content, FieldLayoutModel $fieldLayout, &$nonTranslatableFields, &$otherContentModels) { // Get all of the non-translatable fields $nonTranslatableFields = array(); foreach ($fieldLayout->getFields() as $fieldLayoutField) { $field = $fieldLayoutField->getField(); if ($field && !$field->translatable) { $fieldType = $field->getFieldType(); if ($fieldType && $fieldType->defineContentAttribute()) { $nonTranslatableFields[$field->id] = $field; } } } if ($nonTranslatableFields) { // Get the other locales' content $rows = craft()->db->createCommand()->from($this->contentTable)->where(array('and', 'elementId = :elementId', 'locale != :locale'), array(':elementId' => $element->id, ':locale' => $content->locale))->queryAll(); // Remove the column prefixes foreach ($rows as $i => $row) { $rows[$i] = $this->_removeColumnPrefixesFromRow($row); } $otherContentModels = ContentModel::populateModels($rows); foreach ($otherContentModels as $otherContentModel) { foreach ($nonTranslatableFields as $field) { $handle = $field->handle; $otherContentModel->{$handle} = $content->{$handle}; } $this->_saveContentRow($otherContentModel); } } }
/** * Performs post-save element operations, such as calling all fieldtypes' onAfterElementSave() methods. * * @param BaseElementModel $element * @param ContentModel $content */ public function postSaveOperations(BaseElementModel $element, ContentModel $content) { // Get all of the fieldtypes $fields = craft()->fields->getAllFields(); $fieldTypes = array(); $fieldTypesWithDuplicateContent = array(); foreach ($fields as $field) { $fieldType = craft()->fields->populateFieldType($field); if ($fieldType) { $fieldType->element = $element; $fieldTypes[] = $fieldType; if (!$field->translatable && $fieldType->defineContentAttribute()) { $fieldTypesWithDuplicateContent[] = $fieldType; } } } // Are we dealing with other locales as well? if (Craft::hasPackage(CraftPackage::Localize)) { // Get the other locales' content $rows = craft()->db->createCommand()->from('content')->where(array('and', 'elementId = :elementId', 'locale != :locale'), array(':elementId' => $element->id, ':locale' => $content->locale))->queryAll(); $otherContentModels = ContentModel::populateModels($rows); if ($otherContentModels) { foreach ($fieldTypesWithDuplicateContent as $fieldType) { $handle = $fieldType->model->handle; // Copy the content over! foreach ($otherContentModels as $otherContentModel) { $otherContentModel->{$handle} = $content->{$handle}; } } foreach ($otherContentModels as $otherContentModel) { $this->saveContent($otherContentModel, false); } } } else { $otherContentModels = null; } // Now that all of the content saved for all locales, // call all fieldtypes' onAfterElementSave() functions foreach ($fieldTypes as $fieldType) { $fieldType->onAfterElementSave(); } // Update the search keyword indexes $searchKeywordsByLocale = array(); foreach ($fieldTypes as $fieldType) { $field = $fieldType->model; $handle = $field->handle; // Set the keywords for the content's locale $fieldSearchKeywords = $fieldType->getSearchKeywords($element->{$handle}); $searchKeywordsByLocale[$content->locale][$field->id] = $fieldSearchKeywords; // Should we queue up the other locales' new keywords too? if ($otherContentModels && in_array($fieldType, $fieldTypesWithDuplicateContent)) { foreach ($otherContentModels as $otherContentModel) { $searchKeywordsByLocale[$otherContentModel->locale][$field->id] = $fieldSearchKeywords; } } } foreach ($searchKeywordsByLocale as $localeId => $keywords) { craft()->search->indexElementFields($element->id, $localeId, $keywords); } }