Skip to content

Commit

Permalink
Adding URL encoding feature to uris
Browse files Browse the repository at this point in the history
  • Loading branch information
webtechnick committed Apr 26, 2011
1 parent fcb9e6a commit 5b1fd1e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
10 changes: 10 additions & 0 deletions controllers/seo_uris_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function admin_index($filter = null) {
$this->set('seoUris',$this->paginate($conditions));
$this->set('filter', $filter);
}

function admin_urlencode($id = null){
if($this->SeoUri->urlEncode($id)){
$this->Session->setFlash("uri Successfully Url Encoded.");
}
else {
$this->Session->setFlash("Erorr URL Encoding uri");
}
$this->redirect(array('action' => 'view', $id));
}

function admin_view($id = null) {
if (!$id) {
Expand Down
21 changes: 20 additions & 1 deletion models/seo_uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SeoUri extends SeoAppModel {
* @return true
*/
function beforeSave(){
//url encode the uri, but only once.
if(!empty($this->data[$this->alias]['uri']) && $this->isRegEx($this->data[$this->alias]['uri'])){
if(empty($this->data[$this->alias]['is_approved'])){
$this->data[$this->alias]['is_approved'] = false;
Expand All @@ -62,12 +63,30 @@ function beforeSave(){
/**
* Send need approval email if we need it.
*/
function afterSave(){
function afterSave($created){
if($created){
//Maybe URI
}
if(isset($this->data[$this->alias]['is_approved']) && !$this->data[$this->alias]['is_approved']){
$this->sendNotification(); //Email IT about needing approval... currently me.
}
}

/**
* Url encode the uri
* @param int id
* @return boolean success
*/
function urlEncode($id = null){
if($id){
$this->id = $id;
}
$uri = $this->field('uri');
$uri = rawurlencode($uri);
$uri = str_replace('%2F','/', $uri);
return $this->saveField('uri', $uri);
}

/**
* Named scope to find for view
* @param int id
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/models/seo_uri.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ function startTest() {
$this->SeoUri->Email = new MockEmailComponent();
}

function testUrlEncode(){
$uri = $this->SeoUri->findById(1);
$this->assertEqual('/blah', $uri['SeoUri']['uri']);
$this->assertTrue($this->SeoUri->urlEncode(1));
$result = $this->SeoUri->findById(1);
$this->assertEqual('/blah', $result['SeoUri']['uri']);

$uri = $this->SeoUri->findById(14);
$this->assertEqual('/uri with spaces', $uri['SeoUri']['uri']);
$this->assertTrue($this->SeoUri->urlEncode(14));
$result = $this->SeoUri->findById(14);
$this->assertEqual('/uri%20with%20spaces', $result['SeoUri']['uri']);
}

function testSetApproved(){
$this->SeoUri->id = 6;
$this->assertFalse($this->SeoUri->field('is_approved'));
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/seo_uri_fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class SeoUriFixture extends CakeTestFixture {
'created' => '2011-01-03 10:04:34',
'modified' => '2011-01-03 10:04:34'
),
array(
'id' => 14,
'uri' => '/uri with spaces',
'is_approved' => 0,
'created' => '2011-01-03 10:04:34',
'modified' => '2011-01-03 10:04:34'
),
);
}
?>
1 change: 1 addition & 0 deletions views/seo_uris/admin_edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('URL Encode', true), array('action' => 'urlencode', $this->Form->value('SeoUri.id')), null, sprintf(__('Are you sure you want to url encode # %s?', true), $this->Form->value('SeoUri.id'))); ?></li>
<li><?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $this->Form->value('SeoUri.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $this->Form->value('SeoUri.id'))); ?></li>
</ul>
</div>
Expand Down

0 comments on commit 5b1fd1e

Please sign in to comment.