Ejemplo n.º 1
0
 /**
  * Basic tests for the FormAction builder class.
  *
  * @throws \Exception
  */
 public function testBuilder()
 {
     $type = 't' . (string) rand();
     $label = 'l' . (string) rand();
     $target = 't' . (string) rand();
     $formAction = FormActionBuilder::begin()->setType($type)->setLabel($label)->setTarget($target)->build();
     $this->assertEquals($type, $formAction->getType());
     $this->assertEquals($label, $formAction->getLabel());
     $this->assertEquals($target, $formAction->getTarget());
 }
Ejemplo n.º 2
0
 /**
  * @return WritableInterface
  * @throws \Exception If object id not provided, or object not found.
  */
 protected function makeDetail()
 {
     /** @var boolean $idWasProvided */
     $idWasProvided = static::getObjectId() !== null;
     /** @var ObjectWrapperInterface $object */
     $object = $this->getObjectOr404(true);
     /** @var string $className */
     $className = $this->getQuery()->getTitleCasedObjectName();
     /** @var string $tableName */
     $tableName = StringUtils::slugify($className);
     /** @var FormActionInterface $submitAction */
     $submitAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Save')->addClass('save')->setTarget("\n                athens.ajax.submitForm(\$(this).closest('form')[0], function(){\n                        athens.multi_panel.closePanel(1);\n                        athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n                    });\n                return false;\n            ")->build();
     /** @var string $deleteUrl */
     $deleteUrl = static::makeUrl([static::MODE_FIELD => static::MODE_DELETE, static::QUERY_INDEX_FIELD => $this->getQueryIndex(), static::OBJECT_ID_FIELD => $object->getPrimaryKey()]);
     /** @var FormActionInterface $deleteAction */
     $deleteAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Delete')->setTarget("\n                if (confirm('Are you sure you want to delete this object?')) {\n                    athens.alert.makeAlert('Deleting object.', 'info');\n                    athens.ajax.post(\n                        '{$deleteUrl}',\n                        {},\n                        function() {},\n                        function() {\n                            athens.alert.makeAlert('Object deleted', 'success');\n                            athens.multi_panel.closePanel(1);\n                            athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n                        }\n                    );\n                }\n                return false;\n            ")->build();
     $formBuilder = FormBuilder::begin()->setId('object-manager-detail-form')->addObject($object)->removeWritable($object->getPascalCasedObjectName() . '.Id')->addAction($submitAction);
     if ($idWasProvided === true) {
         $formBuilder->addAction($deleteAction);
     }
     $sectionBuilder = SectionBuilder::begin()->setId('object-manager-detail-form-wrapper')->addLabel($className)->addWritable($formBuilder->build());
     return $sectionBuilder->build();
 }
Ejemplo n.º 3
0
 /**
  * @return void
  */
 protected function validateActions()
 {
     if ($this->actions === []) {
         $this->actions = [FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_SUBMIT)->setLabel('Submit')->build()];
     }
 }
Ejemplo n.º 4
0
 public function testVisitFormAction()
 {
     $writer = new HTMLWriter();
     $label = 'l' . (string) rand();
     $target = 't' . (string) rand();
     $jsFormAction = FormActionBuilder::begin()->setType(FormAction::TYPE_JAVASCRIPT)->setLabel($label)->setTarget($target)->build();
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitFormAction($jsFormAction));
     $this->assertContains("onclick={$target}", $result);
     $this->assertContains(">{$label}</button>", $result);
     $submitAction = FormActionBuilder::begin()->setType(FormAction::TYPE_SUBMIT)->setLabel($label)->build();
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitFormAction($submitAction));
     $this->assertContains("<input", $result);
     $this->assertContains("value={$label}", $result);
     $ajaxSubmitAction = FormActionBuilder::begin()->setType(FormAction::TYPE_AJAX_SUBMIT)->setLabel($label)->build();
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitFormAction($ajaxSubmitAction));
     $this->assertContains("onclick=athens.ajax.submitForm(\$(this).closest(form)[0]);", $result);
     $this->assertContains(">{$label}</button>", $result);
 }