Archive for category Open Source

Generate Crontab (Cron jobs) syntax/command using this simple tool

Create Crontab command using this simple tool

Create Crontab command using this simple tool





Cron is a daemon used for scheduling tasks to be executed at a certain time. You can easily find great articles on Crontab or Cron daemon on the Internet. I found these articles very useful and easy to understand.

1. https://help.ubuntu.com/community/CronHowto
2. http://en.wikipedia.org/wiki/Cron

Actually, here I don’t want to explain how crontab work and how you can schedule tasks using crontab. I just want to discuss this tool I’ve created for generating crontab entries/syntax which you can copy and directly put in your crontab file.

In this tool there are five sections; minute, hour, day, month and weekday. You can select single or multiple values for each section from the list which you want to use in your crontab file.

You may want to run a script some number of times per time section. For example if you want to run crontab job in every 10 minutes (runs on minutes divisible by 10: 0, 10, 20, 30, etc.). In this case you can use “Every (x) minute” option and select 10 from drop down list. You can also get the correct output from “Minutes(s)” list but this option will generate less cumbersome output.

In “Command” text box you need to enter full command you want to execute in crontab. When you click Submit you will see this command with values for different section.

There should be single space between each section with the final section (command) can having one or more spaces in it.

, , ,

2 Comments


Create Drupal form using theme_table() like module list form (Part II)

This post is the second part of Create Drupal form using theme_table() like module list form post. In first part I have showed how we can create Drupal form like we have in Module list pages. In this post, I am showing how we disable some of the checkboxes so user can’t select them like we have for core modules in module list page. We can’t enable/disable core modules in module list.

Drupal Form using theme_table() function (Part II)

Drupal Form using theme_table() function (Part II)

Here I am just showing the changes which we require in code of Part I post.

 $form['featured'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $status,
);
 
// Change this to 
 
$form['featured'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $status,
    '#process' => array(
        'expand_checkboxes',
        'featured_disable',
        ),
    '#disabled_products' => $disabled,
);

In above code “$disabled” is an array containing the productid’s of all the products which we want to show as checked and in disable state. The structure of “$disabled” array is similar to “$status” array. We stored this array in “#disabled_products”. We also added “#process” in above code.

“expand_checkboxes” and “featured_disable” are function names which Drupal call when create and render these checkboxes. “expand_checkboxes()” is defined in Drupal core files and we don’t need to define it again.

function featured_disable($form, $edit) {
  	foreach ($form['#disabled_products'] as $key) {
  		$form[$key]['#attributes']['disabled'] = 'disabled';
  	}
 
  	return $form;
}

“featured_disable()” function is very important here. Drupal call this function when renders these checkboxes. Here we assigned “disabled” attribute to each checkbox which we want as disable.

, ,

4 Comments


Add and show attachments in Joomla Article

Step1: 
         download this extension from the joomla webiste link is:
         <a href="http://extensions.joomla.org/extensions/3115/details"> Attachment </a>
 
Step2: 
         Unzip this extension and read the "INSTALL.txt" to how to install or upgrade this attachment.
 
Step3:
        A).
 
       To show attachments for all users, change some setting from the admin,
        go to: 
               1).  admin-&gt;components-&gt;article attachment then click on "parameters" 
                     look for  "Who can see attachments?" select "Anyone"
 
       B).
 
       To show attachment while article listing.
        1).
           file name :		
			\components\com_content\views\category\view.html.php
            Line no:
			after 157
	    add
			1:	$dispatcher	=&amp; JDispatcher::getInstance();	
 
	   Line no:
			after 191		
	   add
			2:				
				JPluginHelper::importPlugin('content');
				$results = $dispatcher-&gt;trigger('onPrepareContent', array (&amp; $item, &amp; $item-&gt;params, 0));	
				$item-&gt;event-&gt;afterDisplayContent = trim(implode("\n", $results));
 
      2).
           file name : \components\com_content\views\category\tmpl\default_items.php
        	Line no:
			where ever u want to show attachment file link
 
		add
			echo $item-&gt;text;

,

2 Comments


Create a sortable table using “theme_table()” function in Drupal

Sortable table using theme_table() function

Sortable table using theme_table() function


We can create a sortable header using “theme_table()” function. In sortable header user can sort a column in ascending or descending order. I am using a table called “package_coupon“. This table have seven fields (coupon, operator, operand, created, expire, used and user_by). We are providing the sorting facility on four fields only (coupon, created, expire and used). Initially, the coupon field is sorted by ascending order.

Here “tablesort_sql()” function is important. This function produces the ORDER BY clause to insert in your SQL queries, assuring that the returned database table rows match the sort order chosen by the user.

Code:-

