Archive for September, 2009

>Write Module in Drupal

Posted: September 16, 2009 in Drupal

>//$id$

function form_help($path, $arg) {
$output = ”; //declare your output variable
switch ($path) {
case “admin/help#form”:
$output = ‘
‘. t(“Write CRUD functionalities”) .’
‘;
break;
}
return $output;
}

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/
function form_perm() {
return array(‘access form content’);
}

/**
* Implementation of hook_block
* @param string $op one of “list”, “view”, “save” and “configure”
* @param integer $delta code to identify the block
* @param array $edit only for “save” operation
**/
function form_block($op = ‘list’, $delta = 0, $edit = array()) {
if ($op == “list”) {
// Generate listing of blocks from this module, for the admin/block page
$block = array();
$block[0][“info”] = t(‘On CRUD FORM’);
return $block;
}

else if ($op == ‘view’) {

// Generate our block content

// Get today’s date
$today = getdate();

// calculate midnight one week ago
$start_time = mktime(0, 0, 0,
$today[‘mon’], ($today[‘mday’] – 7), $today[‘year’]);

// we want items that occur only on the day in question, so
// calculate 1 day
$end_time = time()/*$start_time + 86400*/;
// 60 * 60 * 24 = 86400 seconds in a day

$limitnum = variable_get(“form_maxdisp”, 3);

$query = “SELECT nid, title, created FROM ” .
“{node} WHERE created >= %d ” .
“AND created <= %d”; $query_result = db_query_range($query, $start_time, $end_time, 0, $limitnum); $block_content = ”; while ($links = db_fetch_object($query_result)) { $block_content .= l($links->title, ‘node/’. $links->nid) .’
‘;
}
// check to see if there was any content before returning
// the block view
if ($block_content == ”) {
// no content from a week ago
$options = array( “attributes” => array(“title” => t(“More events on this day.”) ) );
$link = l( t(“more”), “form”, $options );

$block_content .= “

” . $link . “

“;
$block[‘subject’] = ‘CRUD FORM’;
$block[‘content’] = ‘Sorry No Content’;
return $block;
}

// set up the block
$block = array();
$block[‘subject’] = ‘CRUD FORM’;
$block[‘content’] = $block_content;
return $block;
// more coming…
}

}

function form_admin() {
$form = array();

$form[‘form_maxdisp’] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘Maximum number of links’),
‘#default_value’ => variable_get(‘form_maxdisp’, 3),
‘#size’ => 2,
‘#maxlength’ => 2,
‘#description’ => t(“The maximum number of links to display in the block.”),
‘#required’ => TRUE,
);

return system_settings_form($form);
}

function form_admin_validate($form, &$form_state) {
$maxdisp = $form_state[‘values’][‘form_maxdisp’];
if (!is_numeric($maxdisp)) {
form_set_error(‘form_maxdisp’, t(‘You must enter an integer for the maximum number of links.’));
}
else if ($maxdisp <= 0) { form_set_error(‘form_maxdisp’, t(‘Maximum number of links must be positive.’)); } } function form_menu() { $items = array(); $items[‘admin/settings/form’] = array( ‘title’ => ‘On this date module settings’,
‘description’ => ‘Description of your On this date settings page’,
‘page callback’ => ‘drupal_get_form’,
‘page arguments’ => array(‘form_admin’),
‘access arguments’ => array(‘access administration pages’),
‘type’ => MENU_NORMAL_ITEM,
);

$items[‘form’] = array(
‘title’ => ‘On Form’,
‘page callback’ => ‘form_all’,
‘access arguments’ => array(‘access form content’),
‘type’ => MENU_CALLBACK
);
$items[’empform’] = array(
‘title’ => ‘ADD Employee Details’,
‘page callback’ => ‘form_add’,
‘access arguments’ => array(‘access add employees content’),
‘type’ => MENU_CALLBACK
);
$items[’emplist’] = array(
‘title’ => ‘ADD Employee Details’,
‘page callback’ => ’emp_list’,
‘access arguments’ => array(‘access list employees content’),
‘type’ => MENU_CALLBACK
);
$items[’empedit’] = array(
‘title’ => ‘Edit Employee Details’,
‘page callback’ => ’emp_edit’,
‘access arguments’ => array(‘access edit employees content’),
‘type’ => MENU_CALLBACK
);
return $items;
}
function form_add() {
$page_content = ”;

$page_content .= drupal_get_form(‘form_add_form’);
return $page_content;
}

