Tags: tutorial

Auto Create Slugs in Croogo with jQuery

Posted by Andrew on November 22nd 2009, 11:55pm

I got tired of hand writing slugs for my blog posts, I am used to using the acts_as_sluggable behavior. I thought about implementing the behavior into Croogo but realized this might be better implemented as some simple Javascript. Initially I wrote a simple find and replace function with Javascript that converted spaces to hyphens but realized it didn't take into account special characters or more than one space so I did a quick search on Google and came across the jQuery Slug Plugin. It took me about 2 minutes to download and implement.

All you need to do is download the plugin and place it into your webroot/js directory and then modify a few lines in app/views/nodes/admin_add.ctp. The changes I made are below.

$javascript->link(array('nodes', 'jquery.slug'), false);

Include the jQuery slug plugin at the top of the page.

<script type="text/javascript">
$(document).ready(function(){
$("#title").slug({
slug:'permalink',
});
});
</script>

Set up the plugin and let it know the title field's ID and the class of the slug field.

echo $form->input('title', array('id' => 'title'));
echo $form->input('slug', array('class' => 'permalink'));

Add an ID to the title field and a classname to the slug field. That's about it, hope this saves you some time when you are writing your next article.

Bookmark and Share
Posted in Croogo | Leave a comment

Using CakePHP and RPX for Social Authentication

Posted by Andrew on November 21st 2009, 10:24pm

I am working on a social network application where I want to allow users to import their information from another social network or service that provides an open authentication. I looked at Facebook connect and Twitter sign on but after doing research the easiest solution for me was using RPX's hosted solution. It took me about 10 minutes to implement and maybe another hour to fine tune to my needs.

To start you will need to register with RPX and then set up your widget. Some of the providers (such as Facebook and Twitter) need you to set up the application on their network first and then they will provide you with an API key that you can then plug into RPX.

After setting up the widget they will provide you with some code to copy and paste, there really isn't any modifications needed to the code except setting the token_url which is used as a redirect url once the user has been authenticated. You will probably want to set the token_url to a registration page where the user can enter a username and password for CakePHP's authentication component.

Once you have everything in place you will need to set up some code for the action that corresponds with the token_url. First you will need to receive the token from RPX used to retrieve the user's data, then you will need to request the user's info using their API. Here is the code that I am using in my action.

App::import('Core', 'HttpSocket');
$http = new HttpSocket();

$token = $this->params['form']['token'];
$results = $http->post(
'https://rpxnow.com/api/v2/auth_info',
array(
'token' => $token,
'apiKey' => 'YOUR-API-KEY-HERE'
)
);
$user = json_decode($results);

I am using CakePHP's built in HttpSocket class to request the user's information. The information sent from RPX is in JSON format so I used PHP 5's built in json_decode method to create an object out of the json. Now you would most likely create a new user record and save the information received from RPX and in the view display a form to collect the rest of the user's credentials such as username and password.

That's it, it's very simple to use. If you have any questions leave a comment and I will get back to you as soon as possible.

Bookmark and Share
Posted in CakePHP | 4 Comments

Creating Featured Posts for Croogo CMS

Posted by Andrew on November 15th 2009, 7:02am

In this post I will describe how I created the featured post on the home page of my blog with Croogo. Looking at the database schema I noticed that there was a column named "sticky" that was not being utilized anywhere in the code so I thought that it would be a good place to store whether a post was featured or not. The first thing I did was edit the admin add and edit views to allow me to mark posts as featured under the publishing tab.

<div id="node-publishing">
<?php
    echo $form->input('sticky', array('label' => __('Featured', true)));
    echo $form->input('status', array('label' => __('Published', true)));
    echo $form->input('promote', array('label' => __('Promoted to front page', true)));
    echo $form->input('created');
?>
</div>

All I did here was add one line that will display a checkbox similar to the published and promoted checkboxes. The next thing I had to do was retrieve the latest featured post from the database. I did this in the nodes controller under the promoted action since this is the default home page for Croogo.

$featured = $this->Node->find('first', array(
    'order' => 'Node.created DESC',
    'conditions' => array('Node.sticky' => true)
));
$this->set('featured', $featured);

This just finds the first post marked as featured, ordered by the created date. I then set a variable for the view so that I can access this featured post in my view. The next step was displaying the featured post. For this I created a "featured" element and rendered it in the layout template if the current action was "promoted".

<? if ($this->action == 'promoted') : ?>
<?=$this->element('featured')?>
<? endif ?>

Inside of featured.ctp in the elements directory.

<p><?=$html->link($featured['Node']['title'], $featured['Node']['url'], array('class' => 'link'))?><p>
<p><?=$text->trim(strip_tags($featured['Node']['body']), 200, '...', false, true)?></p>
<p><?=$html->link('Read More...', $featured['Node']['url'])?></p>

This is a dumbed down version of what I am using but should still get you started. If you know your way around CakePHP you should be able to understand what is happening. I am using the text helper to show an excerpt from the post body then display a read more link below that will direct the user to the post.

This should be enough to get you started. In my next post I will explain how to use custom fields in your posts. I am using them to display the image beside my featured posts on the home page. If you have any questions leave a comment and I should get back to you shortly.

Bookmark and Share
Posted in Croogo | 1 Comment