Informatika Mihelac

Articles tagged with php

May 13 2008 php

Static variables and unset()

In Drupal many things are cached with static variables and this is often main source of headache. I just submitted small patch for bug where static cache does not work as it should. Bug report and this example shows that unset() should not be used with static variables:

<?php
function locale_supported_languages($reset = FALSE, $val = array()) {
  static $all = NULL;

  if ($reset) {
    // If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.
    unset($all); // this would not work
    //$all = NULL; // this would work
  }

  if (is_null($all)) {
    $all = array();
    foreach ($val as $key => $value) {
      $all[$key] = $value;
    }
  }
  return $all;  
}

echo "\narray should have 'cached' element (PASS):";
print_r(locale_supported_languages(FALSE, array("first" => "cached")));

echo "\narray should have 'new' element (PASS):";
print_r(locale_supported_languages(TRUE, array("first" => "new")));

echo "\narray should have 'new' element but have 'cached' (FAILS):";
print_r(locale_supported_languages(FALSE));
May 08 2008 php

Drupal - replace colors in buttons with color.module

Drupal Color.module allows the admin to change the color scheme of a theme completely. Color module basically fill viewport with 5 choosen colors, layout transparent PNG on it and then makes image slices and update CSS with this colors.

I would like to share some code that customize color.module further. After color scheme is choosen specified images would be copied and colors updated to reflect current color scheme. This is especially useful for creating buttons that should be color aware.

/**
  * Form alter hook
  */
function mycolormodule_form_alter($form_id, &$form) { 
  if ($form_id == 'system_theme_settings') {
    $form['#submit']['mycolormodule_system_theme_settings'] = array();
  }
} 

/**
 * Copy PNG images and change color in images with color from palette. 
 * 
 * Settings in color.inc $info array:
 *
 *     'change_color' => array(
 *        'images/form_buttons.png' => array("#3CC6F5" => "bottom"), // Color #3CC6F5 will be updated with color of "header bottom" 
 *    ),
 */
function mycolormodule_system_theme_settings($form_id, $values) {

  $theme = $values['theme'];
  $target = variable_get('color_'. $theme .'_stylesheet', NULL);
  if (!$target) return;
  $files = variable_get('color_'. $theme .'_files', array());
  $target = dirname($target) . "/";
  $info = $values['info'];
  $source = drupal_get_path('theme', $theme) .'/';
  foreach ($info['change_color'] as $file => $replace_array) {
    $base = basename($file);
    $source = $source . $file;
    // NOTE: change color
    $im = imagecreatefrompng($source);
    foreach ($replace_array as $old => $new) {
      $old_arr = _color_unpack($old);
      $index = imagecolorexact($im, $old_arr[0], $old_arr[1], $old_arr[2]);
      $new_arr = _color_unpack($values['palette'][$new]);
      imagecolorset($im, $index ,$new_arr[0], $new_arr[1], $new_arr[2]);
      $files[] = $target . $base;
    }
    imagepng($im, $target . $base);
  }
  variable_set('color_'. $theme .'_files', $files);
}

Buttons from above image are based on Wii buttons demo. Using color.module is described in details here.

March 31 2006 php 9 comments

Ruby / Rails alternative to PHP print_r() and var_dump()

Both PHP functions provides information about a variable passed as argument. It is often used in development to get quick and human-readable info about array or object. If you are using Rails you can use DebugHelper’s debug(object) helper function. Note that helpers are available only in views. In controllers, models and other Ruby code you can use ‘puts YAML::dump(object)’ method to get readable info about object.

About

I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in Informatika Mihelac.

Contact: bmihelac@mihelac.org

RSS feedSubscribe to RSS Feed