function form_add_form() {

if(isset($_GET[‘id’])) {
//print_r($_GET[‘id’]);
$res = db_query(“select * from employees where id=”.$_GET[‘id’]);
$data = db_fetch_object($res);
$ename = $data->ename;
$add1= $data->add1;
}

$form = array();

$form[‘ename’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 25,
‘#id’ => ‘ename’,
‘#title’ => t(‘Employee Name’),
‘#default_value’ => ($ename ? $ename : ”),
‘#required’ => TRUE,
);
$form[‘add1’] = array(
‘#type’ => ‘textarea’,
‘#id’ => ‘add1’,
‘#cols’ => 25,
‘#rows’ =>2,
‘#resizable’ => false,
‘#default_value’ => ($add1 ? $add1 : ”),
‘#title’ => t(‘Address1’),
);
$form[‘add2’] = array(
‘#type’ => ‘textarea’,
‘#id’ => ‘add2’,
‘#cols’ => 25,
‘#rows’ =>2,
‘#resizable’ => false,
‘#default_value’ => ($data->add2 ? $data->add2 : ”),
‘#title’ => t(‘Address2’),
);
$form[‘phone’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 25,
‘#id’ => ‘phone’,
‘#default_value’ => ($data->phone ? $data->phone : ”),
‘#title’ => t(‘Phone Number’),

);
$form[’email’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 25,
‘#id’ => ’email’,
‘#title’ => t(‘Email’),
‘#default_value’ => ($data->email ? $data->email : ”),
‘#required’ => TRUE,
);
$form[‘salary’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 25,
‘#id’ => ‘salery’,
‘#default_value’ => ($data->salary ? $data->salery : ”),
‘#title’ => t(‘Salary’),

);
$form[‘dept’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 25,
‘#id’ => ‘dept’,
‘#default_value’ => ($data->dept ? $data->dept : arg(4)),
‘#title’ => t(‘Department’),

);
if(isset($_GET[‘id’])) {

$form[‘id’] = array(
‘#type’ => ‘hidden’,
‘#title’ => t(‘Name’),
‘#default_value’ =>$data->id,
);

$form[‘update’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Update’)
);
}else{
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Submit’)
);
}
return $form;
}
function form_add_form_validate($form_id, $form) {

if (!valid_email_address($form[‘values’][’email’])) {
form_set_error(’email’, ‘Please enter valid Email’);
}

}
function form_add_form_submit($from_id, $form_values) {
//print_r($form_values);
if(isset($form_values[‘values’][‘id’])) {
db_query(“update employees set ename='”.$form_values[‘values’][‘ename’].”‘,add1='”.$form_values[‘values’][‘add1’].”‘,add2='”.$form_values[‘values’][‘add2’].”‘,phone='”.$form_values[‘values’][‘phone’].”‘,email='”.$form_values[‘values’][’email’].”‘,salary='”.$form_values[‘values’][‘salary’].”‘,dept='”.$form_values[‘values’][‘dept’].”‘ where id='”.$form_values[‘values’][‘id’].”‘”);
}else{
db_query(“insert into employees(ename,add1,add2,phone,email,salary,dept) values(‘”.$form_values[‘values’][‘ename’].”‘,'”.$form_values[‘values’][‘add1’].”‘,'”.$form_values[‘values’][‘add2’].”‘,'”.$form_values[‘values’][‘phone’].”‘,'”.$form_values[‘values’][’email’].”‘,'”.$form_values[‘values’][‘salary’].”‘,'”.$form_values[‘values’][‘dept’].”‘)”);
}
drupal_goto(’emplist’);

}

function emp_list() {
$page_content = ”;
$page_content = ‘Add Employees‘;
$page_content .= drupal_get_form(’emp_list_form’);
return $page_content;
}

function emp_list_form() {
$form = array();
$list = db_query(‘select * from employees’);
while($res = db_fetch_array($list)) {
$data[] = $res;//print_r($res);
}

$cn = count($data);
for($i=0;$i<$cn;$i++) { $form[‘check’.$i] = array( ‘#type’ => ‘checkbox’,
‘#default_value’=> 0,
‘#return_value’ => $data[$i][‘id’],
‘#title’=> t($data[$i][‘ename’].’    Edit‘)
);
}
$form[‘count’] = array(
‘#type’ => ‘hidden’,
‘#title’ => t(‘Name’),
‘#default_value’ =>$cn,
);

$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Delete Selected’)

);
return $form;
}

function emp_list_form_submit($form_id,$form_values) {
//print_r($form_values[‘values’]);
for($i=0;$i<$form_values[‘values’][‘count’];$i++) { if($form_values[‘values’][‘check’.$i] != ”) { $empids[] = $form_values[‘values’][‘check’.$i]; } } //print_r($empids); $cnt = count($empids); foreach($empids as $key=>$value) {
db_query(‘delete from employees where id=”‘.$value.'”‘);

}
drupal_goto(’emplist’);
}

// function emp_edit() {
//
// $page_content = ”;
// $page
//
// }

