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

Drupal: Status report problem: HTTP request status fails

Some of you may have encountered this problem in some cases. In "Status Report" page, accidentally appears the error below:

HTTP request status   FailsYour system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.
What I did, was to recheck that my webserver had access to internet. Of course it had! The problem exists mostly when there is no information to resolve the domain name used with the local ip address of the webserver. This means that an error occurs in the entries of the DNS server.

In cases where DNS server is not existant(ex. local testing environment) all you have to do is inform the /etc/hosts file. For example if you use the domain http://www.example.local/ and your local ip addr…

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 …

Override the default Opigno LMS navigation menu

I recently used Opigno LMS for a client's project. It's actually a LMS distribution of Drupal. It supports WebEx meetings, Lessons, even PDF Certificates after you complete a Course. In my case though I needed Lessons but no PDF Certificates. Disabling the proper apps/modules wasn't anything out of the ordinary Drupal procedure. What was lacking though was a way to override the main menu to not display the My Achievements item.My Achievements is enabled as long as the Opigno Quiz App (@see Lessons above) is enabled and no hook exists to override this functionality. To disable it I had to create a custom theme using the Platon theme as a base (Platon is the default theme of the Opigno LMS) and copy 3 functions of Platon's template.php file to my theme's template.php with the proper modifications to get what I wanted.See the code below...