Archive for the PHP Category

Cuando generamos un View, y queremos darle un formato en particular tenemos que agregar una llamada desde nuestro templates a la función views_embed_view, pasando por parámetros el nombre de la vista, el display_id.

<?php echo views_embed_view(’nombre_vista’ , $display_id = ‘default’); ?>

Cuando
hacemos esta llamada nos genera un html con el contenido de la vista.
Si queremos darle un formato html especial tenemos que generar un
archivo con el nombre view_view–nombre_vista.tpl.php
A esta función llega un objeto con el nombre $view->result, que contiene todo el contenido de nuestra vista.

Por ejemplo si quisieramos mostrar el contenido de la vista en una listado con el title y el teaser, el código seria el siguiente.

<ul class=”destacados_libros”>
<?php if (!empty($title)): ?>
<li class=”top_titulo”><?php print $title; ?></li>
<?php endif; ?>
<?php foreach ($view->result as $row): ?>
<li><?php echo $row->node_revisions_teaser; ?></li>
<li><?php echo $row->node_title;?></li>
<?php endforeach; ?>
</ul>

Photoshop Slice tool Graphic designers, when creating websites, will often give programmers the Photoshop (PSD) files of the screens and, if they have enough experience designing for the web, they will ’slice’ the design. Basically what they’ll do is mark a rectangular region (a ’slice’) of the image that will then be used in the HTML of the page. Often a single page is composed of many such slices. When the designer is happy with his work he will save in one shot all the slices and provide the programmer with the slices and PSD files in case he needs to modify anything later on. If you buy templates from templating sites such as Template Monster you will receive the HTML, slices and PSD files.

Here in Easytech we mostly use The Gimp to read and modify PSD files. The Gimp can read and write PSD files with no problem, preserving layout information, etc. Unfortunately, it has no slice support and completely ignores all slice information stored on the PSD file. After looking a while for an alternative I was disapointed I couldn’t find one. What I did find is a little document which describes an old PSD file format and a C# parser which understands the Slices section of the PSD file. With this information and a hex editor and some sample PSD files I was able to write a little PHP script which basically outputs a list of convert statements which can be used to slice an image.

To use the script:

1. Download the script by clicking HERE.
2. Open the PSD file with The Gimp and save it as a PNG
3. Run this script on the PSD file
4. Run the resulting converts on the PNG file to slice it up

This code is quite simple and was written over a weekend so its quite rudimentary. It has almost no error checking and is more a proof of principle. If later on I need something more sophisticated this code will provide a good starting point.

The first thing I should probably do is split the program into two smaller programs: one to write an XML document describing the slices and a second program that processes this document and writes the convert statements. In addition I should probably create a couple of Classes to make the code more re-usable. Maybe later… for the moment have fun with it!

While working on the administration section of a medium to large site, you may need to allow different roles to create and edit different content. Typically you want Role X to be able to create and view some content types only. The creation part is easy: go to the Access control section and give Role X the Create and Edit permission for the content types you want this role to be able to create and edit. The next thing you’ll want to do is give the role a way to list all nodes for the content type it is allowed to view/edit and be able to administer these nodes. The logical thing would be to give the role access to the admin/content/node by giving this role the administer nodes permission. The problem is that the filter for this screen will show you nodes of all types regardless of what the role should be able to edit! This hack will guide you to another trick: creating a views page for Role X to be able to administer the nodes he should have access to. Read on for the rest of this hack.

A page to edit and create content types

 

(more…)

A missing feature in Drupal is the ability to preview changes made to the home page. If you have a custom page as your home and you promote several pieces of content to it there is no way for you to preview how it will look. If the content breaks the layout or doesn’t look as good as you expect it to do then you’ll have to make several changes to fine-tune your home. Wouldn’t it be great if you could get a preview of your home and when you are satisfied with the all the changes hit a publish button to see all your changes commit at once? Well you can! Read on to see how.

