public function testCanInsertAFieldIntoAPositionImmediatelyAfterAnExistingField() { // Insert a field after the first field $this->assertInstanceOf('Dewdrop\\Fields\\FieldInterface', $this->fields->insertAfter('second', 'visible')); $this->assertCount(5, $this->fields); $this->assertTrue($this->fields->has('visible', $visiblePosition)); $this->assertSame(0, $visiblePosition); $this->assertTrue($this->fields->has('second', $secondPosition)); $this->assertSame(1, $secondPosition); $this->assertTrue($this->fields->has('sortable', $sortablePosition)); $this->assertSame(2, $sortablePosition); $this->assertTrue($this->fields->has('editable', $editablePosition)); $this->assertSame(3, $editablePosition); $this->assertTrue($this->fields->has('filterable', $filterablePosition)); $this->assertSame(4, $filterablePosition); // Insert a field after some field in the middle $this->assertInstanceOf('Dewdrop\\Fields\\FieldInterface', $this->fields->insertAfter('fourth', 'sortable')); $this->assertCount(6, $this->fields); $this->assertTrue($this->fields->has('visible', $visiblePosition)); $this->assertSame(0, $visiblePosition); $this->assertTrue($this->fields->has('second', $secondPosition)); $this->assertSame(1, $secondPosition); $this->assertTrue($this->fields->has('sortable', $sortablePosition)); $this->assertSame(2, $sortablePosition); $this->assertTrue($this->fields->has('fourth', $sortablePosition)); $this->assertSame(3, $sortablePosition); $this->assertTrue($this->fields->has('editable', $editablePosition)); $this->assertSame(4, $editablePosition); $this->assertTrue($this->fields->has('filterable', $filterablePosition)); $this->assertSame(5, $filterablePosition); // Insert a field after the last field $this->assertInstanceOf('Dewdrop\\Fields\\FieldInterface', $this->fields->insertAfter('last', 'filterable')); $this->assertCount(7, $this->fields); $this->assertTrue($this->fields->has('visible', $visiblePosition)); $this->assertSame(0, $visiblePosition); $this->assertTrue($this->fields->has('second', $secondPosition)); $this->assertSame(1, $secondPosition); $this->assertTrue($this->fields->has('sortable', $sortablePosition)); $this->assertSame(2, $sortablePosition); $this->assertTrue($this->fields->has('fourth', $sortablePosition)); $this->assertSame(3, $sortablePosition); $this->assertTrue($this->fields->has('editable', $editablePosition)); $this->assertSame(4, $editablePosition); $this->assertTrue($this->fields->has('filterable', $filterablePosition)); $this->assertSame(5, $filterablePosition); $this->assertTrue($this->fields->has('last', $filterablePosition)); $this->assertSame(6, $filterablePosition); }
public function testCanRemoveAFieldUsingUnset() { $this->assertTrue($this->fields->has('visible')); unset($this->fields['visible']); $this->assertFalse($this->fields->has('visible')); }
public function testAddingFieldToAdditionalFieldsetDoesNotOverrideOriginalSet() { $orig = new Fields(); $orig->add($this->field); $second = new Fields(); $second->add($this->field); $this->field->add('test'); $this->assertTrue($orig->has('test')); $this->assertEquals(2, count($orig)); }
/** * Save the user's visible field selections back to the database. We expect * the selections array to just contain field IDs. Only those that match * IDs of fields in the supplied Fields object will be saved. * * @param Fields $fields * @param array $selections * @param boolean $applyToAllUsers * @return void */ public function save(Fields $fields, array $selections, $applyToAllUsers = false) { $visibleFields = []; foreach ($selections as $id) { if ($fields->has($id)) { $visibleFields[] = $id; } } if (count($visibleFields)) { $this->dbAdapter->beginTransaction(); $this->deleteExistingValuesForSave($applyToAllUsers); foreach ($visibleFields as $visibleFieldId) { $data = ['component' => $this->componentName, 'field_id' => $visibleFieldId]; if (!$applyToAllUsers) { foreach ($this->getUserReferenceValues() as $column => $value) { $data[$column] = $value; } } $this->dbAdapter->insert($this->dbTableName, $data); } $this->dbAdapter->commit(); // Reset selections so they'll be loaded again when filter is re-applied $this->selections = null; } }
/** * Apply the filter to the supplied set of fields. In return, you'll end * up with a \Dewdrop\Fields\GroupedFields object that reflects the sort * order and grouping preferences contained in this filter. You can use * that object either as a normal \Dewdrop\Fields object or you can call * getGroups() on it to get the fields back in their assigned groups. * * @param Fields $currentFields * @return GroupedFields */ public function apply(Fields $currentFields) { if (!$this->loadedDbData) { $this->loadedDbData = $this->load(); } $groupedFields = new GroupedFields([], $currentFields->getUser()); foreach ($this->loadedDbData as $fieldConfig) { // Ungrouped fields come after grouped fields if (!$fieldConfig['group_id']) { continue; } $fieldId = $fieldConfig['field_id']; $groupId = $fieldConfig['group_id']; if ($currentFields->has($fieldId)) { if (!$groupedFields->hasGroup($groupId)) { $group = $groupedFields->addGroup($groupId); $group->setTitle($fieldConfig['group_title']); } $groupedFields->getGroup($groupId)->add($currentFields->get($fieldId)); } } $ungrouped = $groupedFields->addGroup('ungrouped'); $ungrouped->setTitle('Other'); foreach ($this->loadedDbData as $fieldConfig) { if (!$fieldConfig['group_id']) { $id = $fieldConfig['field_id']; if ($currentFields->has($id)) { $ungrouped->add($currentFields->get($id)); } } } foreach ($currentFields as $field) { if (!$groupedFields->has($field->getId())) { $ungrouped->add($field); } } return $groupedFields; }