Archive for August, 2010

Drupal Notes 2: sub-theme derivative of bluemarine : Theme inheritance, overriding, preprocessing

Sunday, August 29th, 2010

add folder to:
sites/all/themes(createThis)/subThemeName(createThis)/

create subThemeName.info (in newly created folder):
; $Id$
name = subThemeName
description = blah blah blah blah blah
version = 1.0
core = 6.x
base theme = bluemarine
stylesheets[all][] = descartes-style.css // copy images from parent theme into sub-theme.
stylesheets[all][] = new.css

finally, create above mentioned new.css (in newly created folder) and add styles you’d like to modify, example:
/*
** Style to override the styles.css in
** bluemarine.css
*/

/*
* Plain white right nav.
*/

#sidebar-right{
border-left: 1px solid #ea940c;
background-color: white;
}

ready to go further? copy any of the following over from bluemarine into yourNewSubTheme and edit away:
page.tpl.php (hint: start here), block.tpl.php, box.tpl.php, comment.tpl.php

even further, you’ve moved $breadcrumb in page.tpl.php to where you’d like it, but you want to edit INSIDE that…
these PHPTemplate functions that render content can be found in yourSite/includes/theme.inc
find the one that spits out the content you’d like to modify, and then create a template.php in your new theme directory and override that function…
example: theme_breadcrumb() uses “>>” as separators, in
yourSite/sites/all/themes/yourNewTheme/template.php (make it)
overwrite theme_breadcrumb() via something like this:


// $Id$
/**
* @page
* Override theme_() functions for the youNewTheme.
*/

// Overrides theme_breadcrumb()
function phptemplate_breadcrumb($breadcrumb){
if (empty($breadcrumb)) return;
$sep = ‘<div>&nbsp;&nbsp;</div>’;
$breadcrumb_string = ‘<div>’ . implode($sep, $breadcrumb) . ‘</div>’;
return $breadcrumb_string;
} // end of template.php example

other PHPTemplate theme functions from includes/theme.inc to investigate:
theme_image(), theme_links(), theme_progress_bar(), theme_username(), theme_table(), theme_item_list()

called as follows:
$variable = theme(‘functionName’, $arg1, $arg2, …) , which looks like this:
$crumb = theme(‘item_list’, $breadcrumb, null, ‘ul’, array(‘class’=>’breadcrumb-items’));

IMPORTANT NOTES/REMEMBER:
– a phptemplate_block() function redefined in template.php will override a block.tpl.php file in template folder!
– if you want to preprocess CONTENT before it’s passed to template.php, thus, instead use phptemplate_preprocess_functions()!
– if you’re NOT making a sub-theme, add to .info this line: engine=phptemplate instead of using the base theme line
– don’t forget to make a nice screenshot.png (150×90)

Drupal Notes 1: module hook_block(), hook_help(), check_plain(), check_url(), watchdog(), t(), l(), placeholders(!,%,@)

Sunday, August 29th, 2010

Create:
moduleName.info & moduleName.info, save them in sites, all, modules

.info example content:
;$Id$ // This will be substituted by Drupal CVS
name = “ModuleNameHere”
description = “Displays items blah blah blah”
core = 6.x
php = 5.1

.module beginnings…:
// $Id$ // This will be substituted by Drupal CVS
/**
* @file
// This denotes that this comment refers to this whole file
* Description Here Of Module…
* @see http://www.goodreads.com // Drupal doc is generated by doxygen, this links to a ref
*/

/**
* Implementation of hook_block()
*/

function moduleName_block($op=’list’, $delta=0, $edit=array()){
switch($op){
case ‘list’:
$blocks[0][‘info’] = t(‘Module Info Title’);
return $blocks;
case ‘view’:
$url = ‘http://www.moduleXML.com/review/list_rss/’;
$blocks[‘subject’] = t(‘Module Subject’);
$blocks[‘content’] = “Module Content Dynamic or Static…”;
return $blocks;
}
}

/**
* Implementation of hook_help()
*/

