Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to add multiple request forms #734

Merged
merged 10 commits into from
Aug 8, 2022
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"ext-mbstring": "*",
"ext-simplexml": "*",
"mediawiki/oauthclient": "^1.2",
"php-amqplib/php-amqplib": ">=3.0"
"php-amqplib/php-amqplib": ">=3.0",
"league/commonmark": "^1.6"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3",
Expand Down
89 changes: 88 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions includes/DataObjects/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Request extends DataObject
private $forwardedip;
private $hasComments = false;
private $hasCommentsResolved = false;
private $originform;

/**
* @throws Exception
Expand All @@ -50,11 +51,11 @@ public function save()
INSERT INTO `request` (
email, ip, name, status, date, emailsent,
emailconfirm, reserved, useragent, forwardedip,
queue
queue, originform
) VALUES (
:email, :ip, :name, :status, CURRENT_TIMESTAMP(), :emailsent,
:emailconfirm, :reserved, :useragent, :forwardedip,
:queue
:queue, :originform
);
SQL
);
Expand All @@ -68,6 +69,7 @@ public function save()
$statement->bindValue(':useragent', $this->useragent);
$statement->bindValue(':forwardedip', $this->forwardedip);
$statement->bindValue(':queue', $this->queue);
$statement->bindValue(':originform', $this->originform);

if ($statement->execute()) {
$this->id = (int)$this->dbObject->lastInsertId();
Expand Down Expand Up @@ -455,4 +457,32 @@ public function setQueue(?int $queue): void
{
$this->queue = $queue;
}

/**
* @return int|null
*/
public function getOriginForm(): ?int
{
return $this->originform;
}

public function getOriginFormObject(): ?RequestForm
{
if ($this->originform === null) {
return null;
}

/** @var RequestForm|bool $form */
$form = RequestForm::getById($this->originform, $this->getDatabase());

return $form === false ? null : $form;
}

/**
* @param int|null $originForm
*/
public function setOriginForm(?int $originForm): void
{
$this->originform = $originForm;
}
}
154 changes: 149 additions & 5 deletions includes/DataObjects/RequestForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
namespace Waca\DataObjects;

use Exception;
use PDO;
use Waca\DataObject;
use Waca\Exceptions\OptimisticLockFailedException;
use Waca\PdoDatabase;

