ChronoScripts JobberBase Scripts and (PHP) Support

11Aug/097

Guide on adding new job-fields

This guide will explain how you can add custom fields to the job creation page. It requires basic PHP and HTML knowledge. I'll write another guide on how to add fields to the apply-page later, which is bit easier to follow than this guide.

For this example I added two new fields, which will hold the amount of hours per week a job will be (in the format of "32 to 40 hours per week"). You can use the examples to add any type of field to the form / db of course.

Step 1. Adjusting the DataBase
First we need to add the fields to the jobs table using PhpMyAdmin or a similar tool. As both fields in this example will contain only numbers, we use 'int' as type. In most text variables it's safe to use varchar(255), however.

ALTER TABLE `jobs` ADD `hourfrom` int(4) DEFAULT NULL;
ALTER TABLE `jobs` ADD `hourto` int(4) DEFAULT NULL;

Step 2. Changing the Job Class
Now comes the real work. The Job Class (_includes/class.Job.php) handles the loading and saving of information from the Database, and getting that data to the PHP pages.

To start, we need to add our new fields as class variables, a list found at the top of the file with code similar to "var $mVariable = false;". Notice the naming convention, instead of '-' we use capitals and we always start with a 'm'.

var $mDaysOld = false;

add below:

var $mHourFrom = false;
var $mHourTo = false;

Next we need to adjust the main query, which loads all the fields from the database for further use. Make sure you don't break the query by missing a , or placing your fields at the wrong location. To be sure, add your fields behind "a.apply AS apply,".

a.apply AS apply,

add behind:

 a.hourfrom AS hourfrom, a.hourto AS hourto,

For the just loaded data to be loaded for further use by the different functions, we need to add it to the $this-> list. It's a bit below the query.

$this->mDaysOld = $row['days_old'];

add below:

$this->mHourFrom = $row['hourfrom'];
$this->mHourTo = $row['hourto'];

Now we need to add the variables to the different functions. There's 3 by default (GetInfo, GetBasicInfo, GetBasicInfoAdmin). If unsure, just add the variables to all three. Note that if your new variable is the last on the list, it'll need to end with a ) instead of a comma. If you want to be sure, add it below "'days_old' => ..".

'days_old' => $this->mDaysOld,

add below:

'hourfrom' => $this->mHourFrom,
'hourto' => $this->mHourTo,

The last step to take in the class.Job.php, is adding the fields to the Create and Update functions, so it can be saved in the database later. Look for "public function Create($params)" and find the following structure:

if ($params['city_id'] == '')
{
$params['city_id'] = -1;
}

add below:

if ($params['hourfrom']) == '')
	{
		$params['hourfrom'] = 0;
	}
 
if ($params['hourto']) == '')
	{
		$params['hourto'] = 0;
	}

This simply sets the fields to 0 if they're not given. We will also need to adjust the query itself. The easiest way to do this is to add your variable before 'created_on':

company, city_id, url, apply,

add behind:

hourfrom, hourto,

This was just the fieldname though, we need to add the real variable below. Make sure it's in the right position. If you added the fieldname before 'created_on', you'll simply have to add your variable before 'NOW()'.

NOW(), ' . $params['is_temp'] . ', '. $params['is_active'] .', 0, "' . $this->GenerateAuthCode() . '",

add above:

' . $params['hourfrom'] . ',
' . $params['hourto'] . ',

The sql query for the update() function works a bit different, but the first bit is the same as above (you should recognize the code). To keep it simply, just add your info below apply = "", both the fieldname and value are given at the same row. So:

apply = "' . $params['apply'] . '",

add below:

hourfrom = ' . $params['hourfrom'] . ',
hourto = ' . $params['hourto'] . ',

Step 3. Time to update the form that handles new jobs and edits them.
The template file for the form is found at: "_templates/publish-write.tpl". In this example I add the field to a new fieldset (a frame that holds multiple fields, usually), just above the the fieldset that holds the create and edit buttons.

<fieldset><input type="submit" name="submit" id="submit" value="{$translations.publish.step1_submit}" /></fieldset>

add above:

