hook_access($op, $node, $account)

Define access restrictions. This hook allows node modules to limit access to the node types they define.

Parameters

$op The operation to be performed. Possible values:

  • “create”
  • “delete”
  • “update”
  • “view”

$node The node on which the operation is to be performed, or, if it does not yet exist, the type of node to be created.
$account A user object representing the user for whom the operation is to be performed.

Return value

TRUE if the operation is to be allowed; FALSE if the operation is to be denied; NULL to not override the settings in the node_access table, or access control modules.

Codes:

function hook_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create my sales', $account);
}

if ($op == 'update' || $op == 'delete') {
if (user_access('edit her sales', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
}
}

Usage sample:
<?php
CREATE TABLE node_sample (
vid int(10) unsigned NOT NULL default '0',
nid int(10) unsigned NOT NULL default '0',
color varchar(255) NOT NULL default '',
quantity int(10) unsigned NOT NULL default '0',
PRIMARY KEY (vid, nid),
KEY `node_sample_nid` (nid))
?>

function node_sample_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create sample content', $account);
}

if ($op == 'update') {
if (user_access('edit any sample content', $account) || (user_access('edit own sample content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}

if ($op == 'delete') {
if (user_access('delete any sample content', $account) || (user_access('delete own sample content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}
}

Implementation of hooks in Drupal

In your directory /sites/all/, you have to create 2 sub-directories called “themes” and “modules” where your custom themes and modules are located.

What “hooks” in Drupal is all about? They allow your custom-built modules to interact with the Drupal core.  A hook is a function that is named foo_hookname(), where “foo” is the name of your custom-built module (whose filename is foo.module) and “hookname” is the name of the hook. Each hook has a defined set of parameters and a specified result type.

To extend Drupal system, your custom-built module needs to simply implement a hook. When Drupal wishes to allow intervention from modules, it determines which modules implement a hook and calls that hook in all enabled modules that implement it.

In Drupal 6.x there’re more than 70 hooks that you got to remember in case your internet is down. My next posts will show you how to implement each of these hooks in your custom modules.

hook_access
hook_actions_delete
hook_action_info
hook_action_info_alter
hook_block
hook_boot
hook_comment
hook_cron
hook_db_rewrite_sql
hook_delete
hook_disable
hook_elements
hook_enable
hook_exit
hook_file_download
hook_filter
hook_filter_tips
hook_flush_caches
hook_footer
hook_form
hook_forms
hook_form_alter
hook_form_FORM_ID_alter
hook_help
hook_hook_info
hook_inithook_insert
hook_install
hook_link
hook_link_alter
hook_load
hook_locale
hook_mail
hook_mail_alter
hook_menu
hook_menu_alter
hook_menu_link_alter
hook_nodeapi
hook_node_access_records
hook_node_grants
hook_node_info
hook_node_operations
hook_node_type
hook_perm
hook_ping
hook_prepare
hook_profile_alter
hook_requirements
hook_schema

hook_schema_alter
hook_search
hook_search_preprocess
hook_system_info_alter
hook_taxonomy
hook_term_path
hook_theme
hook_theme_registry_alter
hook_translated_menu_link_alter
hook_translation_link_alter
hook_uninstall
hook_update
hook_update_index
hook_update_last_removed
hook_update_N
hook_update_projects_alter
hook_update_status_alter
hook_user
hook_user_operations
hook_validate
hook_view
hook_watchdog
hook_xmlrpc
module_hook
module_implements
module_invoke
module_invoke_all

How do I learn to build a module in Drupal

Today I have a project from AppraisalWorld that I need to build a module in Drupal. I have known this Drupal CMS since I learned the Joomla Environment but I didn’t have a chance to build many complex modules. So this is an opportunity for me to learn how to build a complex module for Drupal environment.

First, I go to www.drupal.org and read how Drupal works after I install. Then I watched some videos that teach me why and how to use Drupal. Of course, it’s all about the benefits of using Drupal. And then now comes the fun part. I started downloading some contributed modules that you can find here http://drupal.org/project/modules?page=1. You will find tons of contributed modules for Drupal system.

At first, I expected there’s a .sql file like in Joomla and WordPress systems, so I can load to MySQL DB but I keep searching in all folders, I couldn’t find it. So I know that it maybe generating during the installation of the module. Luckily enough, I found a function that use hook_schema() in the file .install. That’s where the table is created in the database. In this function, you have to define a table name, an array of fields, type, length, not null, and default for each table. If you want to add records to database table, build a function that uses hook_update_n and use query “INSERT INTO”.

Each module has to have at least .info and .module files as a mandatory. The .install, or .inc, or .admin.inc, or .tpl.php are just optional depends on how complex your module is.

In the next blog, I will show you how to start with the .info page to get your module going. Stay in tune.


imagegrabscreen function in php5

imagegrabscreen function is only available in PHP 5.2.2. This function is useful when it’s necessary to make a screenshot of a website or a private application web page. For example, when a lender wants to view credentials of an appraiser before an appraisal job is assign. That appraiser should reveal his/her credentials of their license/certificate/reports in an image rather than in html format. Here is how you’re going to use it. Remember to upgrade your server to php version 5.2.2, or if you have a hosting provider, ask them if they support 5.2.2 or higher.

Tips for Windows users: For this function to work properly, your Apache service must be set to ‘Allow service to interact with desktop’ otherwise you will just get a blank image. To fix this,  right-click on My Computer,  select Manage/Services and Applications/Services – find the apache service (such as Apache2) and right-click, select Properties – choose the Log on tab and check the ‘Allow service to interact with desktop’ checkbox. Restart Apache service. You’re now good to go.
imagegrabscreen function in php5
imagegrabscreen function in php5

How to send emails out using PHPMailer and GMail

I’ve been working on a schedule calendar project that can be iframed into any kind of website. This schedule will be a real-time calendar which can be used for class registration, training classes, webinar registration, cooking schools, camping registration, and more …. The calendar will be run on Linux server and MySQL Database.

Step 1: download class phpmailer on sourceforge.net

Step 2: unzip the package and place all files in a folder called /includes/ anywhere on your server. However, you need to access to this folder from a php page though.

Step 3: go to google page to find out how to configure the settings for SMTP outgoing mail server. You can read the settings here https://mail.google.com/support/bin/answer.py?answer=75291

Step 4: create a php file called email_out.php (for example) and save it outside the /includes/ folder.

Step 5: include once the phpmailer class in this file and create a new PHPMailer object in order to send email out.

You can view my sample codes below.

<?php
require_once(“includes/classes/class.phpmailer.php”);

$mailer = new PHPMailer(); //create an object
$mailer->IsSMTP();
$mailer->IsHTML(true); // if you send text message, comment this line
$mailer->Host = ‘ssl://smtp.gmail.com:465’;
$mailer->SMTPAuth = TRUE;
$mailer->Username = ‘your_email@gmail.com’;  // Change this to your Gmail address
$mailer->Password = ‘your_gmail_password’;  // Change this to your Gmail password
$mailer->From = ‘your_email@gmail.com’;  // This HAVE TO be your Gmail address
$mailer->FromName = ‘anything here’; // This is the from name in the email, you can put anything you like here
$mailer->Body = ‘<table border=”1″><tr><td>This is the main body of the email</td></tr></table>’;
$mailer->Subject = ‘This is the subject of the email’;
$mailer->AddAddress(‘yourfriend_email@yahoo.com’);  // This is where you put the email address of the person you want to send this email

if(!$mailer->Send())
{
echo “Message was not sent successfully<br/ >”;
echo “Mailer Error: ” . $mailer->ErrorInfo;
}
else
{
echo “Message has been sent successfully”;
}
exit(); //remember to exit, so you won’t leave any trail here
?>