class RequestForm extends DataObject
{
Expand All @@ -19,23 +21,98 @@ class RequestForm extends DataObject
/** @var int */
private $domain;
/** @var string */
private $name;
private $name = '';
/** @var string */
private $publicendpoint;
private $publicendpoint = '';
/** @var string */
private $formcontent;
private $formcontent = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why these changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because PHP.

Actually, because the get*() methods in RequestForm are type-restricted to be a string, and PHP defaults variables to null. Since these are required fields, I don't really want to make them nullable (the string? type annotation)

For both creation and edits, PageRequestFormManagement calls the populateFromObject method to make it easier to set all the field assignments on the template, but for new objects it's done via populateFromObject(new RequestForm()), thus all the fields are default values, and (without this change) the fields are all null which doesn't match the type annotation on the get*() methods. For a recent example of this sort of thing happening in prod, see #754

/** @var int|null */
private $overridequeue;
/** @var string */
private $usernamehelp;
/** @var string */
private $emailhelp;
/** @var string */
private $commentshelp;

/**
* @param PdoDatabase $database
* @param int $domain
*
* @return RequestForm[]
*/
public static function getAllForms(PdoDatabase $database, int $domain)
{
$statement = $database->prepare("SELECT * FROM requestform WHERE domain = :domain;");
$statement->execute([':domain' => $domain]);

$resultObject = $statement->fetchAll(PDO::FETCH_CLASS, get_called_class());

if ($resultObject === false) {
return [];
}

/** @var RequestQueue $t */
foreach ($resultObject as $t) {
$t->setDatabase($database);
}

return $resultObject;
}

public static function getByName(PdoDatabase $database, string $name, int $domain)
{
$statement = $database->prepare(<<<SQL
SELECT * FROM requestform WHERE name = :name AND domain = :domain;
SQL
);

$statement->execute([
':name' => $name,
':domain' => $domain,
]);

/** @var RequestForm|false $result */
$result = $statement->fetchObject(get_called_class());

if ($result !== false) {
$result->setDatabase($database);
}

return $result;
}

public static function getByPublicEndpoint(PdoDatabase $database, string $endpoint, int $domain)
{
$statement = $database->prepare(<<<SQL
SELECT * FROM requestform WHERE publicendpoint = :endpoint and domain = :domain;
SQL
);

$statement->execute([
':endpoint' => $endpoint,
':domain' => $domain,
]);

/** @var RequestForm|false $result */
$result = $statement->fetchObject(get_called_class());

if ($result !== false) {
$result->setDatabase($database);
}

return $result;
}

public function save()
{
if ($this->isNew()) {
// insert
$statement = $this->dbObject->prepare(<<<SQL
INSERT INTO requestform (
enabled, domain, name, publicendpoint, formcontent, overridequeue
enabled, domain, name, publicendpoint, formcontent, overridequeue, usernamehelp, emailhelp, commentshelp
) VALUES (
:enabled, :domain, :name, :publicendpoint, :formcontent, :overridequeue
:enabled, :domain, :name, :publicendpoint, :formcontent, :overridequeue, :usernamehelp, :emailhelp, :commentshelp
);
SQL
);
Expand All @@ -46,6 +123,9 @@ public function save()
$statement->bindValue(":publicendpoint", $this->publicendpoint);
$statement->bindValue(":formcontent", $this->formcontent);
$statement->bindValue(":overridequeue", $this->overridequeue);
$statement->bindValue(":usernamehelp", $this->usernamehelp);
$statement->bindValue(":emailhelp", $this->emailhelp);
$statement->bindValue(":commentshelp", $this->commentshelp);

if ($statement->execute()) {
$this->id = (int)$this->dbObject->lastInsertId();
Expand All @@ -63,6 +143,9 @@ public function save()
publicendpoint = :publicendpoint,
formcontent = :formcontent,
overridequeue = :overridequeue,
usernamehelp = :usernamehelp,
emailhelp = :emailhelp,
commentshelp = :commentshelp,

updateversion = updateversion + 1
WHERE id = :id AND updateversion = :updateversion;
Expand All @@ -75,6 +158,10 @@ public function save()
$statement->bindValue(":publicendpoint", $this->publicendpoint);
$statement->bindValue(":formcontent", $this->formcontent);
$statement->bindValue(":overridequeue", $this->overridequeue);
$statement->bindValue(":usernamehelp", $this->usernamehelp);
$statement->bindValue(":emailhelp", $this->emailhelp);
$statement->bindValue(":commentshelp", $this->commentshelp);


$statement->bindValue(':id', $this->id);
$statement->bindValue(':updateversion', $this->updateversion);
Expand Down Expand Up @@ -115,6 +202,17 @@ public function getDomain(): int
return $this->domain;
}

public function getDomainObject(): ?Domain
{
if ($this->domain !== null) {
/** @var Domain $domain */
$domain = Domain::getById($this->domain, $this->getDatabase());
return $domain;
}

return null;
}

/**
* @param int $domain
*/
Expand Down Expand Up @@ -187,5 +285,51 @@ public function setOverrideQueue(?int $overrideQueue): void
$this->overridequeue = $overrideQueue;
}

/**
* @return string
*/
public function getUsernameHelp(): ?string
{
return $this->usernamehelp;
}

/**
* @param string $usernamehelp
*/
public function setUsernameHelp(string $usernamehelp): void
{
$this->usernamehelp = $usernamehelp;
}

/**
* @return string
*/
public function getEmailHelp(): ?string
{
return $this->emailhelp;
}

/**
* @param string $emailhelp
*/
public function setEmailHelp(string $emailhelp): void
{
$this->emailhelp = $emailhelp;
}

/**
* @return string
*/
public function getCommentHelp(): ?string
{
return $this->commentshelp;
}

/**
* @param string $commenthelp
*/
public function setCommentHelp(string $commenthelp): void
{
$this->commentshelp = $commenthelp;
}
}
Loading