Archive for the ‘PHP’ Category

editing PHP with NetBeans IDE

Monday, October 27th, 2008

I admit. I’m an IDE and RAD lover. And I enjoy trying out new development enviroments all the time in the quest for the perfect work tools. After using Eclipse for quite a long time for PHP, I switched to Activestate’s Komodo 4.4 IDE. Komodo is a really nice and slick IDE with by far the best PHP code completition I’ve found in any tool. The debugging is easy to setup using xdebugger and it’s easy to analyse your code using watches and analysing tools. One tool I found useful was the Watch file tool wich could be compared to the tail -f command in Linux (allows you to ‘stream’ the data of a file to output. useful to analyze log files on the fly for instance).

However, I think Komodo suffers a bit from a ruff UI, loosing variables and values in debugging sessions, forgets it’s line position and break point positions from time to time and has a limited subversion integration.

Then I found NetBeans early access for PHP. This was a really nice surprise, however I couldn’t get debugging working so I switched back to Komodo again. But now Netbeans.org released the RC1 of version 6.5. After some fiddeling I got the debbuger working (I guess it really didn’t have to do with rc1 but still..) and by that I’ve got my new favourite environment. Main advantages are for me:

  • The debugger is the best PHP debugger I’ve tried.
  • A slick and fast UI. Very easy and intuitive to rearrange panels and views almost in any way I’d like
  • Superb subversion support. Direct markers for changes, easy shortcuts to view diffs and revert actions.
  • Good refactoring support (things like Ctrl+R to refactor a variable etc).
  • Built in SQL editor with code completion
  • Active and easy class/property inspector (Komodo lists all open files, Netbeans shows only active or selected file in file list without opening it)
  • and so on..

A screen shot attached, editing a TYPO3 ext project for a client.. note the SVN popup..

coding PHP with NetBeans 6.5 RC1

create new record link with TCE

Friday, August 8th, 2008

If you need a ‘create new record’ link in BE of TYPO3 and also need to supply some default values, this is the way;

  1. $params = "&edit[$mapTable][123]=new&defVals[$mapTable][name]=banana&defVals[$mapTable][codename]=hajaja";
  2. $aOnClick = t3lib_BEfunc::editOnClick($params,‘../../../../’.TYPO3_mainDir);
  3. $icon = ‘<img’.t3lib_iconWorks::skinImg($this->doc->backPath,‘gfx/edit2.gif’,).‘ title="Create secret agent" alt="" style="border:0;" />’;
  4.  
  5. //print a pen icon with the link
  6. $HTML = ‘<a href="#" onclick="’.$aOnClick.‘">’.$icon.‘</a>’;
  7.  

So, what we do is setting up necessary parameters for alt_doc.php to perform cmd actions. In this example I use;

edit[table name][uid OR pid OR -uid]=command – use uid and the command edit to edit a uid, otherswise (as I do  above) use pid and new as command to create a new record in a certain pid. A negative uid together with the new command Creates a new record right after the supplied uid.
defVals[table name][table column]=value – two times. This sets default values for my two fields; name and codename.

Then assemble the parameters with the TYPO3 API call t3lib_BEfunc::editOnClick(); into a javascript call.

TemplaVoila in your own extension

Thursday, May 15th, 2008

TemplaVoila is an incredibly smart tool for templating your TYPO3 website. However it cannot only be used for pages and fragments of pages (FCE:s), but also for templating your own extensions output.

The classic way of adding template support for an extension has been to use an HTML file marked with tags like

  1. ###MY_REPLACED_MARKER###

