/** * @dataProvider providerUsage * @param array $row * @param string $name * @param string $type * @param string $outputExpected * @return void */ public function testUsage($row, $name, $type, $outputExpected) { $this->field->expects($this->any())->method('isType')->will($this->returnCallback(function () use($type) { return in_array($type, func_get_args(), true); })); $this->field->expects($this->any())->method('getName')->will($this->returnValue($name)); $output = $this->csvCellContentFieldsHelper->render($this->field, $row, 0, 0); $this->assertInternalType('string', $output); $this->assertSame($outputExpected, $output); }
/** * Instantiate an Input object for the supplied field call setAllowEmpty() * depending upon whether the field is required. * * @param Field $field * @return Input */ protected function instantiateInput(Field $field) { $input = new Input($field->getControlName()); if ($field->isRequired() && !$field->isType('boolean')) { $input->setAllowEmpty(false); } else { $input->setAllowEmpty(true); } return $input; }
public function __invoke(Field $field) { if (!$field->isType('reference')) { throw new Exception('The FilterReferenceAsText template can only be used on DB reference fields.'); } $reference = $field->getOptionPairsReference(); $titleColumn = $field->getOptionPairs()->detectTitleColumn(); $field->assignHelperCallback('SelectFilter.FilterType', function () { return ['type' => 'text', 'options' => []]; })->assignHelperCallback('SelectFilter.SelectModifier', function ($helper, Select $select, $conditionSetName, $queryVars) use($reference, $titleColumn) { $filter = new TextFilter($reference['table'], $titleColumn); return $filter->apply($select, $conditionSetName, $queryVars); }); }
/** * Fetch a single row from this listing by using the supplied ID value to * match against the listing's primary key field. * * @param Fields $fields * @param mixed $id * @return \Dewdrop\Db\Row */ public function fetchRow(Fields $fields, $id) { $select = $this->getModifiedSelect($fields); $quotedPrimaryKey = $select->quoteWithAlias($this->primaryKey->getTable()->getTableName(), $this->primaryKey->getName()); $select->where("{$quotedPrimaryKey} = ?", $id); return $this->select->getAdapter()->fetchRow($select); }
/** * Populate the primary key value from the supplied row (already saved in * the RowEditor) up to the linked field in this object. For example, say * you'd configured a link like this in your RowEditor: * * <pre> * $rowEditor->linkByField('addresses', $this->orderModel->field('address_id')); * </pre> * * Then, when this method was called, you'd get a row from the addresses * table that had already been saved by the row editor. And, using that row, * this method would set the value of the orderModel's address_id field * to the primary key of the addresses row. * * @param Row $row * @return \Dewdrop\Fields\RowEditor\Link\Field */ public function populateValueFromSavedRow(Row $row) { $references = $this->field->getTable()->getMetadata('references'); foreach ($references as $foreignKey => $referencedColumnAndTable) { if ($foreignKey === $this->field->getName()) { $referencedColumn = $referencedColumnAndTable['column']; $this->field->setValue($row[$referencedColumn]); } } return $this; }
/** * Using the supplied \Dewdrop\Fields and \Dewdrop\Db\Select, modify the * Select to include only the current page with the correct number of * records. The DB driver is used to ensure we can get the total number * of records that _would_ have been returned had no pagination been applied * after the query has been executed (using whatever facility is provided * for that use in the specific RDBMS). * * @param Fields $fields * @param Select $select * @return Select * @throws Exception */ public function modifySelect(Fields $fields, Select $select) { if (!$this->isEnabled()) { return $select; } $column = $select->quoteWithAlias($this->field->getTable()->getTableName(), $this->field->getName()); $this->showingDeletedRecords = (bool) $this->request->getQuery($this->getQueryParameterName()); if ($this->isShowingDeletedRecords()) { return $select->where("{$column} = true"); } else { return $select->where("{$column} = false"); } }
/** * Assign the provided row object to the named model. This will iterate * over all the fields and call setRow() on each one associated with * the named model so that their values can be set and retrieved for * editing. * * @throws \Dewdrop\Fields\Exception * @param string $modelName * @param Row $row * @return RowEditor */ public function setRow($modelName, Row $row) { $model = $this->getModel($modelName); if ($model !== $row->getTable()) { throw new Exception('The row should be from the same table instance.'); } foreach ($this->fields as $field) { if ($field instanceof DbField && $field->getTable() === $model) { $field->setRow($row); } } if ($this->deleteField) { $this->deleteField->setRow($row); } if (array_key_exists($modelName, $this->defaultsByModelName)) { $this->applyDefaultsToRow($row, $this->defaultsByModelName[$modelName]); } $this->rowsByName[$modelName] = $row; return $this; }
/** * A fall back method for timestamp fields. Will convert the DB value to a * Unix timestamp and then format it with PHP's date() function. (How * retro!) You can customize the format with setDateFormat(). * * @param DbField $field * @param array $rowData * @return string */ protected function renderDbTimestamp(DbField $field, array $rowData) { $value = $rowData[$field->getName()]; $timestamp = strtotime($value); if ($timestamp) { return date($this->dateFormat . ' ' . $this->timeFormat, $timestamp); } else { return ''; } }
/** * Use the supplied \Dewdrop\Db\Field object to set the helper's options * and then render the input. * * @param Field $field * @param array $options * @return string */ protected function directField(Field $field, array $options = array()) { $fieldDefaults = array('name' => $field->getControlName(), 'id' => $field->getHtmlId(), 'value' => $field->getValue()); return $this->directArray($fieldDefaults + $options); }
/** * Add a field, optionally changing its control name to disambiguate it * from other fields with the same control name on this page. * * @param DbField $field * @param string $groupName * @return \Dewdrop\Fields\Edit */ public function add(DbField $field, $groupName = null) { if (null === $groupName) { $this->fields[$field->getControlName()] = $field; } else { $fieldIndex = $groupName . ':' . $field->getName(); $field->setControlName($fieldIndex); $this->fields[$fieldIndex] = $field; } $this->inputFilter->add($field->getInputFilter()); return $this; }
/** * Handle the model (\Dewdrop\Db\Table) objects for the supplied newly added * DB field. We allow custom model names for situations where you need to * add fields from two different instances of the same model (e.g. you have * an two Addresses model instances on your fields set because you have both * a billing and a shipping address). * * @param DbField $field * @param string $modelName * @throws Exception */ protected function handleModelsForDbField(DbField $field, $modelName) { $fieldTable = $field->getTable(); $groupName = $field->getGroupName(); if (null === $modelName && isset($this->modelInstances[$groupName]) && $this->modelInstances[$groupName] !== $fieldTable) { throw new Exception('When adding fields from two instances of the same model, you must specify ' . 'an alternate model name as the second paramter to add().'); } if (null === $modelName) { $modelName = $groupName; } if (isset($this->modelsByName[$modelName]) && $this->modelsByName[$modelName] !== $fieldTable) { throw new Exception("The name '{$modelName}' has already been used with another model instance. " . 'Please make sure to use model names consistently when adding fields.'); } $this->modelInstances[$groupName] = $fieldTable; $this->modelsByName[$modelName] = $fieldTable; // Update the field's control name so that generated IDs, etc., use the new name if ($modelName !== $groupName) { $field->setGroupName($modelName); } }
/** * Render the image picker using the supplied Field object. * * @param Field $field * @return string */ public function directField(Field $field) { return $this->directArray(array('name' => $field->getControlName(), 'id' => $field->getHtmlId(), 'value' => $field->getValue())); }
/** * A fall back method for timestamp fields. Will convert the DB value to a * Unix timestamp and then format it with PHP's date() function. (How * retro!) You can customize the format with setDateFormat(). * * @param FieldInterface $field * @param array $rowData * @return string */ protected function renderDbTimestamp(DbField $field, array $rowData) { $value = $rowData[$field->getName()]; $timestamp = strtotime($value); return $this->view->escapeHtml(date($this->dateFormat . ' ' . $this->timeFormat, $timestamp)); }
public function direct(Field $field, Field $cascadeFrom) { $this->view->headScript()->appendFile($this->view->bowerUrl('/dewdrop/www/js/cascade-select.js')); return $this->view->select(['name' => $field->getControlName(), 'id' => $field->getHtmlId(), 'options' => [], 'value' => $field->getValue(), 'attributes' => ['data-cascade-options' => $this->view->encodeJsonHtmlSafe($field->getOptionGroups()->fetchJsonWrapper()), 'data-cascade-from' => '#' . $cascadeFrom->getHtmlId(), 'data-cascade-title' => "Choose a {$cascadeFrom->getLabel()}...", 'data-show-blank' => true, 'data-blank-title' => '', 'data-value' => $field->getValue()]]); }
protected function handleDbReference(DbField $field, $type) { return function () use($field, $type) { return array('type' => $type, 'options' => array('options' => $field->getOptionPairs()->fetchJsonWrapper())); }; }
/** * Provide a default sorting strategy for reference columns. * * @param DbField $field * @param Select $select * @param string $direction * @return Select * @throws Select\SelectException */ public function sortDbReference(DbField $field, Select $select, $direction) { $optionPairs = $field->getOptionPairs(); $tableName = $optionPairs->getTableName(); try { $titleColumn = $optionPairs->detectTitleColumn(); } catch (TitleColumnNotDetectedException $e) { $titleColumn = $field->getName(); } if ($titleColumn instanceof Expr) { $orderSpec = "{$titleColumn} {$direction}"; } else { $orderSpec = new Expr("{$select->quoteWithAlias($tableName, $titleColumn)} {$direction}"); } return $select->order($orderSpec); }
/** * A fall back method for timestamp fields. Will convert the DB value to a * Unix timestamp and then format it with PHP's date() function. (How * retro!) You can customize the format with setDateFormat(). * * @param FieldInterface $field * @param array $rowData * @return string */ protected function renderDbTimestamp(DbField $field, array $rowData) { $value = $rowData[$field->getName()]; $timestamp = strtotime($value); // Hack for handling GMT offsets in WordPress. if ($timestamp && function_exists('get_option')) { $timezoneString = get_option('timezone_string'); if ($timezoneString) { $offset = timezone_offset_get(new DateTimeZone($timezoneString), date_create($value)); $timestamp += $offset; } } if ($timestamp) { return $this->view->escapeHtml(date($this->dateFormat . ' ' . $this->timeFormat, $timestamp)); } else { return ''; } }
/** * Use the supplied field to render the closing tags. The field's * note will be used, if it is set. * * @param Field $field * @return string */ public function closeField(Field $field) { return $this->closeArray(array('note' => $field->getNote())); }
/** * Use a \Dewdrop\Db\Field object to set the editor's id and content. * * @param Field $field * @return string */ public function directField(Field $field) { $field->getFilterChain()->attach(new StripSlashes()); return $this->directArray(array('name' => $field->getControlName(), 'value' => $field->getValue(), 'id' => $field->getHtmlId())); }