예제 #1
0
 public function testLoadDataFromObject()
 {
     $article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
     $articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
     $tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
     $tag2 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
     $field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
     $form = new Form(new Controller(), 'Form', new FieldList($field), new FieldList());
     $form->loadDataFrom($articleWithTags);
     $this->assertEquals(array($tag1->ID => $tag1->ID, $tag2->ID => $tag2->ID), $field->Value(), 'CheckboxSetField loads data from a manymany relationship in an object through Form->loadDataFrom()');
 }
예제 #2
0
 /**
  * Allows you to set groups based on a CheckboxSetField
  *
  * Pass the form element from your post data directly to this method, and
  * it will update the groups and add and remove the member as appropriate.
  *
  * On the form setup:
  *
  * <code>
  * $fields->push(
  *   new CheckboxSetField(
  *     "NewsletterSubscriptions",
  *     "Receive email notification of events in ",
  *     $sourceitems = DataObject::get("NewsletterType")->toDropDownMap("GroupID","Title"),
  *     $selectedgroups = $member->Groups()->Map("ID","ID")
  *   )
  * );
  * </code>
  *
  * On the form handler:
  *
  * <code>
  * $groups = $member->Groups();
  * $checkboxfield = $form->Fields()->fieldByName("NewsletterSubscriptions");
  * $groups->setByCheckboxSetField($checkboxfield);
  * </code>
  *
  * @param CheckboxSetField $checkboxsetfield The CheckboxSetField (with
  *                                           data) from your form.
  */
 function setByCheckboxSetField(CheckboxSetField $checkboxsetfield)
 {
     // Get the values from the formfield.
     $values = $checkboxsetfield->Value();
     $sourceItems = $checkboxsetfield->getSource();
     if ($sourceItems) {
         // If (some) values are present, add and remove as necessary.
         if ($values) {
             // update the groups based on the selections
             foreach ($sourceItems as $k => $item) {
                 if (in_array($k, $values)) {
                     $add[] = $k;
                 } else {
                     $remove[] = $k;
                 }
             }
             // else we should be removing all from the necessary groups.
         } else {
             $remove = array_keys($sourceItems);
         }
         if ($add) {
             $this->addManyByGroupID($add);
         }
         if ($remove) {
             $this->RemoveManyByGroupID($remove);
         }
     } else {
         USER_ERROR("Member::setByCheckboxSetField() - No source items could be found for checkboxsetfield " . $checkboxsetfield->Name(), E_USER_WARNING);
     }
 }