File: //opt/proundcontra.mosmuc.de/pages/editArgument.php
<?php
class PageEditArgument extends Page
{
public function __construct($row)
{
global $sDB, $sRequest, $sTemplate, $sUser, $sSession;
parent::__construct($row);
$questionTitle = $sRequest->getString("title");
$this->question = false;
$this->argument = false;
$this->view = VIEW_EDIT_ARGUMENT;
$argumentTitle = $sRequest->getString("argument");
$res = $sDB->exec("SELECT * FROM `questions` WHERE `url` = '".mysqli_real_escape_string($sDB->getLink(), $questionTitle)."' LIMIT 1;");
while($rowQ = mysqli_fetch_object($res))
{
$this->question = new Question($rowQ->questionId, $rowQ);
}
if(!$this->question)
{
$sTemplate->error($sTemplate->getString("ERROR_INVALID_QUESTION"));
}
foreach($this->question->arguments() as $k => $v)
{
if($v->urlPlain() == $argumentTitle)
{
$this->argument = $v;
break;
}
}
if(!$this->argument)
{
$sTemplate->error($sTemplate->getString("ERROR_INVALID_ARGUMENT"));
}
// Check if user can edit
if(!$this->argument->canEdit($sUser))
{
$sSession->setVal('notification', $sTemplate->getString("ARGUMENT_EDIT_EXCEEDED"));
$sSession->serialize();
header("Location: ".$this->question->url());
exit;
}
// Handle the form submission
if($sRequest->getInt("edit_argument"))
{
if($this->handleEditArgument())
{
header("Location: ".$this->redirectUrl);
exit;
}
}
}
public function getQuestion()
{
return $this->question;
}
public function getView()
{
return $this->view;
}
public function canView()
{
global $sUser, $sTemplate, $sPermissions;
if(!$this->question)
{
$this->setError($sTemplate->getString("ERROR_INVALID_QUESTION"));
return false;
}
if(!$sUser->isLoggedIn())
{
$this->setError($sTemplate->getString("ERROR_NOT_LOGGED_IN"));
return false;
}
if($sPermissions->getPermission($sUser, ACTION_NEW_ARGUMENT) == PERMISSION_DISALLOWED)
{
$this->setError($sTemplate->getString("ERROR_NO_PERMISSION"));
return false;
}
return true;
}
public function basePath()
{
return $this->question->url();
}
public function getArgument()
{
return $this->argument;
}
public function handleEditArgument()
{
global $sRequest, $sTemplate, $sUser, $sPermissions;
if(!$sUser->isLoggedIn() ||
$sPermissions->getPermission($sUser, ACTION_NEW_ARGUMENT) == PERMISSION_DISALLOWED)
{
return false;
}
$headline = $sRequest->getStringPlain("new_argument_headline");
$headline = mb_substr($headline, 0, MAX_ARGUMENT_HEADLINE_CHR_LENGTH, 'utf-8');
$headlineParsed = preg_replace("/[^0-9a-zÄÖÜäöüáàâéèêíìîóòôúùû\[\]\{\} -]/i", "", $headline);
$abstract = $sRequest->getStringPlain("new_argument_abstract");
$abstract = mb_substr($abstract, 0, MAX_ARGUMENT_ABS_CHR_LENGTH, 'utf-8');
$details = $sRequest->getStringPlain("new_argument_details");
if($headline != "" && $abstract && $headlineParsed != "")
{
return $this->store($headline, $headlineParsed, $abstract, $details);
}
// Set errors for missing fields
if($headlineParsed == "")
{
$this->setError($sTemplate->getString("ERROR_NEW_ARGUMENT_MISSING_HEADLINE"));
}
else if($abstract == "")
{
$this->setError($sTemplate->getString("ERROR_NEW_ARGUMENT_MISSING_ABSTRACT"));
}
else if($details == "")
{
$this->setError($sTemplate->getString("ERROR_NEW_ARGUMENT_MISSING_DETAILS"));
}
else
{
$this->setError($sTemplate->getString("ERROR_NEW_ARGUMENT_TRY_AGAIN"));
}
return false;
}
private function store($headline, $headlineParsed, $abstract, $details)
{
global $sDB, $sUser, $sStatistics;
$questionId = $this->question->questionId();
// If the headline changed, we may need a new URL
if($headline != $this->argument->headlinePlain())
{
$url = url_sanitize($headlineParsed);
$i = 0;
while(true)
{
$cur = $url.($i > 0 ? '-'.$i : '');
$res = $sDB->exec("SELECT `url` FROM `arguments`
WHERE `questionId` = '".i($questionId)."'
AND `parentId` = '0'
AND `url` = '".mysqli_real_escape_string($sDB->getLink(), $cur)."'
LIMIT 1;");
if(mysqli_num_rows($res))
{
$i++;
continue;
}
break;
}
if($i > 0)
{
$url .= '-'.$i;
}
}
else
{
// Keep the existing URL
$url = $this->argument->urlPlain();
}
// Update the DB row
$sDB->exec("
UPDATE `arguments`
SET `url` = '".mysqli_real_escape_string($sDB->getLink(), $url)."',
`headline` = '".mysqli_real_escape_string($sDB->getLink(), $headline)."',
`abstract` = '".mysqli_real_escape_string($sDB->getLink(), $abstract)."',
`details` = '".mysqli_real_escape_string($sDB->getLink(), $details)."',
`score` = '0'
WHERE `argumentId` = '".i($this->argument()->argumentId())."'
LIMIT 1;
");
$sStatistics->resetArgumentVotes($this->argument());
// Redirect to the question’s page after saving
$this->redirectUrl = $this->question->url();
return $this->argument()->argumentId();
}
public function title()
{
global $sTemplate;
return $sTemplate->getString("HTML_META_TITLE_EDIT_ARGUMENT");
}
public function argument()
{
return $this->argument;
}
private $question;
private $argument;
private $view;
private $redirectUrl;
}
?>