or similar that would be replaced by content using the cObj function substituteMarkerArrayCached:

  1. $markerArray[‘###MY_REPLACED_MARKER###’] = $data;
  2. $output =  $this->cObj->substituteMarkerArrayCached($template, $markerArray);

Which is a quite simple and easy to use approach. The TemplaVoila way quite similiar. Let me show you a short example more or less cut from the mininews extension with a few simplifications for us mere mortals;

First off we will load up TV with a hard coded TO (Template Object) record uid. I suggest using some kind of FlexForm setting or similar for that ;) This part normally goes into an init() function or so;

  1. // Load and initialize TemplaVoila TO (if any)
  2. if (t3lib_extMgm::isLoaded(‘templavoila’))    {
  3.   $field_templateObject = 1;  //read my default page "TO" record uid..
  4.   if (intval($field_templateObject))    {
  5.     $this->TMPLobj = t3lib_div::makeInstance(‘tx_templavoila_htmlmarkup’);
  6.     $this->TA = $this->TMPLobj->getTemplateArrayForTO(intval($field_templateObject));
  7.     if (is_array($this->TA))    {
  8.       $this->TMPLobj->setHeaderBodyParts($this->TMPLobj->tDat[‘MappingInfo_head’],$this->TMPLobj->tDat[‘MappingData_head_cached’]);
  9.     }
  10.   }
  11. }

What the code above does is checking if TV exists, instantiates the templavoila HTML markup utility class (tx_templavoila_htmlmarkup), loads a TO record with uid one into it and finally sets some HTML header taggings..

Sooo, we got ourself a loaded TO with HTML template contents and also since TO always has a DS, we got the data structure fields (like field_mainMenu or similar). Next of is adding contents to where we’d like to have it. In our case into the field field_content mapped with TV.

  1. //we got some content into $content var loaded from db earlier, now render it with TV
  2. if (is_array($this->TA))    {    // TemplaVoila:
  3.   // Create list of elements:
  4.   $templatedResult = $this->TMPLobj->mergeDataArrayToTemplateArray(
  5.     $this->TA,
  6.     array(
  7.       ‘field_content’ => $content
  8.     )
  9.   );
  10. }
  11. $out = $templatedResult;

That’s it! :)

Using TemplaVoila for an extension has many benifits, just as using it for normal page rendering. One of them is the possibility to easily map your template using Templavoila. I suggest that you supply a valid datastructure (DS) xml with your extension, that gets loaded as static ds (again taken from mininews);

  1. // Adding datastructure for Mininews:
  2. $GLOBALS[‘TBE_MODULES_EXT’][‘xMOD_tx_templavoila_cm1′][‘staticDataStructures’][]=array(
  3.   ‘title’ => ‘Mininews Template’,
  4.   ‘path’ => ‘EXT:’.$_EXTKEY.‘/template_datastructure.xml’,
  5.   ‘icon’ => ,
  6.   ‘scope’ => 0,
  7. );

