Thursday, April 22, 2010

Create an Adsense widget in 5 minutes

So now that we have seen some basic text output with Pyrocms Widgets, now we are going to explore user input/output with our second Widget. What Pyrocms lacks is a good Ad-sense widget, so that we can make some money out of our blog. So without wasting anymore time, we are going to create one.

If you are not interested with the inner workings, then you can proceed to  download the widget.

Before we start coding let us plan how our widget will work. After installing our widget, the user will be presented with a text area where he will input his adsense script. When he will click save, that adsense script will be stored within Pyrocms and output whenever requested.

So to begin, inside our Widget folder, we create a folder and call it adsense. Inside adsense, we create adsense.php file. Here is the content of our adsense.php file:

<----------CODE STARTS HERE---------->
 
public $fields = array(
   array(
  ' field' => 'js',
  ' label' => 'Adsense',
  ' rules' => 'required'
   )
 );
 

<----------CODE ENDS HERE---------->

Note that we have omitted the widget $title, $description, $author, $website and $version variables to make the code look more redeable. If you want to take a look at them, look at my previous blog on how to create a widget in 5minutes. So in the above, we are setting the necessary validation checks on our Form. When the user is presented with the text area, if nothing is input, a message will appear, telling the user that the field js with label “Adsense” is required.  

Notice that we are introduced to the $fields api for the first time here. Basically, $fields just tell Pyrocms the number of fields the widget will present to the user and if any form of validation must be performed on them.
Second, we need to process the input of the user:

<----------CODE STARTS HERE---------->
 
public function run($options)
{
   if(empty($options['js']))
   {
     return array('output' => '');
   }

//store the adsense js script
return array('output' => "");
}
<----------CODE ENDS HERE---------->
 
Next, we are introduced to the run() function. We will see this function many times during our development. Basically, the run()  function will do the core processing of our widget and output any result as necessary. The problem that I had with the adsense widget is that Pyrocms does not allow any script tags in its code. So after consulting with Philsturgeon, we decided to hard code our script tags inside our run() function. The most important part is the return array where we are telling Pyrocms to store our adsense script inside its code, here known as output.  

Next we create our views folder and inside it, we create two new files: display.php and form.php. Display.php will only contain < ? ph p echo $output; ?> , telling Pyrocms to print the data it has stored for us. Inside our form.php, we insert the following code:

 <----------CODE STARTS HERE---------->

< ? ph p echo form_textarea(array('name'=>'js', 'value' => $options['js'])); ? >
 

 <----------CODE ENDS HERE---------->

 
This is just the form that the user will be presented with when he has installed our widget. Most of the elements are self descriptive. Notice that I have asked the user to paste only specific parts of their adsense code. Then we see the text area that will take user input and send it to our run function. Notice here that $options['js'] is the field that will store our js code.

OK two things, most of what you are seeing here is UNDOCUMENTED. There is really no written documentation of how the widgets system works in Pyrocms and no one has been helping me with it. So, I had to break open the other widgets, analyze and make my own conclusions. Even I do not understand most of these codes. So with these, I encourage you to explore more and discover by yourself.



Wednesday, April 21, 2010

Pyrocms: create a widget in 5 minutes:

Today, I am going to show you how to create as simple Lorem Ipsum dummy text widget. This widget, when placed any where, will produce a paragraph of dummy text. This is essential if you are creating lots of pages and want an idea how the text will look in relation to other elements on the page.

So to get started, download the latest Pyrocms package and extract to your http folder. In Pyrocms, all the widgets are stored in the application/widgets folder. There are many other widgets there for you to explore. 
  1. First we will need to create a folder for our widget. That folder will store all our widget files. Note that the naming of the folder is very important. More on that later, for the moment, we just create a folder and name it lorem_ipsum.
  2. Inside our folder, we fist create our main widget file. We will call it: lorem_ipsum.php. Notice how we are keeping the same naming convention. Our folder, main widget file and later our class name would have the same name. If anytime you receive any kind of error, ensure that these three parts have the same name. Inside our widget we will put the following information:
    <---------FILE STARTS HERE----------->
    class lorem_ipsum extends Widgets
    {
    //The following information will appear when you will install the widget
    public $title = 'Lorem ipsum';
    public $description = 'Puts default Lorem ipsum text.';
    public $author = 'Mahen';
    public $website = 'http://cipyrocms.blogspot.com/';
    public $version = '1.0';
    }

    <---------FILE ENDS HERE----------->
    This is all we need for a basic widget. Notice that no function is defined here, we are just showing some information to the user when he will install the widget.
  3. Next, we create a folder inside our lorem_ipsum and name it as views. This will contain the main functions that will be outputed to the browser.
  4. Inside our views folder, create a file called display.php and insert the following information: 
      < ? ph p echo “hello world”; ? >
    where we replace hello world with our default lorem ipsum text....
  5. We are now technically ready to install our widget. So in Pyrocms's widget screen, do a refresh and if everything is fine, our Lorem Ipsum widget will appear under Un-installed
  6.  Next, we click and install our widget. After install, our widget will receive a unique instance ID, usually denoted by the # tag. Once installed, we need to tell Pyrocms where to display our widget.
  7. Under the pages section, we edit the page under which we want to add our Lorem Ipsum widget, for our example, am going to use our homepage.
  8. Finally we add {widget_instance(3)} where 3 is the instance ID of our Widget. If everything went on well, our Lorem Ipsum text should appear on the homepage 
So this is all for Widgets. Note that we have barely scratched the surface here. If you want to develop your own Pyrocms Widgets, I encourage you to explore the other widgets and learn from them. Thats it for today, check often for newer updates.  

You can download my Widget here and explore it.

Tuesday, April 20, 2010

Pyrocms ver. 0.9.8-rc2 now avaliable:

Pyrocms RC2 Version is now avaliable for download. If you just downloaded the RC1 and you want to upgrade to RC2 then follow these simple steps:

1. Go to docs and read the UPGRADE doc. Basically you just need to update some settings in your databse.
2. Then go to yoursite.com/upgrade. This will finalize the upgrade script. If everything went on fine, you should receive a message saying: Upgraded to 0.9.8-rc2
 3. Now you are successfully upgraded.

This may be the final RC before the final release of Pyrocms.....Wait and see

Codeigniter changes from Model to CI_Model

Codeigniter 2.0 is comming up, although we are still months from the final release, the development is still moving full steam ahead. Accordingly, the latest changes to CI 2.0 was the change from extending your class from Model to CI_Model.



If you download the latest alpha builds from svn servers, you will notice that if you declare your class like this:



class pagemodel extends Model {}

in CI 2.0, it will generate a Fatal Error, complaining that the Model class has not been defined. What has happened is that they are changing the naming from Model to CI_Model, so this works perfectly:





class pagemodel extends CI_Model {}



Now there are discussions internally and externally that if the naming of the Model class is changed, then the naming of the Controller also must be changed to CI_Controller:



"changing the Controller to CI_Controller is one of the things we're discussing. It's a bit of find and replace, and an x.0 is the correct time to make those decisions. Honestly, we haven't fully decided yet; there are pros and cons to each."



Apparantly this change has been made so as to provide more consistency in the Library and to free up more Reserved_names:



"brings that library into consistency with every other CodeIgniter class. It keeps the architecture consistent and frees up key words for your use. In fact, Controller is likely to change to CI_Controller as well."



The latest build does seem to take the latest CI_Model changes, but nothing for the CI_Controller. Guess we will just have to wait and see....