(more…)

A problem which often arises when writing Drupal themes, modules, etc. is making sure there are no ‘hard-coded’ paths in our code. To help new users on their quest to writing quality Drupal code I list here the most used API functions to get paths in a portable way:

  • global $base_url - A global variable which stores your sites URL, typically http://www.<your_site>.com. This variable is set in your settings.php configuration file. This is not very portable and maybe in the future Drupal has a function for this.
  • base_path() - Returns the base URL path of the Drupal installation. At the very least, this will always default to /. This is used, for example when you want to add a resource relative to your base_url. For example like so: drupal_set_html_head(’<style type=”text/css” media=”all”>@import “‘. base_path() . drupal_get_path(’module’, ’system’) .’/system.css”;</style>’);
  • conf_path() - Based on your sites URL tries to find the most appropiate settings.php (site configuration file). To read how it works see Drupals API documentation
  • drupal_get_path($type, $name) - Returns the path to a system item (module, theme, etc.). The $type argument can be ‘theme’, ‘theme_engine’ or ‘module’ and $name the name of the item for which you want the path. To get an absolute path you need to prefix the $DOCUMENT_ROOT (the document root of your drupal installation).
  • path_to_theme() - This points to your theme’s path on the local filesystem. Usually $DOCUMENT_ROOT/themes/<your_theme> or $DOCUMENT_ROOT/sites/<your_site>/themes/<your_theme>
  • path_to_engine() - Returns the path to the currently selected engine.

To learn a bit more search for this terms in the API search box or ‘use the source, Luke!’.

PHP Hacks - written by Jack HerringtonRecently we bought a copy of the O’Reilly book “PHP Hacks” written by Jack Herrington. Inspired by his many wonderful hacks and the need to convert CSV values into SQL Insert statements I wrote a simple PHP script to do the job. To use it simply drop it in your webserver and point a browser to it. Fire up your favorite spreadsheet (Openoffice, right?), create a table (remember to use the first rows for the column names) then cut-n-paste the table into the textarea, hit Submit and Voilá: your spreadsheet data is converted into SQL inserts. Have fun!

(more…)

At Easytech we are using Drupal for projects which require a CMS (Content Management System). It’s very powerful, full of modules to extend the functionality and written in PHP which is a Language for which we have a lot of programming experience. For a project we are working on we used the Related Content module. This module allows you to link a particular piece of content (a node in Drupal parlance) to other content such as images, videos, audios, etc. One problem with the current implementation of the module is that it wasn’t designed to work when you have lots of content on the site. The patch we currently submitted to Drupal fixes this by separating the Related Content into a tab and adding a filter to be able to quickly locate content items. Here’s a screenshot:

This is how the new features looks like

If you are interested in getting the patch its here. In case you want to download a patched version of the module I have included a tar file with the latest version as an attachment in this posting. Try it out and send feedback to: miquel <at> easytech <dot> com <dot> ar.

Here’s a tar file with the patched version to try out: relatedcontent-5.x-1.1-patch.tar.gz. To install it just download it and untar it in the modules/contrib directory of your Drupal installation.

  • english

One of our latest projects here in Easytech was done using PHP as our programming language and Oracle as our Database. We had a lot of experience coding in PHP and developing with the Oracle Database but none using both together. The project turned out to be a great one but there isn’t a lot of information on the web since most PHP applications use MySQL or PostgreSQL as their database. Today I was looking at what blogs where available on the Oracle site and came across Christopher Cross’s blog which has a lot of interesting information for PHP developers. In it I found links to two interesting presentations: “Leveraging the Power of Oracle with PHP: Taking Advantage of the Database” and ” PHP and Performance” and a link to a book called “The Underground PHP and Oracle Manual” written by Christopher himself and Alison Holloway which covers PHP and Oracle development. For more references on using PHP and Oracle visit the PHP section in the Oracle Technology Network.