function package_coupon_list() {
	$head = array(
		array('data' => t('Coupon'), 'field' => 'coupon', 'sort' => 'asc'),
		array('data' => t('Type of Discount')),
		array('data' => t('Created'), 'field' => 'created'),
		array('data' => t('Expire'), 'field' => 'expire'),
		array('data' => t('Status'), 'field' => 'used'),
		array('data' => t('Used by')),
	);
 
  	$sql = "SELECT * FROM package_coupon" . tablesort_sql($head);
 
   	$result = db_query($sql);
 
  	while ($coupon = db_fetch_object($result)) {
		$rows[] = array(
			array('data' => _coupon_format($coupon->coupon)),
			array('data' => ($coupon->operator == '%' ? "{$coupon->operand}% discount" : "\${$coupon->operator}{$coupon->operand} discount")),
			array('data' => format_date($coupon->created, 'small')),
			array('data' => format_date($coupon->expire, 'small')),
			array('data' => $coupon->used . ' left'),
			array('data' => ($coupon->user_by == '') ? 'None' : $coupon->user_by),
		);
  	}
 
	return theme_table($head, $rows);
}

8 Comments


Create an action confirm form using “confirm_form()” function in Drupal

Doctor's List with delete option

Doctor's List with delete option


Action confirm form in Drupal

Action confirm form in Drupal

We can easily create an action confirm form using confirm_form() function in Drupal. Normally we need this form when user want to delete something and we want user to confirm his/her action. I am showing here an example in which we are listing all the Doctors information with Edit and Delete option. When user click the delete link we will show him the action confirm form with “Delete” and “Cancel” options. If user click the “Delete” button we will delete that Doctor record from database and if user click on “Cancel” we simply show the doctors list again.

Here is the code. I am using the module name “doctor” in this example.

Step 1. First implement hook_menu() to register new path in Drupal.

function doctor_menu() {
  $items = array();
 
  $items['doctors'] = array(
    'title' => t('Doctors'),
    'page callback' => 'doctors_list',
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  $items['doctors/delete/%doctor_user'] = array(
    'title' => t('Delete doctor'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doctor_delete_confirm', 2),
    'access arguments' => array('access doctor'),
    'type' => MENU_CALLBACK,
  );
 
  return $items;
}

Step 2. Implementing the doctors_list() function to display the list of doctors.

function doctors_list() {
	$header = array(t('Doctor Name'), t('Gender'), t('Phone No.'), t('Status'), t('Action'));
 
	$query = "SELECT * FROM {doctor}";
	$rs = db_query($query);
 
	$row = array();
 
	if ($rs) {
		while ($data = db_fetch_object($rs)) {
			$gender = $data->gender == 'M' ? t('Male') : t('Female');
			$status = $doctor->status ? t('active') : t('inactive');
			$row[] = array(stripslashes($data->firstname) . ' ' . stripslashes($data->lastname), $gender, stripslashes($data->phoneno), $status, 
			"<a href='/doctors/edit/{$data->doctorid}'>" . t('Edit') . "</a> | <a href='/doctors/delete/{$data->doctorid}'>" . t('Delete') . "</a>");
		}
	}
 
	$str .= theme_table($header, $row);
 
	return $str;
}

Step 3. Create a wildcard loader function doctor_user_load() which we used in “doctors/delete/%doctor_user” path. This function checks the given “ID” in path is valid or not. If the given doctor id is not exists in database then “Page not found.” message will display.

function doctor_user_load($doctorid) {
	$query = "SELECT * FROM {doctor} WHERE doctorid = %d";
	$rs = db_query($query, $doctorid);
 
	if ($rs) {
		while ($data = db_fetch_object($rs)) {
			return $data;
		}
	}
 
	return FALSE;
}

Step 4. Create the doctor_delete_confirm() function. We are using the “confirm_form()” function in this function to show action confirm form.

function doctor_delete_confirm(&$form_state, $doctor) {
	$form['_doctor'] = array(
		'#type' => 'value',
		'#value' => $doctor,
	);
 
	return confirm_form($form,
    	t('Are you sure you want to delete this doctor?'),
    	isset($_GET['destination']) ? $_GET['destination'] : "doctors",
    	t('This action cannot be undone.'),
    	t('Delete'),
    	t('Cancel'));
}

Step 5. Create the Form API submit function for doctor_delete_confirm form.

function doctor_delete_confirm_submit($form, &$form_state) {
	$form_values = $form_state['values'];
 
	if ($form_state['values']['confirm']) {
		$doctor = $form_state['values']['_doctor'];
 
		doctor_delete($form_state['values'], $doctor->doctorid);			
 
		drupal_set_message(t('Doctor has been deleted successfully.'));
  	}
 
	drupal_goto("doctors");
}

, ,

15 Comments