<commit_before><?php
namespace wcf\form;
use wcf\data\IStorableObject;
use wcf\system\event\EventHandler;
use wcf\system\exception\UserInputException;
use wcf\system\form\builder\FormDocument;
use wcf\system\form\builder\IFormDocument;
use wcf\system\request\IRouteController;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;

/**
 * Abstract implementation of a form using the form builder API.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Form
 * @since	3.2
 */
abstract class AbstractFormBuilderForm extends AbstractForm {
	/**
	 * form document
	 * @var	IFormDocument
	 */
	public $form;
	
	/**
	 * action performed by the form
	 * by default `create` and `edit` is supported
	 * @var	string
	 */
	public $formAction = 'create';
	
	/**
	 * updated object, not relevant for form action `create`
	 * @var	IStorableObject
	 */
	public $formObject;
	
	/**
	 * name of the object action class performing the form action
	 * @var	string
	 */
	public $objectActionClass;
	
	/**
	 * @inheritDoc
	 */
	public function assignVariables() {
		parent::assignVariables();
		
		WCF::getTPL()->assign([
			'action' => $this->formAction === 'create' ? 'add' : 'edit',
			'form' => $this->form
		]);
	}
	
	/**
	 * Builds the form.
	 */
	public function buildForm() {
		$this->createForm();
		
		EventHandler::getInstance()->fireAction($this, 'createForm');
		
		$this->form->build();
		
		$this->finalizeForm();
		
		EventHandler::getInstance()->fireAction($this, 'buildForm');
	}
	
	/**
	 * Creates the form object.
	 * 
	 * This is the method that is intended to be overwritten by child classes
	 * to add the form containers and fields.
	 */
	protected function createForm() {
		$classNamePieces = explode('\\', get_class($this));
		$controller = preg_replace('~Form$~', '', end($classNamePieces));
		
		$this->form = FormDocument::create(lcfirst($controller));
		
		if ($this->formObject !== null) {
			$this->form->formMode(IFormDocument::FORM_MODE_UPDATE);
		}
	}
	
	/**
	 * Finalizes the form after it has been successfully built.
	 * 
	 * This method can be used to add form field dependencies.
	 */
	protected function finalizeForm() {
		// does nothing
	}
	
	/**
	 * @inheritDoc
	 */
	public function readData() {
		if ($this->formObject !== null) {
			$this->form->loadValuesFromObject($this->formObject);
		}
		else if ($this->formAction === 'edit') {
			throw new \UnexpectedValueException("Missing form object to update.");
		}
		
		parent::readData();
		
		$this->setFormAction();
	}
	
	/**
	 * @inheritDoc
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		$this->form->readValues();
	}
	
	/**
	 * @inheritDoc
	 */
	public function save() {
		parent::save();
		
		$this->objectAction = new $this->objectActionClass(array_filter([$this->formObject]), $this->formAction, $this->form->getData());
		$this->objectAction->executeAction();
		
		$this->saved();
		
		// re-build form after having created a new object
		if ($this->formAction === 'create') {
			$this->buildForm();
		}
		
		WCF::getTPL()->assign('success', true);
	}
	
	/**
	 * Sets the action of the form.
	 */
	protected function setFormAction() {
		$classNamePieces = explode('\\', get_class($this));
		$application = $classNamePieces[0];
		$controller = preg_replace('~Form$~', '', end($classNamePieces));
		
		$parameters = ['application' => $application];
		if ($this->formObject !== null) {
			if ($this->formObject instanceof IRouteController) {
				$parameters['object'] = $this->formObject;
			}
			else {
				$object = $this->formObject;
				
				$parameters['id'] = $object->{$object::getDatabaseTableIndexName()};
			}
		}
		
		$this->form->action(LinkHandler::getInstance()->getLink($controller, $parameters));
	}
	
	/**
	 * @inheritDoc
	 */
	public function show() {
		$this->buildForm();
		
		return parent::show();
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		parent::validate();
		
		$this->form->validate();
		
		if ($this->form->hasValidationErrors()) {
			throw new UserInputException($this->form->getPrefixedId());
		}
	}
}
<commit_msg>Add alternative object action name `AbstractFormBuilderForm`<commit_after><?php
namespace wcf\form;
use wcf\data\AbstractDatabaseObjectAction;
use wcf\data\IStorableObject;
use wcf\system\event\EventHandler;
use wcf\system\exception\UserInputException;
use wcf\system\form\builder\FormDocument;
use wcf\system\form\builder\IFormDocument;
use wcf\system\request\IRouteController;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;