For a more detailed documentation around TV and the source to inspiration for this article, see the TemplaVoila extension documentation (http://typo3.org/documentation/document-library/extension-manuals/templavoila/1.3.4/view/1/6/)

TYPO3 and xajax quick guide

Tuesday, November 20th, 2007

Ajax is definetly the thing to go with the web as it enhance the user experience as well as allowing more logic to be on the server side (okey, your pages javascript usualy tend to get slightly more bloated as well..).

Anyway. It’s not ajax in general today, but the good looking PHP/ajax combination found with the xajax project. Xajax allows you to have PHP functions that you mapp/register as javascript calls. There is an extension for TYPO3 called – xajax! You guessed it! There are two tutorial/example extensions as well that describes how it works. But for my memory I’m writing a short guide here as well with my own words. Maybe it helps someone else.

Using ajax in your backend module:

  • Do the obvious. Install the xajax extension from TER (ie TYPO3 Extension Repository).
  • Launch your favourite editor and open your module file like – /typo3conf/ext/myext/mod1/index.php
  • Include the ajax class
  1. // DEFAULT initialization of a module [BEGIN]unset($MCONF);require_once(‘conf.php’);require_once($BACK_PATH.’init.php’);
  2. require_once($BACK_PATH.‘template.php’);
  3. // include XAJAX class library
  4. require (t3lib_extMgm::extPath(‘xajax’) . ‘class.tx_xajax.php’);
  5. $LANG->includeLLFile(‘EXT:myext/mod1/locallang.xml’);
  • Somewhere in your init() method register your PHP function
  1. $this->xajax->registerFunction(array(‘xajaxTest’, &amp;$this, ‘xajaxTest’));
  • In your printContent() method add this to ensure that xajax does whatever it wants (ie handle requests..) first
  1. $this->xajax->processRequests();
  • Then create your nifty PHP function where you want to do magic, and add a responce object like this:
  1. function xajaxTest() {
  2. $c = "ohlas!";// Instantiate the tx_xajax_response object
  3. $objResponse = new tx_xajax_response();
  4. $objResponse->addAssign("mydiv","innerHTML", $c);//return the XML response generated by the tx_xajax_response object
  5. return $objResponse->getXML();
  6. }
  • Now finally add a call to your function. This is probably done in moduleContent() or referenced functions
  1. <a href="#" onClick="xajax_xajaxTest();">xajax magic</a>

Note that we got an extra xajax_ added to the beginning of our function which is added automatically.

That’s it folks!

Modded vc_javascriptslideshow

Friday, June 15th, 2007

I’ve modified the TYPO3 extension vc_javascriptslideshow from ground up. The vc_javascriptslideshow is a javascript/css based slideshow that flips images and I needed to be able to control it fully from TS. That ended up in adding a few extras as well.

The extension now includes

  • added full support for TS configuration
  • added support for multiple slideshows on one page
  • Added settings for border and background for the slideshow (stylesheet settings on a surrounding div)
  • Changed the file rezise to use IMG_RESOURCE instead of custom exec of IM and now use width/height attributes there. This makes it also possible to define w/h as ie 200m or 200c for cropping or relative resize (see the static ts template).
  • added support to (in TS only so far) define a path from where all images should be taken.

TypoScript example setup, included via TV element:

  1.  
  2. lib.topBanner < plugin.tx_vcjavascriptslideshow_pi1
  3.  
  4. lib.topBanner {
  5.   //specify path from where to fetch images
  6.   imagesPath = fileadmin/bilder/banners/
  7.   //..or a use a specified list of images
  8.   //  imagesList = Vinter.jpg,Solnedgang.jpg
  9.   altText = informationsruta
  10.     imageWidth = 500
  11.   imageHeight = 70c
  12.   //override imagesList to fetch all images
  13.   getAllFromPath = 1
  14.   //number of millisecond for each transition
  15.   msTransitionDelay = 140
  16.   //background color for slider space
  17.   #backgroundColor = #ccc
  18.   //a possible border if you like
  19.   borderSize = 0
  20. }
  21.  

The extension probably contains some bugs, but works for my purpose. I will try to add some documentation soon.
I have e-mail the original author hoping that he will include it in the official release. Otherwise I’ll probably add a new ext.. (*hrhr*).

Download the modified ext. (unpack, upload and update ext in EM).

Delphi for PHP, a promising PHP RAD

Thursday, March 1st, 2007

I just recently found out about Delphi for PHP – a PHP RAD environment. Actually the first real PHP RAD environment I’ve seen. Delphi was (and still is I guess) a fast and easy to use RAD for MS Windows solutions based on the the Pascal language. I more or less started my programming path with Delphi so there are special feelings still there for it :)

Delphi for PHP can be found at http://www.codegear.com/products/delphiforphp. Take a sneak peak at the demos. I really like the table grid and database connection.

I think that a RAD like Delphi for PHP is a great thing for the PHP language though not required. But developing complex, custom tailored solutions for your clients by hand is time consuming and demanding even if you use frameworks like cakePHP or symfony (which both are really great btw). Using some kind of IDE like eclipse (with php plugin), PSPad or JEdit is a requirement for any development.

What I fear is that I might loose to much control over the source code and it’s parts. To much happening automaticaly. A bit like using Microsofts development tools ;) . I’m also a bit worried when using proprietary libraries like the vcls as I can’t enhance or redefine them in the same way as with open source ones. The good thing here on the other hand is that these libraries are object oriented and therefore easily extendible. Thinking of it, it would also be interesting to know a bit more about layout support and application flow controls.

Well I’m hoping to fetch a trial of it before I mumble to much about pros and cons. ;)

So please codegear. Release a trial version so we can give it a spinn. I’m definitely considering to start use it. If it can increase productivity and efficiency, while still leaving me with full control over my sources it’s worth a lot. I’m ready to let go of total control and “notepad mentality” in benefit of that. Maybe I might actually be able to start earn some money on the projects then.