function moduleName_help($path, $arg){
if($path == ‘admin/help#moduleName){
$txt = ‘The moduleName module uses the !subThisHolder_url API ‘;
$link = l(‘copyForTheAnchorTag’, ‘http://www.moduleName.com’);
$replace = array(!subThisHolder_url‘ => $link);
return ‘<p>’ . t($txt, $replace) . ‘</p>’;
}
} //end of example .module code…

Other things useful of note:

The watchdog() function: http://api.drupal.org/api/function/watchdog/6
watchdog(‘loggingCategoryNameThisModuleNameMostLikely’, $msg, $varsToSubIntoMsg, WATCHDOG_WARNING):
logging options:
WATCHDOG_EMERG
WATCHDOG_ALERT
WATCHDOG_CRITICAL
WATCHDOG_ERROR
WATCHDOG_WARNING
WATCHDOG_NOTICE
WATCHDOG_INFO
WATCHDOG_DEBUG

check_plain()http://api.drupal.org/api/function/check_plain/6

check_url() http://api.drupal.org/api/function/check_url/6

t() example:

t(‘Replacing %value by !urlHere for @emailPerhaps ‘, array(‘%value’=>’test’, ‘@emailPerhaps’=>$email, ‘!urlHere’=>’http://blah.com’);

!placeholders replaced as is, @placeholders replaced effectively “escaped by” check_plain(), %placeholders are replaced themed as well

PHP Interface and Abstract Class example

Sunday, August 22nd, 2010

abstract class animal
{
abstract function getowned();
private $age;

protected function __construct($age) {
$this->age = $age;
}

public function getage()
{
return $this->age;
}
}
interface insurable {
public function getvalue();
}

class pet extends animal implements insurable {
private $name;
public function __construct($name,$age) {
parent::__construct($age);
$this->name = $name;
}
public function getname() {
return $this->name;
}
public function getowned() {
return (“Owner String”);
}
public function getvalue() {
return (“Priceless”);
}
}

class house implements insurable {
public function getvalue() {
return (“Rising fast”);
}

}

$charlie = new pet(“Charlie”,6);
$catage = $charlie -> getage();
$catname = $charlie -> getname();
print “$catname is $catage years old!<br><br>”;

if ($charlie instanceof pet) print (“charlie is a pet<br>”);
if ($charlie instanceof animal) print (“charlie is an animal<br>”);
if ($charlie instanceof house) print (“charlie is a house<br>”);

if ($charlie instanceof insurable) print (“charlie is insurable<br>”);
reference: http://www.wellho.net/resources/ex.php4?item=h108/animal3.php

simple PHP namespace example

Sunday, August 22nd, 2010

Foo.class.php
class Foo
{
function Simple()
{
$var = “Hello”;
return $var;
}
}

namespace.php
namespace Second;
include(‘Foo.class.php’);

class Foo
{
function Simple()
{
$var = “Hello in Second Namespace<br />”;
return $var;
}
}

$fooSecond = new \Second\Foo();
echo $fooSecond->Simple();
//option 1
//$fooFirst = new \Foo();
//$fooFirst->Simple();
//option 2

echo \Foo::Simple();

simple PHP __autoload example

Sunday, August 22nd, 2010

Foo.class.php
class Foo
{
public static $varz;
public static function Simple()
{
self::$varz = “Hello”;
return self::$varz;
}
}

index.php (in the same directory)
function __autoload($classname) {
include(“$classname.class.php”);
}

//include “Foo.class.php”; //no longer needed!

$foo = new Foo();
echo $foo->Simple();

simple ob_start(), flush(), ob_gzhandler, sleep(), HEREDOC examples

Sunday, August 22nd, 2010

//ob_start()
ob_start();
print “Hello First!\n”;
ob_end_flush();

ob_start();
print “Hello Second!\n”;
ob_end_clean();

ob_start();
print “Hello Third!\n”;

//outputs “Hello First! Hello Third!”

//flush()

<HTML>
<BODY>
<DIV ID=”flushme”>
Hello, world!
</DIV>
< ? php flush(); sleep(2); ? >
<SCRIPT>
d = document.getElementById(“flushme”);
d.innerHTML = “Goodbye, Perl!”;
</SCRIPT>
< ? php flush(); sleep(2); ? >
<SCRIPT>
d.innerHTML = “Goodnight, New York!”;
</SCRIPT>
</BODY>
</HTML>

// compressed output

< ? php
ob_start(‘ob_gzhandler’);
print “My content\n”;
ob_end_flush();
? >

// sleep

< ? php
ob_implicit_flush(true);
for($i=0;$i<5;$i++)
{
$dis=<<<DIS
<div style=”width:200px; background-color:lime;border:1px; text-align:center;text-decoration:blink;”>
$i
</div>
DIS;
echo $dis;

sleep(5);
//flush();
}
? >

memory jogger: Chain of responsibility, Polymorphism, Design Pattern reference, Decorator pattern, Singleton

Saturday, August 21st, 2010

Good stuff, take a moment to refresh and review:

Chain of responsibility:
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
Polymorphism:
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
JavaScript really OOP?:
http://articles.techrepublic.com.com/5100-10878_11-1044656.html
http://mckoss.com/jscript/object.htm
JS Prototype object:
http://www.javascriptkit.com/javatutors/proto4.shtml
Design Patterns reference/list:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29
Decorator Pattern:
http://en.wikipedia.org/wiki/Decorator_pattern
Loose Coupling def:
http://en.wikipedia.org/wiki/Loose_coupling
Singleton Pattern:
http://en.wikipedia.org/wiki/Singleton_pattern

MySQl sub query example 2

Tuesday, August 17th, 2010

SELECT
q.questionId,
q.question,
q.questionTopic,
q.questionDate,
q.courseId,
q.customerId,
v.voted_this
FROM
” . $course_database_questions_table_name . ” q
LEFT JOIN
(SELECT questionId voted_this FROM votes WHERE customerId = ‘” . $customer_id . “‘) v
ON
q.questionId = v.voted_this
WHERE
q.courseId = ” . $courseId . ”
ORDER BY q.questionDate DESC

MySQL sub query example – nice

Monday, August 16th, 2010

$cp_output = “”;
if(!$get_cp_data = mysql_query(”
SELECT
q.*,
c.*,
vote_counts.Tally
FROM
questions q
LEFT JOIN
(SELECT
questionId,
count(1) as Tally
FROM
votes
GROUP BY
questionId
) vote_counts
ON
q.questionId = vote_counts.questionId
LEFT JOIN
customers c
ON
c.customerId = q.customerId
WHERE
q.courseId = 1
ORDER BY
vote_counts.Tally desc

“)){ echo mysql_error(); }

while($get_cp_data_array = mysql_fetch_array($get_cp_data)){
$q_id = $get_cp_data_array[‘questionId’];

$q_fname = $get_cp_data_array[‘customerFName’];
$q_lname = $get_cp_data_array[‘customerLName’];
$q_email = $get_cp_data_array[‘customerEmail’];

$q_topic = $get_cp_data_array[‘questionTopic’];
$q_question = $get_cp_data_array[‘question’];
$q_date = $get_cp_data_array[‘questionDate’];
//$q_count = $get_cp_data_array[‘votecount’];
$q_customer = $get_cp_data_array[‘customerId’];

if($q_topic == “”){ $q_topic = “–“; }
if($q_fname != “” || $q_lname != “”){
$from = ‘<strong>’ . $q_fname . ‘ ‘ . $q_lname . ‘</strong> – ‘;
}

# Parse the question text.
$old = array(“\n”);
$new   = array(“<br/>”);
$q_question = str_replace($old, $new, $q_question);

# Output the question.
$cp_output = $cp_output . ‘
<div>’ . $q_id . ‘</div>
<div>
<div><strong>’ . $q_topic . ‘</strong></div>
<div>’ . $q_question . ‘</div>
<div><strong>Submitted</strong> ‘ . $q_date . ‘ by ‘ . $from . ‘<a href=”mailto:’ . $q_email . ‘”>’ . $q_email . ‘</a></div>
</div>
<div></div>
‘;
}

clean & simple check your $_SERVER var

Friday, August 6th, 2010

<?

echo “<pre>”;
print_r($_SERVER);

?>