浏览代码

Add Qurls backend.

Mark Scherer 10 年之前
父节点
当前提交
b4d87ff821

+ 127 - 0
Controller/QurlsController.php

@@ -0,0 +1,127 @@
+<?php
+App::uses('ToolsAppController', 'Tools.Controller');
+
+/**
+ * Qurls Controller
+ *
+ */
+class QurlsController extends ToolsAppController {
+
+	public $paginate = [];
+
+	public $components = ['Tools.Common'];
+
+	public function beforeFilter() {
+		parent::beforeFilter();
+
+		if (isset($this->Auth)) {
+			$this->Auth->allow('go');
+		}
+	}
+
+	/**
+	 * Main login function
+	 */
+	public function go($key) {
+		$entry = $this->Qurl->translate($key);
+		if (empty($entry)) {
+			throw new NotFoundException();
+		}
+		//die(returns($entry));
+		$note = $entry['Qurl']['note'];
+		$url = $entry['Qurl']['url'];
+
+		if ($note) {
+			$this->Flash->message(nl2br($note), 'info');
+		}
+		$this->Qurl->markAsUsed($entry['Qurl']['id']);
+		return $this->redirect($url);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function admin_index() {
+		$this->Qurl->recursive = 0;
+		$qurls = $this->paginate();
+		$this->set(compact('qurls'));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function admin_view($id = null) {
+		$this->Qurl->recursive = 0;
+		if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
+			$this->Flash->message(__('invalidRecord'), 'error');
+			return $this->Common->autoRedirect(['action' => 'index']);
+		}
+		$this->set(compact('qurl'));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function admin_add($templateId = null) {
+		if ($this->Common->isPosted()) {
+			$this->Qurl->create();
+			$this->request->data['Qurl']['key'] = '';
+			if ($res = $this->Qurl->save($this->request->data)) {
+				$var = $this->Qurl->urlByKey($res['Qurl']['key'], $res['Qurl']['title']);
+
+				$this->Flash->message(__('Qurl: %s', $var), 'success');
+				return $this->Common->postRedirect(['action' => 'index']);
+			} else {
+				$this->Flash->message(__('formContainsErrors'), 'error');
+			}
+		} else {
+			$this->request->data['Qurl']['active'] = 1;
+
+			if ($templateId && ($template = $this->Qurl->get($templateId))) {
+				$this->request->data = $template;
+			}
+		}
+	}
+
+	/**
+	 * @return void
+	 */
+	public function admin_edit($id = null) {
+		if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
+			$this->Flash->message(__('invalidRecord'), 'error');
+			return $this->Common->autoRedirect(['action' => 'index']);
+		}
+		if ($this->Common->isPosted()) {
+			if ($this->Qurl->save($this->request->data)) {
+				$var = $this->request->data['Qurl']['key'];
+				$this->Flash->message(__('record edit %s saved', h($var)), 'success');
+				return $this->Common->postRedirect(['action' => 'index']);
+			} else {
+				$this->Flash->message(__('formContainsErrors'), 'error');
+			}
+		}
+		if (empty($this->request->data)) {
+			$this->request->data = $qurl;
+		}
+	}
+
+	/**
+	 * @return void
+	 */
+	public function admin_delete($id = null) {
+		$this->request->allowMethod('post');
+		if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id], 'fields' => ['id', 'key']]))) {
+			$this->Flash->message(__('invalidRecord'), 'error');
+			return $this->Common->autoRedirect(['action' => 'index']);
+		}
+		$var = $qurl['Qurl']['key'];
+
+		if ($this->Qurl->delete($id)) {
+			$this->Flash->message(__('record del %s done', h($var)), 'success');
+			return $this->redirect(['action' => 'index']);
+		}
+		$this->Flash->message(__('record del %s not done exception', h($var)), 'error');
+		return $this->Common->autoRedirect(['action' => 'index']);
+	}
+
+}

+ 148 - 0
Test/Case/Controller/QurlsControllerTest.php

