Exemple #1
0
	function testInvert() {
		$arr = array(
			'row1' => array(
				'col1' =>'val1',
				'col2' => 'val2'
			),
			'row2' => array(
				'col1' => 'val3',
				'col2' => 'val4'
			)
		);
		
		$this->assertEquals(
			ArrayLib::invert($arr),
			array(
				'col1' => array(
					'row1' => 'val1',
					'row2' => 'val3',
				),
				'col2' => array(
					'row1' => 'val2',
					'row2' => 'val4',
				),
			)
		);
	}
Exemple #2
0
	/** 
	 * Saves the Dataobjects contained in the field
	 */
	function saveInto(DataObjectInterface $record) {
		// CMS sometimes tries to set the value to one.
		if(is_array($this->value)){
			$newFields = array();
			
			// Sort into proper array
			$value = ArrayLib::invert($this->value);
			$dataObjects = $this->sortData($value, $record->ID);
			
			// New fields are nested in their own sub-array, and need to be sorted separately
 			if(isset($dataObjects['new']) && $dataObjects['new']) {
 				$newFields = $this->sortData($dataObjects['new'], $record->ID);
 			}

			// Update existing fields
			// @todo Should this be in an else{} statement?
			$savedObjIds = $this->saveData($dataObjects, $this->editExisting);
			
			// Save newly added record
			if($savedObjIds || $newFields) {
				$savedObjIds = $this->saveData($newFields,false);
 			}

			// Add the new records to the DataList
			if($savedObjIds) foreach($savedObjIds as $id => $status) {
				$this->getDataList()->add($id);
			}	

			// Update the internal source items cache
			$this->value = null;
			$items = $this->sourceItems();
			
			// FormResponse::update_dom_id($this->id(), $this->FieldHolder());
		}
	}
 /** 
  * Saves the Dataobjects contained in the field
  */
 function saveInto(DataObject $record)
 {
     // CMS sometimes tries to set the value to one.
     if (is_array($this->value)) {
         $newFields = array();
         // Sort into proper array
         $value = ArrayLib::invert($this->value);
         $dataObjects = $this->sortData($value, $record->ID);
         // New fields are nested in their own sub-array, and need to be sorted separately
         if (isset($dataObjects['new']) && $dataObjects['new']) {
             $newFields = $this->sortData($dataObjects['new'], $record->ID);
         }
         // Update existing fields
         // @todo Should this be in an else{} statement?
         $savedObjIds = $this->saveData($dataObjects, $this->editExisting);
         // Save newly added record
         if ($savedObjIds || $newFields) {
             $savedObjIds = $this->saveData($newFields, false);
         }
         // Optionally save the newly created records into a relationship
         // on $record. This assumes the name of this formfield instance
         // is set to a relationship name on $record.
         if ($this->relationAutoSetting) {
             $relationName = $this->Name();
             if ($record->has_many($relationName) || $record->many_many($relationName)) {
                 if ($savedObjIds) {
                     foreach ($savedObjIds as $id => $status) {
                         $record->{$relationName}()->add($id);
                     }
                 }
             }
         }
         // Update the internal source items cache
         $this->value = null;
         $items = $this->sourceItems();
         FormResponse::update_dom_id($this->id(), $this->FieldHolder());
     }
 }
	/** 
	 * Saves the Dataobjects contained in the field
	 */
	function saveInto(DataObject $record) {
		// CMS sometimes tries to set the value to one.
		if(is_array($this->value)){
			
			// Sort into proper array
			$this->value = ArrayLib::invert($this->value);
			$dataObjects = $this->sortData($this->value, $record->ID);
			if(isset($dataObjects['new']) && $dataObjects['new']) {
				$newFields = $this->sortData($dataObjects['new'], $record->ID);
			}

			$savedObj = $this->saveData($dataObjects, $this->editExisting);
			if($savedObj && isset($newFields)) {
				$savedObj = $this->saveData($newFields,false);
			} else if(isset($newFields)) {
				$savedObj = $this->saveData($newFields,false);
			}
			$items = $this->sourceItems();
			FormResponse::update_dom_id($this->id(), $this->FieldHolder());
		}
	}
 function saveInto(DataObject $record)
 {
     $fieldname = $this->name;
     $value = ArrayLib::invert($this->value);
     if ($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
         $idList = array();
         if ($value) {
             foreach ($value['Value'] as $id => $bool) {
                 if ($bool) {
                     $idList[] = $id;
                 }
             }
         }
         $record->{$fieldname}()->setByIDList($idList);
     } elseif ($fieldname && $record) {
         if ($value) {
             if (is_array($value)) {
                 foreach ($value as $k => $items) {
                     if (is_array($items)) {
                         foreach ($items as $key => $val) {
                             if (!$val) {
                                 unset($value[$k][$key]);
                             } else {
                                 if ($k == 'Value') {
                                     $value[$k][$key] = str_replace(", ", "{comma}", $val);
                                 }
                             }
                         }
                     }
                 }
             }
             foreach ($value as $k => $v) {
                 if ($k == 'Value') {
                     $record->{$fieldname} = implode(",", $v);
                 } else {
                     $record->{$k} = Convert::array2json($v);
                 }
             }
         } else {
             $record->{$fieldname} = '';
         }
     }
 }