function form_all() {
// content variable that will be returned for display
$page_content = ”;

// Get today’s date
$today = getdate();

// calculate midnight one week ago
$start_time = mktime(0, 0, 0, $today[‘mon’], ($today[‘mday’] – 7), $today[‘year’]);

// we want items that occur only on the day in question,
// so calculate 1 day
$end_time = time();
// 60 * 60 * 24 = 86400 seconds in a day

$query = “SELECT nid, title, created FROM ” .
“{node} WHERE created >= ‘%d’ ” .
” AND created <= ‘%d'”; // get the links (no range limit here) $queryResult = db_query($query, $start_time, $end_time); while ($links = db_fetch_object($queryResult)) { $page_content .= l($links->title, ‘node/’.$links->nid) . ‘
‘;
}
if ($page_content == ”) {
// no content from a week ago, let the user know
$page_content = “No events occurred on this site on this date in history.”;
}
return $page_content;
// More coming….
}

function emptest_form() {
$form[‘#attributes’] = array(‘enctype’ => “multipart/form-data”);
$year = drupal_map_assoc(array(‘Select’,’2000′,’2001′,’2002′,’2003′,’2004′));
$form[‘select’] = array(
‘#type’ => ‘select’,
‘#title’ => t(‘Year’),
‘#options’ => $year
);
$form[‘radio’] = array(
‘#type’ => ‘radios’,
‘#title’ => ‘Gender’,
// ‘#default_value’ => 1,
‘#options’ => array(t(‘Male’), t(‘Female’)),
);
$today = getdate();
//$dt=$today[‘mday’].’/’.$today[‘mon’].’/’.$today[‘year’];
$form[‘date’] = array(
‘#type’ => ‘date’,
‘#title’ => t(‘date’),
‘#default_value’ => array(‘year’ => $today[‘year’], ‘month’ => $today[‘mon’], ‘day’ => $today[‘mday’]),
);
$form[‘upload’] = array(
‘#type’ => ‘file’,
‘#title’ => t(‘Attach new file’),
‘#size’ => 40,
);
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Submit’)
);
return $form;

}
function emptest_form_submit($formid,$form_values) {
//print_r($_FILES[‘files’][‘name’][‘upload’]);
$dir = drupal_get_path(‘module’, ‘fileupload’) . ‘/files’; //echo base_path();
if(isset($_FILES[‘files’][‘name’][‘upload’])){
$name = $_FILES[‘files’][‘name’][‘upload’];
$size = $_FILES[‘files’][‘size’][‘upload’];
$type = $_FILES[‘files’][‘type’][‘upload’];
$tmp = $_FILES[‘files’][‘tmp_name’][‘upload’];
$file = file_save_upload(‘upload’, array() , $dir);
if($file){
drupal_set_message(“You uploaded $name, it is $size bytes, and has a mimetype of $type.”);
}
else{
drupal_set_message(“file upload failure”);
}
}
}

>Sample Cake PHP Application

Posted: September 15, 2009 in Cakephp

>Controller
———————-
data)) {
$user_check[’email’] = $this->data[‘User’][’email’];
$user_check[‘password’] = $this->data[‘User’][‘password’];
$users = $this->User->find($user_check);
if(isset($users[‘User’][‘id’])) {

$this->Session->write(‘User’,$users);
$this->redirect(array(‘controller’=>’users’,’action’=>’home’));

}else{
$this->Session->setFlash(‘Please Check the user details provided’);
}
}
}
function home() {
$this->LoginRequired();
$this->set(‘name’,$this->Session->read(‘User’));

}
function logout() {
$this->Session->delete(‘User’);
$this->Session->setFlash(‘You have successfully logout’);
$this->redirect(‘/’);
}
function register() {
if(!empty($this->data)) {
$user_email[’email’] = $this->data[‘User’][’email’];
$check_user = $this->User->find($user_email);//print_r($check_user);
if(empty($check_user[‘User’][‘id’])) {
if($this->User->save($this->data)) {
$this->redirect(‘/’);
}else{
$this->set(‘error’,false);
//$this->render();
}
}else{
$this->Session->setFlash(‘Email Already Exist’);
}
}

}
function listusers() {
$users = $this->User->find(‘all’);
$this->set(‘users’,$users);

}
function userdetails($id) {
$this->User->id = $id;
$user = $this->User->read();
print_r($user);
}
function edituser($id) {
$this->User->id = $id;
if(empty($this->data)) {
$this->data = $this->User->read();
}else{
$this->User->save($this->data);
}
//$this->set(‘user’,$user);
}

}
?>

Model
—————–
array( ‘required’ => ‘true’, ‘message’ =>’Please Enter E-mail address’),
’email’ => array(
‘Invalid Email format’ => VALID_EMAIL,
‘Please Enter Email’=>VALID_NOT_EMPTY
),

‘password’=>array(
‘Enter Password’=>VALID_NOT_EMPTY,
‘Password must be at least 4 characters in length’ => array(
‘rule’=>array(‘minLength’, 4)
)
)

);
}

?>