2017-01-03 23:41:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorEditEngineProfileMenuItem
|
|
|
|
extends PhabricatorProfileMenuItem {
|
|
|
|
|
|
|
|
const MENUITEMKEY = 'editengine';
|
|
|
|
|
2017-01-20 20:03:09 +01:00
|
|
|
const FIELD_FORM = 'formKey';
|
|
|
|
|
2017-01-03 23:41:09 +01:00
|
|
|
private $form;
|
|
|
|
|
|
|
|
public function getMenuItemTypeIcon() {
|
|
|
|
return 'fa-plus';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMenuItemTypeName() {
|
2017-01-18 04:15:09 +01:00
|
|
|
return pht('Form');
|
2017-01-03 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function canAddToObject($object) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function attachForm($form) {
|
|
|
|
$this->form = $form;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getForm() {
|
|
|
|
$form = $this->form;
|
|
|
|
if (!$form) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function willBuildNavigationItems(array $items) {
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
$engines = PhabricatorEditEngine::getAllEditEngines();
|
|
|
|
$engine_keys = array_keys($engines);
|
|
|
|
$forms = id(new PhabricatorEditEngineConfigurationQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withEngineKeys($engine_keys)
|
|
|
|
->withIsDisabled(false)
|
|
|
|
->execute();
|
|
|
|
$form_engines = mgroup($forms, 'getEngineKey');
|
|
|
|
$form_ids = $forms;
|
|
|
|
|
|
|
|
$builtin_map = array();
|
|
|
|
foreach ($form_engines as $engine_key => $form_engine) {
|
|
|
|
$builtin_map[$engine_key] = mpull($form_engine, null, 'getBuiltinKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$key = $item->getMenuItemProperty('formKey');
|
2017-01-20 20:03:09 +01:00
|
|
|
list($engine_key, $form_key) = PhabricatorEditEngine::splitFullKey($key);
|
|
|
|
|
2017-01-03 23:41:09 +01:00
|
|
|
if (is_numeric($form_key)) {
|
|
|
|
$form = idx($form_ids, $form_key, null);
|
|
|
|
$item->getMenuItem()->attachForm($form);
|
|
|
|
} else if (isset($builtin_map[$engine_key][$form_key])) {
|
|
|
|
$form = $builtin_map[$engine_key][$form_key];
|
|
|
|
$item->getMenuItem()->attachForm($form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDisplayName(
|
|
|
|
PhabricatorProfileMenuItemConfiguration $config) {
|
|
|
|
$form = $this->getForm();
|
|
|
|
if (!$form) {
|
|
|
|
return pht('(Restricted/Invalid Form)');
|
|
|
|
}
|
|
|
|
if (strlen($this->getName($config))) {
|
|
|
|
return $this->getName($config);
|
|
|
|
} else {
|
|
|
|
return $form->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildEditEngineFields(
|
|
|
|
PhabricatorProfileMenuItemConfiguration $config) {
|
|
|
|
return array(
|
|
|
|
id(new PhabricatorDatasourceEditField())
|
2017-01-20 20:03:09 +01:00
|
|
|
->setKey(self::FIELD_FORM)
|
2017-01-03 23:41:09 +01:00
|
|
|
->setLabel(pht('Form'))
|
2017-01-19 22:23:42 +01:00
|
|
|
->setIsRequired(true)
|
2017-01-03 23:41:09 +01:00
|
|
|
->setDatasource(new PhabricatorEditEngineDatasource())
|
|
|
|
->setSingleValue($config->getMenuItemProperty('formKey')),
|
2017-01-20 20:42:29 +01:00
|
|
|
id(new PhabricatorTextEditField())
|
|
|
|
->setKey('name')
|
|
|
|
->setLabel(pht('Name'))
|
|
|
|
->setValue($this->getName($config)),
|
2017-01-03 23:41:09 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getName(
|
|
|
|
PhabricatorProfileMenuItemConfiguration $config) {
|
|
|
|
return $config->getMenuItemProperty('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function newNavigationMenuItems(
|
|
|
|
PhabricatorProfileMenuItemConfiguration $config) {
|
|
|
|
|
|
|
|
$form = $this->getForm();
|
|
|
|
if (!$form) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$engine = $form->getEngine();
|
|
|
|
$form_key = $form->getIdentifier();
|
|
|
|
|
|
|
|
$icon = $form->getIcon();
|
|
|
|
$name = $this->getDisplayName($config);
|
|
|
|
$href = $engine->getEditURI(null, "form/{$form_key}/");
|
|
|
|
|
|
|
|
$item = $this->newItem()
|
|
|
|
->setHref($href)
|
|
|
|
->setName($name)
|
|
|
|
->setIcon($icon);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$item,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-20 20:03:09 +01:00
|
|
|
public function validateTransactions(
|
|
|
|
PhabricatorProfileMenuItemConfiguration $config,
|
|
|
|
$field_key,
|
|
|
|
$value,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if ($field_key == self::FIELD_FORM) {
|
|
|
|
if ($this->isEmptyTransaction($value, $xactions)) {
|
|
|
|
$errors[] = $this->newRequiredError(
|
|
|
|
pht('You must choose a form.'),
|
|
|
|
$field_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
$new = $xaction['new'];
|
|
|
|
|
|
|
|
if (!$new) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($new === $value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($engine_key, $form_key) = PhabricatorEditEngine::splitFullKey(
|
|
|
|
$new);
|
|
|
|
|
|
|
|
$forms = id(new PhabricatorEditEngineConfigurationQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withEngineKeys(array($engine_key))
|
|
|
|
->withIdentifiers(array($form_key))
|
|
|
|
->execute();
|
|
|
|
if (!$forms) {
|
|
|
|
$errors[] = $this->newInvalidError(
|
|
|
|
pht(
|
|
|
|
'Form "%s" is not a valid form which you have permission to '.
|
|
|
|
'see.',
|
|
|
|
$new),
|
|
|
|
$xaction['xaction']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2017-01-03 23:41:09 +01:00
|
|
|
}
|