Skip to main content

Drupal - Custom functions

I created the functions below to easily process the values of a $field in a $table by $number using the specified $params.

Explanation: $number = number
$field = string
$table = string(schema name)
$params = array(
  'fields' => array('uid','nid',...),
  'select' => array('=','>','<',...),
  'update' => array('=','>','<',...),
  'values' => array('1','1',...),
);
/*
 * This function increases by $number,
 * the $field of the $table WHERE $params
 * are valid. This function assumes the
 * field needs to be updated and NOT created.
 */
function module_increase_field($table,$field,$number,$params){
  $select = "SELECT {$field} FROM {".$table."}";
  if(!empty($params['fields'])){
    $select .= " WHERE ";
    for($i=0;$i<count($params['fields']);$i++){
      $select .= $params['fields'][$i].$params['select'][$i]."'".$params['values'][$i]."',";
    }
    $select = substr($select,0,strlen($select)-1);
    $value = db_result(db_query($select),0);
    $value++;
    $update = "UPDATE {".$table."} SET {$field}='{$value}' WHERE ";
    for($i=0;$i<count($params['fields']);$i++){
      $update .= $params['fields'][$i].$params['update'][$i]."'".$params['values'][$i]."' AND ";
    }
    $update = substr($update,0,strlen($update)-4);
    db_query($update);
  }else{
    watchdog('Modify field',t('The $params array is empty'));
  }
}

/*
 * This function increases by $number,
 * the $field of the $table WHERE $params
 * are valid. This function assumes the
 * field needs to be updated and NOT created.
 *
 * @see function module_increase_field()
 * for more information
 */
function module_decrease_field($table,$field,$number,$params){
  $select = "SELECT {$field} FROM {".$table."}";
  if(!empty($params['fields'])){
    $select .= " WHERE ";
    for($i=0;$i<count($params['fields']);$i++){
      $select .= $params['fields'][$i].$params['select']."'".$params['values'][$i]."',";
    }
    $select = substr($select,0,strlen($select)-1);
    $value = db_result(db_query($select),0);
    $value++;
    $update = "UPDATE {".$table."} SET {$field}='{$value}' WHERE";
    for($i=0;$i<count($params['fields']);$i++){
      $update .= $params['fields'][$i].$params['update'][$i]."'".$params['values'][$i]."' AND ";
    }
    $update = substr($update,0,strlen($update)-4);
    db_query($update);
  }else{
    watchdog('Modify field',t('The $params array is empty'));
  }
}

/*
 * This function returns the number of
 * $field(s) the $table has WHERE $params
 * are valid.
 *
 * @see function_module_increase_field()
 * for more information
 */
function module_count_field($table,$field,$params){
  $sql = "SELECT COUNT({$field}) FROM {".$table."}";
  if(!empty($params['fields'])){
    $sql .= " WHERE ";
    for($i=0;$i<count($params['fields']);$i++){
      $sql .= $params['fields'][$i].$params['select'][$i]."'".$params['values'][$i]."' AND ";
    }
    $sql = substr($sql,0,strlen($sql)-4);
  }
  return db_result(db_query($sql));
}

Comments

Popular posts from this blog

Configure drupal_http_request() on the fly

The drupal_http_request() function supports connections using a proxy. You can configure a global proxy to use for all drupal_http_request() callbacks from the settings.php file of your website/installation by filling the following lines with the proper information:For those of you preferring a UI, then HTTP proxy module is for you.But what if you only want to use a proxy in some cases; like a custom module? In that case you will need the following function(modify accordingly):Now all you have to do is Call <?php _MYMODULE_change_proxy(); ?>.Do your drupal_http_request() calls.Call <?php _MYMODULE_change_proxy(TRUE); ?> to reset your previous settings.

Drupal: Allow registrations through Invite or Referral modules only

The Invite module provides invitations from existing users to their contacts. The Referral module, in contrary, creates a special URL for each existing user, which can be found in each user's profile, and allows new user registration. Even though, these two modules seem to provide the same functionality, they don't (and they shouldn't). Invite module, provides a mechanism for a site administrator to limit new registrations to "Invitations only". Referral module doesn't provide any of this functionality. Some users have requested the Invite module and Referral module to join in one module. Until now, there isn't anything to that direction. Wouldn't it be great though if there was a solution to limit drupal registration to referral or invitation only? Copy the functions below to a refinvite.module file in your sites→all→modules→refinvite folder and enable the module. Then go to http://<your-address-here>/admin/user/settings and enable the new …

Send mass email to certain users using VBO

Views Bulk Operations uses the built-in actions to act upon nodes, users, entities etc. Among these actions is our ability to send emails to users. If you try to use this action though you will encounter a small but serious problem. You are asked for an email address to send your message. You might say "but I have selected the users I want this message to reach. Why should I input an address again?". Don't get overwhelmed. What this field expects is not an address but a token. Specially this token [user-email]. If the website is intended for you, then you might say that's not such a problem to write this token down again and again. But if the manager of the website is bound to be the client or if you just want to spend less time writing things down, you might prefer to use a custom hook_form_alter() callback to set a default value. It could be done in both theme and module environment so suit yourself. Just add the following in your module or theme.