php

Dreaming in Drupal

How do you know you've been thinking about work too much? When your wife relates a conversation she had with you in the morning, and you don't remember a word, but can definitely see how what you said relates to what you're working on:

Saith my wife: "Jeff, how do you set your alarm?"

My (groggy) reply: "Hit field, the arrow, then default."

Now, this could possibly have something to do with alarm clocks. There are often arrows on them, and you hit buttons... but I know better. I was referring to:

$this->addFieldMapping('field', 'source')->defaultValue(0);

...which I have probably typed about 100 times in the past week, and maybe 20 or so last night during a late-night debugging session with the Migration process of flockNote v2 to v3 (from a proprietary WAMP-based system to a new Drupal 7 LAMP-based system).

PHP: Calculating Monthly/Yearly Billing Dates - Same day next month

On a recent project, I needed to generate the timestamp for the 'same day, next month' for billing purposes. This can be tough in some circumstances, like when a user signs up on the 31st of August. What happens in September, when there are 30 days? If I were to simply try something like strtotime('+1 month'), I would get back the first day of October rather than September 30th. Same problem happens when someone hits February (oh my! only 28 days there... sometimes 29).

After looking through a bunch of different forums, Stack Overflow, etc., and finding that most people simply pro-rated the current month and billed people on the 1st (which is a valid option), I decided to write my own function that calculates the same day, next month, and simply gives the last day of the next month if the next month doesn't have as many days as this month.

It's pretty much self-documenting. It could probably be reworked to be a little faster, but I had to do this pretty quickly, and it's pretty robust imo:

Programmatically Adding or Removing a User or Node Reference from a Node (D7 / References)

The References module in Drupal 7 allows for easy creation and removal of user and node references through Drupal's interface. However, programmatically adding and removing these references is a little more difficult.

You basically have to load the node which has the reference in it, edit the reference field (in my example, the reference field can have an unlimited number of references), add or remove the user ID (or node ID if you're chaging a node reference), and save the node.

Let's look at the example of simply adding a user reference to a node:

<?php
 
// Load the node you'd like to edit.
 
$node = node_load($nid);
 
// Add the user ID you'd like to add to this node reference.
 
$node->field_node_user_references[$node->language][] = array('uid' => $uid);
 
// Save the node.
 
node_save($node);
?>

It takes a little more effort to remove a user reference (or node reference) from a node. For this, since I have to do it for a few different fields on a node, I've written a helper function that removes a given $uid from the array of user references on a given node.

Integrate a Webform (3.x) with PayPal

For quite some time, I've wanted to integrate a particular webform with PayPal, since many nonprofits I help use that payment service. With older versions of PayPal, one could add some PHP code into a webform on the site to do this, but it was (a) hackish, and (b) a much less maintainable and secure way of accomplishing the goal I was trying to achieve. So I never did it.

However, after reading Additional processing in Drupal's Webform 3 module (from Drupal Coder), I found that Webform 3 has a hook that runs just after webform has saved a form's data to the database, but before the webform returns the user to a predefined redirected page. hook_webform_submission_insert() is the perfect time to hook into Webform's process and send a user to PayPal, along with that user's data.

Drupal Performance Guide - Drupal and the LAMP/LEMP stack

LAMP Stack with Drupal - Druplicon, Linux, Apache, MySQL, PHP

Drupal is a scalable, flexible, and open source content management system that is built to run on a variety of server architectures. The only real requirement is that PHP runs on your system. You can run Linux, Microsoft, Mac OS X, etc., along with Apache, IIS, nginx, MariaDB, MySQL, PostgreSQL, etc. if you're willing to do a few extra things.

However, the overwhelming majority of Drupal websites use the most popular LAMP stack on the backend: Linux, Apache, MySQL and PHP, or the 'LEMP' variation, with Nginx instead of Apache. This white paper (which is a living document – I'll be updating it as time progresses) provides my thoughts on performance considerations for Drupal on a LAMP stack, but this information can be used for pretty much any system on any server, if you look at the basic principles.

Sections:

Catholic Programmer's T-Shirt and Mousepad

Reposted from Open Source Catholic:

A few weeks back, after finishing a full day of swimming in PHP, HTML and CSS, I was pondering the great mystery of human existence, but probably had a little too much to drink. The result?

Catholic Programming - Design

You can buy the design on the following products (via Zazzle):

Would you like the design on anything else? I could do a mug, or socks, or anything else in Zazzle's catalog...

Alternatively, can you think of a way to code this better? ;-)

Building a Theme for Drupal 7

After having built out many themes for Drupal 6 (and a couple for Drupal 5), I'm going to start from scratch on a couple designs and build a theme in Drupal 7, which will be released sometime in 2010. I'll take you along my journey in this article.

Please note, this article is a work in progress, and I'll be updating it as I go. Hopefully, within a couple weeks, I'll have the article complete, and a nice new theme to release on Drupal.org (maybe), only for Drupal 7.

To get things kicked off, here are a few articles that have good background information on Drupal 7 theming:

Drupal: 2009 Best Open Source PHP CMS!

Packt publishing just announced earlier this morning that Drupal has won the 2009 Open Source PHP CMS award!

Packt Publishing is pleased to announce that Drupal has won the Best Open Source PHP CMS Category in the 2009 Open Source CMS Award. This category featured a very close contest between the top three, Drupal, WordPress, and Joomla! in which Drupal ended up as the overall choice for the judges and the public.

Joomla was the second place winner (kudos to them as well!); read the original release here.

With Drupal being used on Whitehouse.gov, AT&T's mobile apps website, and countless other corporate sites and subsites, it's a good time to be in the Open Source arena!

More award categories will be announced soon!

From OSC: Caching a Page; Saving a Server

I posted a story over on Open Source Catholic today concerning page caching and its importance for saving a server under a heavy load (read: the slashdot effect). You can save a lot of resources on your server by not only using built-in page caching on your favorite CMS, but also exploring further options (for Drupal, there's Boost (read our case study on Boost); for WordPress, there's WP Super Cache). From OSC:

A couple months ago, the Archdiocese of Saint Louis announced that a new Archbishop had been chosen (then-Archbishop-elect Robert J. Carlson). For the announcement, the Archdiocese streamed the press conference online, then posted pictures on the St. Louis Review website of the day's events (updated every hour or two).

Let's Get Through the PHPTemplates!

I just finished rolling a patch for fixing node.tpl.php in Drupal 7 over on the Drupal.org issue queue; hopefully it's ready to be rolled into core, as it's been weeks months since that particular issue was started. Page.tpl.php is already complete. We still have a few more to go, including comment.tpl.php, block.tpl.php and a bunch of little .tpl.php files.

I think Drupal 7 is going to be the best release of Drupal yet, in terms of being able to have a lot of appeal to non-programmers/techies. I set up my first ever full-fledged Wordpress site a few days ago, and it was super-easy to get things going (although also severely limited in what it could do, compared to a base Drupal install with Views and CCK), choose a new theme, change some settings, and hit the ground running.