@@ -0,0 +1,148 @@
+<?php
+App::uses('QurlsController', 'ToolsExtra.Controller');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
+
+/**
+ * QurlsController Test Case
+ *
+ */
+class QurlsControllerTest extends MyCakeTestCase {
+
+	public $Qurls;
+
+	/**
+	 * Fixtures
+	 *
+	 * @var array
+	 */
+	public $fixtures = ['plugin.tools.qurl', 'core.auth_user'];
+
+	/**
+	 * SetUp method
+	 *
+	 * @return void
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		$this->Qurls = new QurlsController();
+	}
+
+	/**
+	 * TearDown method
+	 *
+	 * @return void
+	 */
+	public function tearDown() {
+		unset($this->Qurls);
+
+		parent::tearDown();
+	}
+
+	/**
+	 * TestIndex method
+	 *
+	 * @return void
+	 */
+	public function testIndex() {
+	}
+
+	/**
+	 * TestView method
+	 *
+	 * @return void
+	 */
+	public function testView() {
+	}
+
+	/**
+	 * TestAdd method
+	 *
+	 * @return void
+	 */
+	public function testAdd() {
+	}
+
+	/**
+	 * TestEdit method
+	 *
+	 * @return void
+	 */
+	public function testEdit() {
+	}
+
+	/**
+	 * TestDelete method
+	 *
+	 * @return void
+	 */
+	public function testDelete() {
+	}
+
+	/**
+	 * TestAdminIndex method
+	 *
+	 * @return void
+	 */
+	public function testAdminIndex() {
+	}
+
+	/**
+	 * TestAdminView method
+	 *
+	 * @return void
+	 */
+	public function testAdminView() {
+	}
+
+	/**
+	 * TestAdminAdd method
+	 *
+	 * @return void
+	 */
+	public function testAdminAdd() {
+	}
+
+	/**
+	 * TestAdminEdit method
+	 *
+	 * @return void
+	 */
+	public function testAdminEdit() {
+	}
+
+	/**
+	 * TestAdminDelete method
+	 *
+	 * @return void
+	 */
+	public function testAdminDelete() {
+	}
+
+}
+
+/**
+ * TestQurlsController
+ *
+ */
+class TestQurlsController extends QurlsController {
+
+	/**
+	 * Auto render
+	 *
+	 * @var bool
+	 */
+	public $autoRender = false;
+
+	/**
+	 * Redirect action
+	 *
+	 * @param mixed $url
+	 * @param mixed $status
+	 * @param bool $exit
+	 * @return void
+	 */
+	public function redirect($url, $status = null, $exit = true) {
+		$this->redirectUrl = $url;
+	}
+}

+ 28 - 0
View/Qurls/admin_add.ctp

@@ -0,0 +1,28 @@
+<div class="page form">
+<h2><?php echo __('Add %s', __('Qurl')); ?></h2>
+
+<?php echo $this->Form->create('Qurl');?>
+	<fieldset>
+		<legend><?php echo __('Add %s', __('Qurl')); ?></legend>
+	<?php
+		echo $this->Form->input('url');
+		echo $this->Form->input('title');
+		echo $this->Form->input('note', ['type' => 'textarea']);
+		echo $this->Form->input('active');
+	?>
+	</fieldset>
+	<fieldset>
+		<legend><?php echo __('Internal details')?></legend>
+	<?php
+		echo $this->Form->input('comment', ['type' => 'textarea']);
+	 ?>
+	</fieldset>
+<?php echo $this->Form->submit(__('Submit')); echo $this->Form->end();?>
+</div>
+
+<div class="actions">
+	<ul>
+
+		<li><?php echo $this->Html->link(__('List %s', __('Qurls')), ['action' => 'index']);?></li>
+	</ul>
+</div>

+ 32 - 0
View/Qurls/admin_edit.ctp

@@ -0,0 +1,32 @@
+<div class="page form">
+<h2><?php echo __('Edit %s', __('Qurl')); ?></h2>
+
+<?php echo $this->Form->create('Qurl');?>
+	<fieldset>
+		<legend><?php echo __('Edit %s', __('Qurl')); ?></legend>
+	<?php
+		echo $this->Form->input('id');
+		echo $this->Form->input('url');
+		echo $this->Form->input('title');
+		echo $this->Form->input('note', ['type' => 'textarea']);
+		echo $this->Form->input('active');
+	?>
+	</fieldset>
+	<fieldset>
+		<legend><?php echo __('Internal details')?></legend>
+	<?php
+		echo $this->Form->input('comment', ['type' => 'textarea']);
+		echo $this->Form->input('used');
+		echo $this->Form->input('last_used', ['empty' => '- -', 'dateFormat' => 'DMY', 'timeFormat' => 24]);
+	 ?>
+	</fieldset>
+<?php echo $this->Form->submit(__('Submit')); echo $this->Form->end();?>
+</div>
+
+<div class="actions">
+	<ul>
+
+		<li><?php echo $this->Form->postLink(__('Delete'), ['action' => 'delete', $this->Form->value('Qurl.id')], ['confirm' => __('Are you sure you want to delete # %s?', $this->Form->value('Qurl.id'))]); ?></li>
+		<li><?php echo $this->Html->link(__('List %s', __('Qurls')), ['action' => 'index']);?></li>
+	</ul>
+</div>

+ 62 - 0
View/Qurls/admin_index.ctp

