PyroCMS Widget Creation for Affiliate Marketing – Section 1
8:51 AM in CSS, Frameworks, jQuery, PHP, Programming, PyroCMS by Vic Russell
PyroCMS is a very powerful and capable PHP-based CMS. It is authored on top of the Codeigniter framework. It is very fast, and scalable and very promising as a powerful PHP framework.
Download the affiliate.zip file here
Caveat: The documentation for PyroCMS (PCMS) is very sparse. Any answer to any question can be exceedingly difficult to find. Check back often for additional links to developing documentation.
One of the new and useful features of PyroCMS is the addition of Widgets. Now, we all know what a widget is, right? Actually, in the PCMS world, the concept of widget is very accurate – it is a discrete entity (in this case, a class) that can be added to a page, section of a page (right sidebar, left sidebar, header, footer, content, etc) that can access the database as well as execute custom code and present content that you create, in a format you define. You can use this to display images, text, and any other content you decide is needed – EXCEPT for JavaScript.
Let me clarify the javascript problem: if you want to create a widget that allows you to enter javascript snippets, it will not work – the <script> tags as well as any document.write() methods are stripped out for security reasons and replaced with [removed]. I am searching for a workaround, but given the above statement regarding documentation, this might take some time. [ note: The feature within the Codeigniter (CI) form_helper class that sanitizes strings is xss_clean and prep_for_form ]. Until then, I am adding the needed JavaScript in the class and view files.
There are adequate installation documentation and install examples on the web for installing PyroCMS and Codeigniter. Here are a couple.
Installing Codeigniter (this is link is provided only for reference - you do not need to download and install CI as PyroCMS is built on top of CI)
It is critical that you define your database connection accurately, and remember your default user name and password.
Our Platform
I am using a Dell laptop with 2 GB of RAM and a dual core Athlon X2 processor (circa 2006) with WAMP (Windows Apache MySQL PHP) installed for development, PHP Designer 7 as the IDE (Eclipse is too heavy, but would work with a more powerful machine). WAMP is a very easy install.
All of my web files are under C:\wamp\www\ - which is the default web root for a typical WAMP installation.
Lets create a widget in PyroCMS - 'affiliate'
There is no widget named affiliate that comes with your installation. Therefore, any reference to a file or folder/directory implies you have added it or should add it.
Navigate to the widgets directory under the application directory
- (note: I am a Linux developer first - some of my paths will have the forward Unix-style slash ( / ) rather than the backwards Windows-style slash ( \ ). Make adjustments where needed if I forget to use Windows style slashes)
C:\wamp\www\pyrocms_directory\application\widgets\affiliate
First, create the directory within the ...\widgets\ path called affiliate.
Copy the code from this link into a download folder, extract the files, then copy the the files into the above path. You will copy the 'views' directory and the affiliate.php file into the affiliate directory (see folder screenshot).
Download the affiliate.zip file here
Within the 'views' directory, you will have two files: display.php and form.php. These files, and the containing 'views' folder, will be present in every PyroCMS widget you create - these files are required and necessary.
- The first file you edit (or create, depending on your need) is the class file. Remember to capitalize the name of the class - which must be the same name as the file (less the .php extension
).
Here is a bare affiliate.php file after editing to add the fields needed for the new widget:
<?php
if(!defined('BASEPATH')) exit('No direct script access permitted');
class Affiliate extends Widgets
{
// This section will appear when you click on the widget on the 'widgets' page of the admin
// interface (aka dashboard). You access the admin interface using the following URL:
// http://your-host/your-pyrocms-base-dir/admin
public $title = 'Affiliate Widget';
public $description = 'Add Affiliate Links and Images';
public $author = 'Vic Russell';
public $website = 'http://www.randaweb.com';
public $version = '0.3';
// Now you define the fields that you will use in your widget.
// The widgets class will contain a 'name' field, so it is optional here
public $fields = array(
array(
'field' => 'aff_title',
'label' => 'Product Title',
'rules' => 'trim|max_length[254]'
),
array(
'field' => 'aff_order',
'label' => 'Add Order - 1 = new line',
'rules' => 'trim|max_length[3]|integer'
),
array(
'field' => 'aff_link',
'label' => 'Affiliate Link',
'rules' => 'required|trim'
),
array(
'field' => 'aff_alt',
'label' => 'Affiliate Alt Text',
'rules' => 'trim|prep_url|max_length[254]'
),
array(
'field' => 'aff_class',
'label' => 'CSS Class',
'rules' => 'trim|max_length[254]'
),
array(
'field' => 'aff_style',
'label' => 'CSS Style',
'rules' => 'trim'
),
array(
'field' => 'aff_source',
'label' => 'Affiliate Source)',
'rules' => 'trim|max_length[254]'
),
array(
'field' => 'aff_company',
'label' => 'Company Source for add',
'rules' => 'trim|max_length[254]'
)
);
public function run($options)
{
// The requisite 'run()' method that returns the data in the $output[] array //
if(empty($options['aff_title']))
{
return array('output' => '');
}
return array('output' => array($options['aff_title'], $options['aff_link'],
$options['aff_alt'], $options['aff_class'], $options['aff_style'],
$options['aff_source'], $options['aff_company'] ) );
}
}
/* Close class Affiliate */