/**
 * Abstract implementation of a form using the form builder API.
 * 
 * @author	<NAME>
 * @copyright	2001-2018 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\Form
 * @since	3.2
 */
abstract class AbstractFormBuilderForm extends AbstractForm {
	/**
	 * form document
	 * @var	IFormDocument
	 */
	public $form;
	
	/**
	 * action performed by the form
	 * by default `create` and `edit` is supported
	 * @var	string
	 */
	public $formAction = 'create';
	
	/**
	 * updated object, not relevant for form action `create`
	 * @var	IStorableObject
	 */
	public $formObject;
	
	/**
	 * name of the object action performing the form action
	 * if not set, `$formAction` is sued
	 * @var	null|string
	 */
	public $objectActionName;
	
	/**
	 * name of the object action class performing the form action
	 * @var	string
	 */
	public $objectActionClass;
	
	/**
	 * @inheritDoc
	 */
	public function assignVariables() {
		parent::assignVariables();
		
		WCF::getTPL()->assign([
			'action' => $this->formAction === 'create' ? 'add' : 'edit',
			'form' => $this->form
		]);
	}
	
	/**
	 * Builds the form.
	 */
	public function buildForm() {
		$this->createForm();
		
		EventHandler::getInstance()->fireAction($this, 'createForm');
		
		$this->form->build();
		
		$this->finalizeForm();
		
		EventHandler::getInstance()->fireAction($this, 'buildForm');
	}
	
	/**
	 * Creates the form object.
	 * 
	 * This is the method that is intended to be overwritten by child classes
	 * to add the form containers and fields.
	 */
	protected function createForm() {
		$classNamePieces = explode('\\', get_class($this));
		$controller = preg_replace('~Form$~', '', end($classNamePieces));
		
		$this->form = FormDocument::create(lcfirst($controller));
		
		if ($this->formObject !== null) {
			$this->form->formMode(IFormDocument::FORM_MODE_UPDATE);
		}
	}
	
	/**
	 * Finalizes the form after it has been successfully built.
	 * 
	 * This method can be used to add form field dependencies.
	 */
	protected function finalizeForm() {
		// does nothing
	}
	
	/**
	 * @inheritDoc
	 */
	public function readData() {
		if ($this->formObject !== null) {
			$this->form->loadValuesFromObject($this->formObject);
		}
		else if ($this->formAction === 'edit') {
			throw new \UnexpectedValueException("Missing form object to update.");
		}
		
		parent::readData();
		
		$this->setFormAction();
	}
	
	/**
	 * @inheritDoc
	 */
	public function readFormParameters() {
		parent::readFormParameters();
		
		$this->form->readValues();
	}
	
	/**
	 * @inheritDoc
	 */
	public function save() {
		parent::save();
		
		/** @var AbstractDatabaseObjectAction objectAction */
		$this->objectAction = new $this->objectActionClass(
			array_filter([$this->formObject]),
			$this->objectActionName ?: $this->formAction,
			$this->form->getData()
		);
		$this->objectAction->executeAction();
		
		$this->saved();
		
		// re-build form after having created a new object
		if ($this->formAction === 'create') {
			$this->buildForm();
		}
		
		WCF::getTPL()->assign('success', true);
	}
	
	/**
	 * Sets the action of the form.
	 */
	protected function setFormAction() {
		$classNamePieces = explode('\\', get_class($this));
		$application = $classNamePieces[0];
		$controller = preg_replace('~Form$~', '', end($classNamePieces));
		
		$parameters = ['application' => $application];
		if ($this->formObject !== null) {
			if ($this->formObject instanceof IRouteController) {
				$parameters['object'] = $this->formObject;
			}
			else {
				$object = $this->formObject;
				
				$parameters['id'] = $object->{$object::getDatabaseTableIndexName()};
			}
		}
		
		$this->form->action(LinkHandler::getInstance()->getLink($controller, $parameters));
	}
	
	/**
	 * @inheritDoc
	 */
	public function show() {
		$this->buildForm();
		
		return parent::show();
	}
	
	/**
	 * @inheritDoc
	 */
	public function validate() {
		parent::validate();
		
		$this->form->validate();
		
		if ($this->form->hasValidationErrors()) {
			throw new UserInputException($this->form->getPrefixedId());
		}
	}
}

{'fraction_non_alphanumeric': 0.16372135446357314, 'fraction_numerical': 0.0025082658761828754, 'mean_word_length': 3.6833956219967967, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3402670', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2650, 'n_words': 623}