/**
  * Gets the form fields as defined through the metadata
  * on {@link $obj} and the custom parameters passed to FormScaffolder.
  * Depending on those parameters, the fields can be used in ajax-context,
  * contain {@link TabSet}s etc.
  * 
  * @return FieldList
  */
 public function getFieldList()
 {
     $fields = new FieldList();
     // tabbed or untabbed
     if ($this->tabbed) {
         $fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
         $mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
     }
     //var_dump($this->obj->db());exit();
     // add database fields
     foreach ($this->obj->db() as $fieldName => $fieldType) {
         if ($this->restrictFields && !in_array($fieldName, $this->restrictFields)) {
             continue;
         }
         // @todo Pass localized title
         if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
             $fieldClass = $this->fieldClasses[$fieldName];
             $fieldObject = new $fieldClass($fieldName);
         } else {
             $fieldObject = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
         }
         $fieldObject->setTitle($this->obj->fieldLabel($fieldName));
         if ($this->tabbed) {
             $fields->addFieldToTab("Root.Main", $fieldObject);
         } else {
             $fields->push($fieldObject);
         }
     }
     return $fields;
 }
 /**
  *
  * @param \GridField $gridField
  * @param \DataObject $record
  * @param string $columnName
  * @return string|null - the HTML for the column
  */
 public function getColumnContent($gridField, $record, $columnName)
 {
     if (!$record instanceof \Payment) {
         return null;
     }
     if (!$record->canRefund()) {
         return null;
     }
     \Requirements::css('omnipay-ui/css/omnipay-ui-cms.css');
     \Requirements::javascript('omnipay-ui/javascript/omnipay-ui-cms.js');
     \Requirements::add_i18n_javascript('omnipay-ui/javascript/lang');
     $infoText = '';
     switch (GatewayInfo::refundMode($record->Gateway)) {
         case GatewayInfo::MULTIPLE:
             $infoText = 'MultiRefundInfo';
             break;
         case GatewayInfo::PARTIAL:
             $infoText = 'SingleRefundInfo';
             break;
         case GatewayInfo::FULL:
             $infoText = 'FullRefundInfo';
             break;
     }
     /** @var \Money $money */
     $money = $record->dbObject('Money');
     /** @var \GridField_FormAction $field */
     $field = \GridField_FormAction::create($gridField, 'RefundPayment' . $record->ID, false, 'refundpayment', array('RecordID' => $record->ID))->addExtraClass('gridfield-button-refund payment-dialog-button')->setAttribute('title', _t('GridFieldRefundAction.Title', 'Refund Payment'))->setAttribute('data-icon', 'button-refund')->setAttribute('data-dialog', json_encode(array('maxAmount' => $money->Nice(), 'maxAmountNum' => $money->getAmount(), 'hasAmountField' => $record->canRefund(null, true), 'infoTextKey' => $infoText, 'buttonTextKey' => 'RefundAmount')))->setDescription(_t('GridFieldRefundAction.Description', 'Refund a captured payment'));
     return $field->Field();
 }
 /**
  * @param DataObject $obj
  * @param array      $config
  * @return array|bool
  */
 public function convertDataObjectToArray(DataObject $obj, $config = array())
 {
     $content = array();
     $allowedFields = $obj instanceof FlexibleDataFormatterInterface ? $obj->getAllowedFields($config) : array_keys($obj->db());
     foreach ($allowedFields as $fieldName) {
         if ($obj->hasMethod($fieldName)) {
             $fieldValue = $obj->{$fieldName}();
         } else {
             $fieldValue = $obj->dbObject($fieldName);
             if (is_null($fieldValue)) {
                 $fieldValue = $obj->{$fieldName};
             }
         }
         if ($fieldValue instanceof Object) {
             switch (get_class($fieldValue)) {
                 case 'Boolean':
                     $content[$fieldName] = (bool) $fieldValue->getValue();
                     break;
                 case 'PrimaryKey':
                     $content[$fieldName] = $obj->{$fieldName};
                     break;
                 case 'HTMLText':
                     $content[$fieldName] = $fieldValue->forTemplate();
                     break;
                 default:
                     $content[$fieldName] = $fieldValue->getValue();
                     break;
             }
         } else {
             $content[$fieldName] = $fieldValue;
         }
     }
     if ($obj instanceof FlexibleDataFormatterInterface) {
         foreach ($obj->getAllowedHasOneRelations($config) as $relName) {
             if ($obj->{$relName . 'ID'}) {
                 $content[$relName] = $this->convertDataObjectToArray($obj->{$relName}(), $config);
             }
         }
         foreach ($obj->getAllowedHasManyRelations($config) as $relName) {
             $items = $obj->{$relName}();
             if ($items instanceof SS_List && count($items) > 0) {
                 $content[$relName] = array();
                 foreach ($items as $item) {
                     $content[$relName][] = $this->convertDataObjectToArray($item, $config);
                 }
             }
         }
         foreach ($obj->getAllowedManyManyRelations($config) as $relName) {
             $items = $obj->{$relName}();
             if ($items instanceof SS_List && count($items) > 0) {
                 $content[$relName] = array();
                 foreach ($items as $item) {
                     $content[$relName][] = $this->convertDataObjectToArray($item, $config);
                 }
             }
         }
     }
     return $content;
 }
 /**
  *
  * @param \GridField $gridField
  * @param \DataObject $record
  * @param string $columnName
  * @return string|null - the HTML for the column
  */
 public function getColumnContent($gridField, $record, $columnName)
 {
     if (!$record instanceof \Payment) {
         return null;
     }
     if (!$record->canVoid()) {
         return null;
     }
     \Requirements::css('omnipay-ui/css/omnipay-ui-cms.css');
     \Requirements::javascript('omnipay-ui/javascript/omnipay-ui-cms.js');
     \Requirements::add_i18n_javascript('omnipay-ui/javascript/lang');
     /** @var \GridField_FormAction $field */
     $field = \GridField_FormAction::create($gridField, 'VoidPayment' . $record->ID, false, 'voidpayment', array('RecordID' => $record->ID))->addExtraClass('gridfield-button-void payment-dialog-button')->setAttribute('title', _t('GridFieldVoidAction.Title', 'Void Payment'))->setAttribute('data-icon', 'button-void')->setAttribute('data-dialog', json_encode(array('maxAmount' => $record->dbObject('Money')->Nice(), 'hasAmountField' => false, 'infoTextKey' => 'VoidInfo', 'buttonTextKey' => 'VoidPayment')))->setDescription(_t('GridFieldVoidAction.Description', 'Void an authorized payment'));
     return $field->Field();
 }
	/**
	 * Gets the form fields as defined through the metadata
	 * on {@link $obj} and the custom parameters passed to FormScaffolder.
	 * Depending on those parameters, the fields can be used in ajax-context,
	 * contain {@link TabSet}s etc.
	 * 
	 * @return FieldSet
	 */
	public function getFieldSet() {
		$fields = new FieldSet();
		
		// tabbed or untabbed
		if($this->tabbed) {
			$fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
			$mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
		}
		
		// add database fields
		foreach($this->obj->db() as $fieldName => $fieldType) {
			if($this->restrictFields && !in_array($fieldName, $this->restrictFields)) continue;
			
			// @todo Pass localized title
			if($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
				$fieldClass = $this->fieldClasses[$fieldName];
				$fieldObject = new $fieldClass($fieldName);
			} else {
				$fieldObject = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
			}
			$fieldObject->setTitle($this->obj->fieldLabel($fieldName));
			if($this->tabbed) {
				$fields->addFieldToTab("Root.Main", $fieldObject);
			} else {
				$fields->push($fieldObject);
			}
		}
		
		// add has_one relation fields
		if($this->obj->has_one()) {
			foreach($this->obj->has_one() as $relationship => $component) {
				if($this->restrictFields && !in_array($relationship, $this->restrictFields)) continue;
				$hasOneField = $this->obj->dbObject("{$relationship}ID")->scaffoldFormField(null, $this->getParamsArray());
				$hasOneField->setTitle($this->obj->fieldLabel($relationship));
				if($this->tabbed) {
					$fields->addFieldToTab("Root.Main", $hasOneField);
				} else {
					$fields->push($hasOneField);
				}
			}
		}
		
		// only add relational fields if an ID is present
		if($this->obj->ID) {
			// add has_many relation fields
			if($this->obj->has_many() && ($this->includeRelations === true || isset($this->includeRelations['has_many']))) {
				foreach($this->obj->has_many() as $relationship => $component) {
					if($this->tabbed) {
						$relationTab = $fields->findOrMakeTab(
							"Root.$relationship", 
							$this->obj->fieldLabel($relationship)
						);
					}
					$relationshipFields = singleton($component)->summaryFields();
					$foreignKey = $this->obj->getComponentJoinField($relationship);
					$ctf = new ComplexTableField(
						$this,
						$relationship,
						$component,
						$relationshipFields,
						"getCMSFields", 
						"$foreignKey = " . $this->obj->ID
					);
					$ctf->setPermissions(TableListField::permissions_for_object($component));
					if($this->tabbed) {
						$fields->addFieldToTab("Root.$relationship", $ctf);
					} else {
						$fields->push($ctf);
					}
				}
			}

			if($this->obj->many_many() && ($this->includeRelations === true || isset($this->includeRelations['many_many']))) {
				foreach($this->obj->many_many() as $relationship => $component) {
					if($this->tabbed) {
						$relationTab = $fields->findOrMakeTab(
							"Root.$relationship", 
							$this->obj->fieldLabel($relationship)
						);
					}

					$relationshipFields = singleton($component)->summaryFields();
					$filterWhere = $this->obj->getManyManyFilter($relationship, $component);
					$filterJoin = $this->obj->getManyManyJoin($relationship, $component);
					$ctf =  new ComplexTableField(
						$this,
						$relationship,
						$component,
						$relationshipFields,
						"getCMSFields", 
						$filterWhere,
						'', 
						$filterJoin
					);
					$ctf->setPermissions(TableListField::permissions_for_object($component));
					$ctf->popupClass = "ScaffoldingComplexTableField_Popup";
					if($this->tabbed) {
						$fields->addFieldToTab("Root.$relationship", $ctf);
					} else {
						$fields->push($ctf);
					}
				}
			}
		}
		
		return $fields;
	}
