Opensource, CMS, PHP, MySql, Drupal, Frameworks

Thursday, August 19, 2010

Useful functions[drupal]

drupal_parse_info_file($filename)

system_settings_form($form)

node_get_types($op = 'types', $node = NULL, $reset = FALSE)

drupal_get_path($type, $name)

drupal_get_form($form_id)

variable_set($name, $value)
variable_get($name, $default)
variable_del($name)

MENU_NORMAL_ITEM
MENU_CALLBACK
MENU_SUGGESTED_ITEM
MENU_LOCAL_TASK
MENU_DEFAULT_LOCAL_TASK

Wednesday, August 18, 2010

module annotate(_nodeapi)[drupal]

annotate.info
; $Id$
name = Annotate
description = Allows users to annotate nodes.
core = 6.x
package = Drupal

annotate.module
'Annotation Settings',
'description' => 'Change how annotations behave',
'page callback' => 'drupal_get_form',
'page arguments' => array('annotate_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'annotate.admin.inc',
);
$items['admin/annotate'] = array(
'title' => 'Node Annotation',
'description' => 'Adjust node annotation options.',
'position' => 'right',
'weight' => -5,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module','system'),
);
return $items;
}
function annotate_nodeapi(&$node, $op, $teaser, $page){
global $user;
switch($op){
case 'view':
if($user->uid == 0 !$page ){
break;
}
$type_to_annotate = variable_get('annotate_node_types',array('page'));
if(!$type_to_annotate[$node->type]){
break;
}
$result = db_query('SELECT note FROM {annotations} WHERE nid = %d AND uid = %d', $node->nid, $user->uid);
$node->annotate = db_result($result);
$node->content['annotation_form'] = array(
'#value' => drupal_get_form('annotate_entry_form',$node),
'#weignt' => 10,
);
break;
case 'delete':
db_query('DELETE FROM {annotations} WHERE nid = %d', $node->nid);
break;
}
}
function annotate_entry_form($form_state, $node){
$form['annotate'] = array(
'#type' => 'fieldset',
'#title' => t('Anotations'),
);
$form['annotate']['note'] = array(
'#type' => 'textarea',
'#title' => t('Notes'),
'#default_value' => isset($node->annotate) ? $node->annotate : '',
'#description' => t('Make your personal annotations about this content here. Only you (and the site administrator) will be able to see them.')
);
$form['annotate']['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['annotate']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
return $form;
}
function annotate_entry_form_submit($form,$form_state){
global $user;
$note = $form_state['values']['note'];
$nid = $form_state['values']['nid'];
db_query('DELETE FROM {annotations} WHERE nid = %d AND uid = %d', $nid, $user->uid);
db_query("INSERT INTO {annotations} (nid, uid, note, created) VALUES (%d, %d, '%s', %d)", $nid, $user->uid, $note, time());
drupal_set_message('Your annotation have been saved.');
}


annotate.admin.inc
'checkboxes',
'#title' => t('Users may annotate these content types'),
'#options' => $options,
'#default_value' => variable_get('annotate_node_types',array('page')),
'#description' => t('A text field will be available on these content types tomake user-specific notes.'),
);
$form['annotation_deletion'] = array(
'#type' => 'radios',
'#title' => t('Annotation will be deleted.'),
'#description' => t('Select a method for deleting annotations.'),
'#options' => array(
t('Never'),
t('Randomly'),
t('After 30 days')
),
'#default_value' => variable_get('annotation_deletion',0) // Default to never
);
$form['annotate_limit_per_node'] = array(
'#type' => 'textfield',
'#title' => t('Annotations per node'),
'#description' => t('Enter the maximum number of annotations allowed per node (0 for no limit).'),
'#default_value' => variable_get('annotate_limit_per_node',1),
'#size' => 3,
);
 
return system_settings_form($form);
}
function annotate_admin_settings_validate($form, $form_state){
dpm($form_state);
$limit = $form_state['values']['annotate_limit_per_node'];
if(!is_numeric($limit)){
form_set_error('annotate_limit_per_node',t('Please enter a number.'));
}
}
 

annotate.install
t('Stores node annotations that users write.'),
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {node}.nid to which the annotation applies.'),
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {user}.uid of the user who created the annotation.'),
),
'note' => array(
'description' => t('The text of the annotation.'),
'type' => 'text',
'not null' => TRUE,
'size' => 'big'
),
'created' => array(
'description' => t('A Unix timestamp indicating when the annotation was created.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0
),
),
'primary key' => array(
'nid', 'uid'
),
);
return $schema;
}