Beispiel #1
0
 public function getNewItem($id, $setID = true)
 {
     list($dummy, $className, $parentID, $suffix) = explode('-', $id);
     if (!Translatable::is_default_lang()) {
         $originalItem = Translatable::get_original($className, Session::get("{$id}_originalLangID"));
         if ($setID) {
             $originalItem->ID = $id;
         } else {
             $originalItem->ID = null;
             Translatable::creating_from(Session::get($id . '_originalLangID'));
         }
         return $originalItem;
     }
     $newItem = new $className();
     if (!$suffix) {
         $sessionTag = "NewItems." . $parentID . "." . $className;
         if (Session::get($sessionTag)) {
             $suffix = '-' . Session::get($sessionTag);
             Session::set($sessionTag, Session::get($sessionTag) + 1);
         } else {
             Session::set($sessionTag, 1);
         }
         $id = $id . $suffix;
     }
     $newItem->Title = _t('CMSMain.NEW', "New ", PR_MEDIUM, '"New " followed by a className') . $className;
     $newItem->URLSegment = "new-" . strtolower($className);
     $newItem->ClassName = $className;
     $newItem->ParentID = $parentID;
     // DataObject::fieldExists only checks the current class, not the hierarchy
     // This allows the CMS to set the correct sort value
     if ($newItem->castingHelperPair('Sort')) {
         $newItem->Sort = DB::query("SELECT MAX(Sort)  FROM SiteTree WHERE ParentID = '" . Convert::raw2sql($parentID) . "'")->value() + 1;
     }
     if (Member::currentUser()) {
         $newItem->OwnerID = Member::currentUser()->ID;
     }
     if ($setID) {
         $newItem->ID = $id;
     }
     # Some modules like subsites add extra fields that need to be set when the new item is created
     $this->extend('augmentNewSiteTreeItem', $newItem);
     return $newItem;
 }
	function updateCMSFields(FieldSet &$fields) {
		if(!$this->stat('enabled', true)) return false;
		
		// add hidden fields for the used language and original record
		$fields->push(new HiddenField("Lang", "Lang", $this->getLang()) );
		$fields->push(new HiddenField("OriginalID", "OriginalID", $this->owner->OriginalID) );
		
		// if a language other than default language is used, we're in "translation mode",
		// hence have to modify the original fields
		$isTranslationMode = (Translatable::default_lang() != $this->getLang() && $this->getLang());
		if($isTranslationMode) {
			$originalLangID = Session::get($this->owner->ID . '_originalLangID');
			
			$translatableFieldNames = $this->getTranslatableFields();
			$allDataFields = $fields->dataFields();
			$transformation = new Translatable_Transformation(Translatable::get_original($this->owner->class, $this->owner->ID));
			
			// iterate through sequential list of all datafields in fieldset
			// (fields are object references, so we can replace them with the translatable CompositeField)
			foreach($allDataFields as $dataField) {
				
				if(in_array($dataField->Name(), $translatableFieldNames)) {
					//var_dump($dataField->Name());
					// if the field is translatable, perform transformation
					$fields->replaceField($dataField->Name(), $transformation->transformFormField($dataField));
				} else {
					// else field shouldn't be editable in translation-mode, make readonly
					$fields->replaceField($dataField->Name(), $dataField->performReadonlyTransformation());
				}
			}
		} else {
			// if we're not in "translation mode", show a dropdown to create a new translation.
			// this action should just be possible when showing the default language,
			// you can't create new translations from within a "translation mode" form.
			
			$alreadyTranslatedLangs = array();
			foreach ($alreadyTranslatedLangs as $i => $langCode) {
				$alreadyTranslatedLangs[$i] = i18n::get_language_name($langCode);
			}
			$fields->addFieldsToTab(
				'Root',
				new Tab(_t('Translatable.TRANSLATIONS', 'Translations'),
					new HeaderField('CreateTransHeader', _t('Translatable.CREATE', 'Create new translation'), 2),
					$langDropdown = new LanguageDropdownField("NewTransLang", _t('Translatable.NEWLANGUAGE', 'New language'), $alreadyTranslatedLangs),
					$createButton = new InlineFormAction('createtranslation',_t('Translatable.CREATEBUTTON', 'Create'))
				)
			);
			if (count($alreadyTranslatedLangs)) {
				$fields->addFieldsToTab(
					'Root.Translations',
					new FieldSet(
						new HeaderField('ExistingTransHeader', _t('Translatable.EXISTING', 'Existing translations:'), 3),
						new LiteralField('existingtrans',implode(', ',$alreadyTranslatedLangs))
					)
				);
			}
			$langDropdown->addExtraClass('languageDropdown');
			$createButton->addExtraClass('createTranslationButton');
			$createButton->includeDefaultJS(false);
		}
	}