<fieldset>
	<legend>Hours per week</legend>
	<table border="0" cellspacing="2" cellpadding="2">
		<tr>
			<td class="publish-label" valign="top">{$translations.publish.hour}:</td>
			<td>
				<input {if $errors.hoursfrom}class="error"{/if} type="text" name="hourfrom" id="hourfrom" tabindex="6" size="5" maxlength="10" value="{if $job.hourfrom}{$job.hourfrom}{else}{$smarty.post.hourfrom}{/if}" />
				to
				<input {if $errors.hoursto}class="error"{/if} type="text" name="hourto" id="hourto" tabindex="6" size="5" maxlength="10" value="{if $job.hourto}{$job.hourto}{else}{$smarty.post.hourto}{/if}" />
				per week
			</td>
		</tr>
	</table>
</fieldset>

I didn't use the translation file, but that should be an easy adjustment later. Also note I included some code for error-messages.

Step 4. Updating the PHP file that's used for the form-submitting.
We also need to update the PHP file to actually do something with the form variable. For this we open page_write.php. There are two relevant parts, one that updates and one that creates. Both need the same steps, so I'll only explain one.

First add validation for the field (if needed). First find the following check:

if ($poster_email == '')
{
	$errors['poster_email'] = $translations['jobs']['email_error'];
}

Now add your field below it, in the same fasion, if it needs validation to begin with. In this example we disallow non-numeric input, but we do allow empty fields.

if ($hoursfrom != '' && !is_numeric($hoursfrom))
{
	$errors['hoursfrom'] = 'Your error message;
}
if ($hoursto != '' && !is_numeric($hoursto))
{
	$errors['hoursto'] = 'Your error message;
}

After validation, add it to the "$data = array(..." list a bit further below. Again, if you add your field to the end of the list, make sure it ends with a ) instead of a comma.

'is_active' => 0,
'apply' => '',

add below:

'hourfrom' => $hourfrom,
'hourto' => $hourto,

Do these last few steps twice (as there's two occurrences), and we're almost there!

Step 5. Lastly we need to add our new field somewhere in the job details page.
Open "_templates/job-details.tpl". In this example we simply add it directly below the {job.$description}:

{if $job.hourfrom != "" && $job.hourfrom != 0}<p><strong>{$job.hourfrom}{if $job.hourto != "" && $job.hourto != 0}~{$job.hourto}{/if} hours per week.</strong></p>{/if}

This code simply tells SMARTY to only display the 'hourfrom" field when it's actually set, and does the same for the hourto. Again you should use the translations file for the text, but I won't explain that here.

That's all for now, I'll probably update a bit later on, as it's a quick draft.

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon
Filed under: Blog News Leave a comment
Comments (7) Trackbacks (0)
  1. Hello!

    great tut – I found a (little) bug:
    if ($params['hourfrom']) == ”)
    {
    $params['hourfrom'] = 0;
    }

    if ($params['hourto']) == ”)
    {
    $params['hourto'] = 0;
    }

    error 500 –>
    if ($params['hourfrom'] == ”)
    if ($params['hourto'] == ”)

    well – but it dont work – at least a get a publish error 500
    would you please be so nice an publish the changed files:
    class.Job.php
    publish-write.tpl
    page_write.php
    job-details.tpl

    that would be great :-)
    thank´s for your fine work

    regards
    wolkenlos

  2. Looks like a great tutorial again! Good job!

  3. How ’bout adding a logo to the post form?

  4. Values are not adding to the database. For verify button it will show a message “Please revise the form…” . when i leave the new field blank , there is no error. But database will take the value zero..

  5. Hi.
    This guide dosn’´t work with v1.8

    I cant find this sting in class.job.php:
    if ($params['city_id'] == ”)
    {
    $params['city_id'] = -1;
    }

    In v1.8 class.job.php it looks like this:
    if ($params['city_id'] == ” || $params['city_id'] == 0)
    {
    $params['city_id'] = ‘NULL’;
    }

    I get a error like this when I am trying to edit it in admin zone:

    Notice: Undefined index: price in /home/w11002/domains/tovas.se/public_html/_includes/class.Job.php on line 947

    Warning: Cannot modify header information – headers already sent by (output started at /home/w11002/domains/tovas.se/public_html/_includes/class.Job.php:947) in /home/w11002/domains/tovas.se/public_html/_includes/function.redirect_to.php on line 30

    Please help me!! :)

  6. Hi,

    Thanks for the tutorial but it doesn´t work for me either. Would it be possible to have access to already modified files.

    Thanks in advance,

  7. Appreciate the tutorial.

    Chronos I reaaaaally need this to work as well as these other guys. Awesome tutorial but it does in fact not work with 1.8 for reasons posted above by Vid, kanaka, and Thommy. Do you have any plans to fix this or are we all out of luck here? Please please please =)


Leave a comment


No trackbacks yet.