Adding New Option In SweetCron

Ok, before we start, we need to know that, sweetcron’s option is saved into the database in the table named ‘option’.  The table consists of 4 columns which is option_id, option_name, option_value, and autoload. In order to add new option, we need to add new item in the table.

As an example, I wanted to add a new option to limit the no of latest blog post shown on the sweetcron page. So, I added a row with the following parameter:

  1. option_name=’latest_post_limit’
  2. option_value=10
  3. autoload=YES

After added the row on the database, we also needs to add some code in sweetcron.

As we already know, sweetcron is an application that uses the CodeIgniter framework which uses the concept of Model-View-Controller (MVC). So, in order to modify the sweetcron source code, we need to modify the controller and view of the option inside the admin folder. We do not need to modify the option model because, we did not modify the option table structure.

View

Location: sweetcron/system/application/views/admin/options.php

For the option view, we must add one more field inside the option’s form, where we want to input the option that we have added on the database. in my case, I added this code:

<p>
<label class=”title” for=”items_per_page_input”>Latest Item Limit</label>
<span class=”option_container”>
<span class=”option”><input id=”latest_post_limit_input” type=”text” name=”latest_post_limit” value=”<?php echo $this->config->item(‘latest_post_limit’)?>” size=”5″ /></span>
</span>
</p>

make sure the name of the field is the same with the option_name value, in my case, ‘latest_post_limit‘. Other than that, in order to make current option displayed when the option page is loaded, I also added a php line on the field’s value:

echo $this->config->item(‘latest_post_limit‘)

this line will  output the value of the specified option_name.

Controller

Location: sweetcron/system/application/controllers/admin/options.php

As For the Option Controller, we need to add a few more lines of code to make sure it will validate the input and save the option value into the database. In my case, I added the following code

if ($_POST) {

$this->load->library(‘validation‘);
//…………… other codes………….

$fields[‘latest_post_limit] = ‘Latest Post Limit‘;  //added code

$this->validation->set_fields($fields);
//…………… other codes………….

$rules[‘latest_post_limit‘]    = “numeric“; //added code

the key added for the array $fields and $rules must also be the same with the option_name.

After finished all the steps above, we have successfully added new option in Sweetcron. In order to acccess the newly created option, use the function

$this->config->item(‘option_name‘)

enjoy!!

Screenshot:

SweetCron With Latest Item Display Limit Option

SweetCron With Latest Item Display Limit Option

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s