Computer Programming

Yes, a bit specific, but I need to store some links!

Drupal: Upgrade Notes

Been upgrading from an old Drupal 4.5 (aka Civicspace 82) to Drupal 5.1.

Dropping CiviCRM

I had to get rid of the unused civicrm tables. Installing it seemed like a good idea at the time, but no point of using it for a tiny campaign. Here's a script that will delete the tables. It's posted here because the InnoDB engine complains if you drop these in the wrong order, and violate some constraints.


drop table if exists `civicrm_activity`;
drop table if exists `civicrm_activity_history`;
drop table if exists `civicrm_activity_type`;
drop table if exists `civicrm_address`;
drop table if exists `civicrm_county`;
drop table if exists `civicrm_custom_option`;
drop table if exists `civicrm_custom_value`;
drop table if exists `civicrm_donation_page`;
drop table if exists `civicrm_email_history`;

Name Based Dispatch

Here's a simple way to do name-based dispatch. $dispatchTable = array( 'First Thing' => 'functionName', 'Second Way' => array('ClassName', 'StaticMethod') ); ... ... $data = "Your data here, probably in an array."; call_user_func( $dispatchTable[ $name ], $data ); This is nice, because you can put the dispatch table at the top of your code, or in a separate file. I'm using it in a payment processing system, where different products may be dispatched to different code. You get a level of indirection here, where you can swap out different methods for each product (or use the same method). The only "trick" here is that, call_user_func() takes a pseudo-type known as a callback. Callbacks come in three forms:

NVU - text fields for copy-and-paste

Here's how to create one of those text fields with HTML that the user's supposed to copy-and-paste into their page. It's not hard.

Create a form.

Add a Text Area. Give it a name, and set the rows and columns.

From the text area dialog, click on the "Advanced Properties..."

Click on the Javascript tab.

Add a property named "onclick" with the value of "this.focus(); this.select();".

Click OK.

Click OK.

NOTE: I found a serious problem - NVU's code reformatting will cause the html code to break within myspace, because NVU inserts newlines. To fix the problem, you have to save out the source, join all the lines, and upload the file manually.

Anti-Pattern: Working With Live Data

I recently lost a chunk of data while I was developing a nice little macro to produce a report. How it was lost, is pretty sad. I had become used to pressing a few keys to clear out my spreadsheet, and I accidentally pressed the keys on a spreadsheet of the live data. Pffft. Data vanished.

I luckily had most of the data in another document, and restored some of the lost data, but, the lost bits were lost. All this was due, not to faulty code, but because I failed to create a development sandbox.

Yes, this was only a macro, but, even for something so simple, it's smart to make a separate place to develop it. This sandbox would have contained a copy of the data.

A sandbox is better than a backup. That's because the sandbox is a minimal subset of what you need to write your program. The real deployment environment is usually a lot more complex. To back up the real environment, so it's safe to develop in there, could be more difficult than you could imagine, and take a long time, too.

Hiding Email Addresses from Spiders and Spammers

Here's a bit of PHP code that emits javascript. The javascript emits an email link, but coded in Javascript, so that most spiders won't be able to get your email address for spamming.


<?php
function printEmailAddress( $email )
{
$parts = explode( '@', $email );
echo ' var d = document;';
echo 'd.write("");';
echo 'd.write("'.$parts[0].'");';
echo 'd.write("@");';
echo 'd.write("'.$parts[1].'");';
echo 'd.write("
");';
}

printEmailAddress("fake@mail.com");
?>

Error Handling Notes

Error messaging facility:


$errL = new ErrorListener();

global $errL;
...
$errL->add('context', 'message', $errCode);
...

if ($errL->hasErrors())
$errL->showAsHtml();

$errL->getError('context');

if ($errL->hasError('context'))
exit;

The listener would collect all error messages into an object, and provide methods to help display them.

It can also dispatch code:

$errL->addHandlerObject('context', &$objRef );

If an error is triggered with the key 'context', $objRef->errorHandler($errCode) is called.

Shortly after I wrote this note, a class at PHP Classes showed up that does something related: error_manager

HTML Tag Closing Function