@@ -0,0 +1,62 @@
+<div class="page index">
+	<h2><?php echo __('Qurls');?></h2>
+
+	<table class="list">
+		<tr>
+		<th><?php echo $this->Paginator->sort('url');?></th>
+		<th><?php echo $this->Paginator->sort('used');?></th>
+		<th><?php echo $this->Paginator->sort('last_used');?></th>
+		<th><?php echo $this->Paginator->sort('created', null, ['direction' => 'desc']);?></th>
+		<th class="actions"><?php echo __('Actions');?></th>
+	</tr>
+<?php
+$i = 0;
+foreach ($qurls as $qurl): ?>
+	<tr>
+		<td>
+			<?php
+				$url = $qurl['Qurl']['url'];
+				if (strpos($url, $baseUrl = Router::url('/', true)) === 0) {
+					$url = substr($url, strlen($baseUrl));
+					if (strpos($url, '/') !== 0) {
+						$url = '/'.$url;
+					}
+				}
+				echo $url; ?>
+			<div><code><?php echo Qurl::urlByKey($qurl['Qurl']['key'], $qurl['Qurl']['title']); ?></code></div>
+			<?php if ($qurl['Qurl']['comment']) { ?>
+			<small><?php echo h($qurl['Qurl']['comment']); ?></small>
+			<?php } ?>
+		</td>
+
+		<td>
+			<?php echo h($qurl['Qurl']['used']); ?>
+			<div><?php echo $this->Format->yesNo($qurl['Qurl']['active'], __('Active'), __('Inactive')); ?></div>
+		</td>
+		<td>
+			<?php echo $this->Datetime->niceDate($qurl['Qurl']['last_used']); ?>
+		</td>
+		<td>
+			<?php echo $this->Datetime->niceDate($qurl['Qurl']['created']); ?>
+		</td>
+		<td class="actions">
+			<?php echo $this->Html->link($this->Format->icon('add', 'Als Vorlage verwenden'), ['action'=>'add', $qurl['Qurl']['id']], ['escape'=>false]); ?>
+			<?php echo $this->Html->link($this->Format->icon('view'), ['action'=>'view', $qurl['Qurl']['id']], ['escape'=>false]); ?>
+			<?php echo $this->Html->link($this->Format->icon('edit'), ['action'=>'edit', $qurl['Qurl']['id']], ['escape'=>false]); ?>
+			<?php echo $this->Form->postLink($this->Format->icon('delete'), ['action'=>'delete', $qurl['Qurl']['id']], ['escape' => false, 'confirm' => __('Are you sure you want to delete # %s?', $qurl['Qurl']['id'])]); ?>
+		</td>
+	</tr>
+<?php endforeach; ?>
+	</table>
+
+	<div class="pagination-container">
+<?php echo $this->element('pagination', [], ['plugin'=>'tools']); ?>
+	</div>
+
+</div>
+
+<div class="actions">
+	<ul>
+		<li><?php echo $this->Html->link(__('New %s', __('Qurl')), ['action' => 'add']); ?></li>
+	</ul>
+</div>

+ 38 - 0
View/Qurls/admin_view.ctp

@@ -0,0 +1,38 @@
+<div class="page view">
+<h2><?php echo __('Qurl');?></h2>
+	<dl>
+		<dt><?php echo __('Url'); ?></dt>
+		<dd>
+			<?php echo h($qurl['Qurl']['url']); ?>
+			&nbsp;
+		</dd>
+		<dt><?php echo __('Comment'); ?></dt>
+		<dd>
+			<?php echo h($qurl['Qurl']['comment']); ?>
+			&nbsp;
+		</dd>
+		<dt><?php echo __('Used'); ?></dt>
+		<dd>
+			<?php echo h($qurl['Qurl']['used']); ?>
+			&nbsp;
+		</dd>
+		<dt><?php echo __('Last Used'); ?></dt>
+		<dd>
+			<?php echo $this->Datetime->niceDate($qurl['Qurl']['last_used']); ?>
+			&nbsp;
+		</dd>
+		<dt><?php echo __('Created'); ?></dt>
+		<dd>
+			<?php echo $this->Datetime->niceDate($qurl['Qurl']['created']); ?>
+			&nbsp;
+		</dd>
+	</dl>
+</div>
+
+<div class="actions">
+	<ul>
+		<li><?php echo $this->Html->link(__('Edit %s', __('Qurl')), ['action' => 'edit', $qurl['Qurl']['id']]); ?> </li>
+		<li><?php echo $this->Form->postLink(__('Delete %s', __('Qurl')), ['action' => 'delete', $qurl['Qurl']['id']], ['confirm' => __('Are you sure you want to delete # %s?', $qurl['Qurl']['id'])]); ?> </li>
+		<li><?php echo $this->Html->link(__('List %s', __('Qurls')), ['action' => 'index']); ?> </li>
+	</ul>
+</div>