View source
<?php
namespace Drupal\shurly\Form;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class ShurlyEditForm extends FormBase {
public function access(AccountInterface $account, $rid) {
if (is_numeric($rid)) {
$row = \Drupal::database()
->query('SELECT uid, source, destination FROM {shurly} WHERE rid = :rid', [
'rid' => $rid,
])
->fetchObject();
return AccessResult::allowedIf($account
->hasPermission('administer short URLs') || $account
->hasPermission('edit own URLs') && $row->uid == $account
->id());
}
}
public function getFormId() {
return 'shurly_edit_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $rid = NULL) {
$shurly_link = \Drupal::database()
->query('SELECT * FROM {shurly} WHERE rid = :rid', [
'rid' => $rid,
])
->fetchAllAssoc('rid');
$shurly_history = \Drupal::database()
->query('SELECT * FROM {shurly_history} WHERE rid = :rid', [
'rid' => $rid,
])
->fetchAll();
$shurly_history_count = count($shurly_history);
$form_state
->setStorage([
'shurly' => [
'rid' => $rid,
'source' => $shurly_link[$rid]->source,
'count' => $shurly_link[$rid]->count,
'destination' => urldecode($shurly_link[$rid]->destination),
],
]);
if ($shurly_history) {
$form['history'] = [
'#prefix' => '<table>',
'#suffix' => '</table>',
'#tree' => TRUE,
];
$form['history']['header'] = [
'#markup' => '<thead>
<tr>
<th>' . t('Source') . '</th>
<th>' . t('Changed') . '</th>
</tr>
</thead>',
];
for ($i = 0; $i < $shurly_history_count; $i++) {
$form['history']['row_' . $i] = [
'#prefix' => '<tr class="' . ($i % 2 ? "odd" : "even") . '">',
'#suffix' => '</tr>',
];
$form['history']['row_' . $i]['destination'] = [
'#prefix' => '<td>',
'#suffix' => '</td>',
'#markup' => link::fromTextAndUrl(Unicode::truncate($shurly_history[$i]->destination, 30), Url::fromUri($shurly_history[$i]->destination, [
'attributes' => [
'target' => [
'_blank',
],
],
]))
->toString(),
];
$form['history']['row_' . $i]['last_date'] = [
'#prefix' => '<td>',
'#suffix' => '</td>',
'#markup' => \Drupal::service('date.formatter')
->format($shurly_history[$i]->last_date, 'short'),
];
}
}
$form['source'] = [
'#type' => 'textfield',
'#title' => 'source',
'#value' => $shurly_link[$rid]->source,
'#disabled' => TRUE,
];
$form['destination'] = [
'#type' => 'textfield',
'#title' => 'destination',
'#value' => $shurly_link[$rid]->destination,
];
$form['rid'] = [
'#type' => 'hidden',
'#value' => $rid,
];
$form['count'] = [
'#type' => 'hidden',
'#value' => $shurly_link[$rid]->count,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!shurly_validate_long($form_state
->getValue('destination'))) {
$form_state
->setErrorByName('long_url', t('Invalid URL'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$storage =& $form_state
->getStorage();
$rid = $storage['shurly']['rid'];
$new_destination = $form_state
->getValue('destination');
$request_time = \Drupal::time()
->getRequestTime();
$previous_history = \Drupal::database()
->query('SELECT * FROM {shurly_history} WHERE rid = :rid ORDER BY vid DESC LIMIT 1', [
'rid' => $rid,
])
->fetchAssoc();
\Drupal::database()
->query('INSERT INTO {shurly_history} (rid, vid, source, destination, last_date, count) VALUES (:rid, :vid, :source, :destination, :last_date, :count)', [
':rid' => $rid,
':vid' => isset($previous_history['vid']) ? $previous_history['vid'] + 1 : 0,
':source' => $storage['shurly']['source'],
':destination' => $storage['shurly']['destination'],
':last_date' => $request_time,
':count' => $storage['shurly']['count'],
]);
\Drupal::database()
->query('UPDATE {shurly} SET destination = :new_destination, count = :reset_count WHERE rid = :rid', [
':new_destination' => $new_destination,
':reset_count' => 0,
'rid' => $rid,
]);
unset($storage['shurly']);
$this
->messenger()
->addStatus(t('URL has been edited.'));
}
}