User-submitted HTML often contains small markup errors that can affect other parts of the page. The most common are unclosed tags that cause text to be bolded, italicized, or linked all the way down the page. The visual effect is catastrophic, though the error is really minor.

The html_close_tags() function scans HTML code, and generates a string that will close all the open tags. An easy way to use it is like this:


  $html = $html.html_close_tags($html);

A Totally Simple Text Captcha Class

First draft of a "captcha" class that asks the user the sum of two numbers. It helps you write code to ask a question like this: What is the total of five plus three? (answer with a word) [ ] The user should respond with "eight".

How it Works

A captcha needs to "remember" the question that it posed to the user. To do this, the constructor takes an argument that's used as the key. It then saves the captcha data to a file in tmp/ named with the key.

Associative Array (Hash) Tricks in PHP

Suppose you're tallying some data. You want to know if a value has shown up in the data. You can do this by using the array, like this:

$tally = array();
while( $row = getData() )
    array_push($tally, $row);
if (in_array($tally, 'theValueIWant'))
    echo "yes";
else
    echo "no";

There's a faster way, by using the array key. (It should be faster.)

FileMirror in Perl

This is a Perl version of the VB FileMirror class shown earlier on this site. The class helps you write scripts to (semi) safely mirror or move a file system from one directory to another. It does this by building the correct destination paths, making destination directories, and deleting files only after they appear to have been copied over.

Docs are in FileMirror.txt, attached.

DriveGMover.pl is a script that uses the class.

Comment Styles for C-Style Code

If there's anything that annoys people more than funky indentation, it's bad comments. I don't mean about the code, but in the code.


function name()
{
/* Once upon a time, all my comments
were inside the functions.
*/
}

It seemed to makse sense, but there's something that sucks about having to scroll more to start reading code.

/* Moving it up above the function seems to help!
So i did this for a while.
*/
function name()
{
...codehere....
{

Lately, all the languages are getting automatic documentation generation. They use comments like this:

/**
* The code comments here get turned into web pages.
* I like how there's a little extra whitespace above and below.
* And the stars are a pain to keep adding, even with editor support.

Indenting Styles for C-Style Code

People get into all kinds of gripey little snits about how to indent code. Whenever you start a project, it's pretty important to nail down indentation, because it's one of those personal preferences that becomes "a big issue" when there's a conflict. Usually, the indentation is a non-issue, but it's something to fight about instead of discussing the real underlying issues, like interpersonal communication problesm.

So, let's catalog some styles, and discuss:

if (a==b) {
do_this();
}

That's the standard Java style. It's pretty compact.

if (a==b)
{
do_this();
}

That's the standard C style, and it puts a little extra whitespace in there. It's my favorite style, because it is the easiest to read.

if (a==b)
{
do_this();

Novice's Notebook

This is a repository of "novice" articles, written with the intent of driving more traffic to the site, and getting more ad clicks. It's pretty crass, I know, but the information may be very useful.

Image Gallery: Yet Another

Here's yet another image gallery. This one requires no database, but requires you to create a file called captions.php in each directory of images, and populate it thusly:


<?php
$captions = array (
"image1.jpg" => "Caption for Image 1",
"image2.jpg" => "Caption for Image 2",
"image3.jpg" => "Caption for Image 3",
"image4.jpg" => "Caption for Image 4"
);
?>

It features a potentially annoying "paramater hiding" feature, one-image-ahead-preload, and very simple navigation. It was written at the request of Eternal_Student on Experts Express.

Generating Word Files with WordML (xml) as a Template

Background

I've been spending the past day messing around with WordML to get it to generate Word files from PHP. The goal was to create printable tables. As George Bush said as the Iraq War slid toward failure: "Mission Accomplished."

My strategy was to start with a Word file, save it as xml, aka WordML, and then using the file as an xml template for the table layout. This is analogous to designing your output forms in NVU or Dreamweaver, and then turning it into a template.

Note that I saved the document out as an .xml file, not an .htm file. The .htm file could also be used as a template, but it doesn't retain all the Word features. There's also an alternate format, .htx, that is a MIME encoded .htm with enclosures, but the entire thing is MIME encoded, and decoding (and re-encoding) is a pain. XML is clean and relatively simple, and allows you to embed graphics.

Syndicate content