Example #6
0
 /**
  * Gets the form fields as defined through the metadata
  * on {@link $obj} and the custom parameters passed to FormScaffolder.
  * Depending on those parameters, the fields can be used in ajax-context,
  * contain {@link TabSet}s etc.
  *
  * @return FieldList
  */
 public function getFieldList()
 {
     $fields = new FieldList();
     // tabbed or untabbed
     if ($this->tabbed) {
         $fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
         $mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
     }
     // add database fields
     foreach ($this->obj->db() as $fieldName => $fieldType) {
         if ($this->restrictFields && !in_array($fieldName, $this->restrictFields)) {
             continue;
         }
         // @todo Pass localized title
         if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
             $fieldClass = $this->fieldClasses[$fieldName];
             $fieldObject = new $fieldClass($fieldName);
         } else {
             $fieldObject = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
         }
         $fieldObject->setTitle($this->obj->fieldLabel($fieldName));
         if ($this->tabbed) {
             $fields->addFieldToTab("Root.Main", $fieldObject);
         } else {
             $fields->push($fieldObject);
         }
     }
     // add has_one relation fields
     if ($this->obj->hasOne()) {
         foreach ($this->obj->hasOne() as $relationship => $component) {
             if ($this->restrictFields && !in_array($relationship, $this->restrictFields)) {
                 continue;
             }
             $fieldName = $component === 'DataObject' ? $relationship : "{$relationship}ID";
             if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
                 $fieldClass = $this->fieldClasses[$fieldName];
                 $hasOneField = new $fieldClass($fieldName);
             } else {
                 $hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
             }
             if (empty($hasOneField)) {
                 continue;
             }
             // Allow fields to opt out of scaffolding
             $hasOneField->setTitle($this->obj->fieldLabel($relationship));
             if ($this->tabbed) {
                 $fields->addFieldToTab("Root.Main", $hasOneField);
             } else {
                 $fields->push($hasOneField);
             }
         }
     }
     // only add relational fields if an ID is present
     if ($this->obj->ID) {
         // add has_many relation fields
         if ($this->obj->hasMany() && ($this->includeRelations === true || isset($this->includeRelations['has_many']))) {
             foreach ($this->obj->hasMany() as $relationship => $component) {
                 if ($this->tabbed) {
                     $relationTab = $fields->findOrMakeTab("Root.{$relationship}", $this->obj->fieldLabel($relationship));
                 }
                 $fieldClass = isset($this->fieldClasses[$relationship]) ? $this->fieldClasses[$relationship] : 'GridField';
                 $grid = Object::create($fieldClass, $relationship, $this->obj->fieldLabel($relationship), $this->obj->{$relationship}(), GridFieldConfig_RelationEditor::create());
                 if ($this->tabbed) {
                     $fields->addFieldToTab("Root.{$relationship}", $grid);
                 } else {
                     $fields->push($grid);
                 }
             }
         }
         if ($this->obj->manyMany() && ($this->includeRelations === true || isset($this->includeRelations['many_many']))) {
             foreach ($this->obj->manyMany() as $relationship => $component) {
                 if ($this->tabbed) {
                     $relationTab = $fields->findOrMakeTab("Root.{$relationship}", $this->obj->fieldLabel($relationship));
                 }
                 $fieldClass = isset($this->fieldClasses[$relationship]) ? $this->fieldClasses[$relationship] : 'GridField';
                 $grid = Object::create($fieldClass, $relationship, $this->obj->fieldLabel($relationship), $this->obj->{$relationship}(), GridFieldConfig_RelationEditor::create());
                 if ($this->tabbed) {
                     $fields->addFieldToTab("Root.{$relationship}", $grid);
                 } else {
                     $fields->push($grid);
                 }
             }
         }
     }
     return $fields;
 }
 /**
  * @param DataObject $rec           This would normally just be a singleton but we don't want to have to create it over and over
  * @param string     $filterField
  * @param mixed      $filterVal
  * @return array - returns the new filter added
  */
 public function processFilterField($rec, $filterField, $filterVal)
 {
     // First check for VFI fields
     if ($rec->hasExtension('VirtualFieldIndex') && ($spec = $rec->getVFISpec($filterField))) {
         if ($spec['Type'] == VirtualFieldIndex::TYPE_LIST) {
             // Lists have to be handled a little differently
             $f = $rec->getVFIFieldName($filterField) . ':PartialMatch';
             if (is_array($filterVal)) {
                 foreach ($filterVal as &$val) {
                     $val = '|' . $val . '|';
                 }
                 return array($f => $filterVal);
             } else {
                 return array($f => '|' . $filterVal . '|');
             }
         } else {
             // Simples are simple
             $filterField = $rec->getVFIFieldName($filterField);
         }
     }
     // Next check for regular db fields
     if ($rec->dbObject($filterField)) {
         // Is it a range value?
         if (preg_match('/^RANGE\\~(.+)\\~(.+)$/', $filterVal, $m)) {
             $filterField .= ':Between';
             $filterVal = array_slice($m, 1, 2);
         }
         return array($filterField => $filterVal);
     }
     return array();
 }