Looking for Web Developer Enthusiasts for Kartaca

It’s been almost a month since my last post and this time I’m back with a recruitment option to you! I will strict this post to Turkish, as opposed to my previous posts because we are mostly interested in developers who live in Turkey. If you are interested keep reading!
Continue reading Looking for Web Developer Enthusiasts for Kartaca

PHP Debugging on PHP IDEs

As NetBeans has started to be a great IDE with powerful debugging features, like most people who are migrating to NetBeans, I would like to use it’s built-in debugger abilities. PHP debugging is not new. XDebug is quite around for some time but it always lacked the attention it has now. From now on PHP Developers are starting to use IDEs instead of colorful text editors. Because from now on, PHP started to be accepted as an Enterprise language. It’s pretty easy to code with PHP, it’s very simple but yet it can be a good base for complex applications. With the newest release of PHP 5, it also contains OOP features and with powerful frameworks built around (CakePHP, CodeIgniter, Symphony, Zend,…) some strict rules apply on designing your application for a cleaner, better code and yet you can still use the quick and dirty ways. :)

Continue reading PHP Debugging on PHP IDEs

Writing You Own PHPUnit Listener

Well, in the company, Parkyeri, we have restarted to a crusade to cover all the code lines we had by writing unit tests.

As a PHP Developer, I’m taking the part where I have to write PHPUnit, the xUnit family which is developed for the PHP language. I had used it in the first version for a while, but I had seen that it had reached to the 3rd version. As I’m someone who likes to use the latest things and demos, I tried the latest version and migrated all the first version unit tests to the latest version, which was surprisingly, very easy. You just have to change some class inheritance, delete some constructors, and you are nearly done.

Continue reading Writing You Own PHPUnit Listener

How to Customize Magento Product Page

I’m back with another tutorial about Magento. I’m not the best man you can find about Magento. But I’m trying to learn something from forums. The best you learn something is when you can explain it to someone else. So this is for that!

As I had mentioned in my previous post Magento is an open source ecommerce application made with PHP and Zend Framework. Company behind it is very dedicated to this project and works a lot about it. But the missing part is the tutorials I guess. There isn’t enough entry in their wiki and if you want to learn something, you have find it out yourself. So let’s begin!

This tutorial will cover basics about how you can find your way in the magento code and get to the right place and how you can change a product’s page design by changing the place of the images from left to right.

I tried this on Magento version 1.1.3 and it worked. Check here for the latest version.

First of all we have to understand which part of the code does show the product’s detail page. You can guess of course. But don’t do it and open the Magento folder. Navigate to /app/code/core/Mage/. Then you will see.

No I mean it, really! Go and look on that folder! So your best bet will be the Catalog folder. So is mine. So I enter the folder and look for things which can suit me. Well As I’m a bit familiar with Magento and I know that they have mostly separated the logic and design. So there must be somewhere where this binding must have been done. So the people who has read the previous post and people who are familiar with Unix systems, they must have noticed the etc/. Yes this is the folder you are looking for. In it, there is the configuration file: config.xml. Not that hard to find I guess. Now the real problem is to find out, what is related with what?

config.xml is a huge file and if you don’t know what you are looking for you are in a bit of troube but don’t be afraid. Let’s cover the details a bit and make an understanding. In my previous post I had entered some information about modules’ configuration files. But it was a small file and let’s say I knew what I had to write. So let’s look under the hood. For people who does not want to see how the engine works, the file you are looking for is /app/design/frontend/default/default/layout/catalog.xml. (Maybe instead of looking to code I should have checked folders better :)

There is a version info, ok. There is an admin part, which does not mean a lot to you nor to me. Maybe it’s something fatal but it doesn’t mean a lot for us (Nothing more than a newbie I’m). So the next part I’m more comfortable, as I understand what it belongs to. Well first of all, we know that this part is global. It will be used for all the parts where this module is being used. And the next part is models. This is about database fields. You can see that, below the models, there is the definition of a resourceModel to a class. This means the class defined in this config, will use that resourceModel. But what is this model? Next part is this. You see that the name of the resourceModel (catalog_resource_eav_mysql4 for me) starts now and a lot things is defined. Classes (let’s say the database’s driver folder) and database tables related to this resourceModel are listed here. It basically says that, this attribute comes from this table. You can be shocked by the count but open your phpmyadmin and you will see that there are 192 tables in it. Ouch! The only important part is that (if you don’t want to change the core aspects of course), if you are stuck which table is what, you can always look to these configuration files and find out which table is related to which module. A bit confusing but better than nothing.

Forget about the database and let’s keep going on the config file. Passing to models we come to the resources part. I guess this is about what connections will be used for querying and other things. You see that in catalog_read, there is a connection value and this is core_read. You may guess that it defines which connection this database drivers will use, comes from here. You are right, probably. But I’m not sure, and it doesn’t look I need this part to change the place of the picture, so I move on.

And now comes the block part. This isn’t new and I’m happy to see something that I know from my previous experience. In CMS pages, this part of the configuration had helped me to get to the layout so this might be the part where the output is given. So I get to the /app/code/core/Mage/Catalog/Block/ there are some parts about products but this is the backend part as I see. There isn’t any meaningful htmls in files which are in that folder. So I had a hope that I had found it, but it seems that this is not the case yet. So I move on.

Now a part that I do not even have slightest idea comes and I pass. The next part is adminhtml. I mean HTML! This can be it! There are some definitions about some things but I do not get it. There is a folder /app/design/adminhtml/default/default/ in the file system. And these bindings refer to the template folder. I assume there are some language additions and some other tweaks about admin pages. But again, I’m not sure.

Finally I have reached the frontend part. I’m now sure that this is the part I’m looking, as there are language definitions, per_page_values and layout! There are some things which still I do not get what are they for, but I see the catalog.xml. And this my best bet! I open the file and I see: Default layout, loads most of the pages! Yes I found it!

Below that part, there are things which are not much useful to me, things like url rewrites and some more configuration values. You can alter the general layout of Magento by changing these options by the way (As this module is about catalogs, the general view of catalogs of course, not the payment part!).

catalog.xml is a file familiar to me. From my previous post I knew there was something about blocks defining somewhere and this is it! There are blocks here and I follow the comments and finds Product View.

<block type=”catalog/product_view” name=”product.info” template=”catalog/product/view.phtml”>

This was the part I was looking for all the time. You see the template part?. It refers to /app/design/frontend/default/default/template/catalog/product/view.phtml. So I go there open the file and yes htmls!!! product-img-box line is very meaningful what I’m looking for and it has a css class. I make a file search between files and I find /skin/frontend/default/default/css/boxes.css. The product-img-box is defined in it and I change the float:left; to float:right;.

I open my web browser, open a product page and see that images are at right and not at left. You may have been more lucky if you just looked with firebug and saw the css and changed to it. But you wouldn’t had gained the insight you would have gained with this.

I hope that this was helpful. I’m not an expert on Magento. This article on Magento wiki helped me to figure out something. My previous post’s references were also valid. Do not forget to keep watching Magento Wiki and Magento Forums for best resources about Magento.

Using Custom PHP Codes on Magento

Magento is one of the newest and awarded ecommerce application right now. And I seem to have a project launched nowadays about it.

Above the ecommerce part like adding/modifying products, it also has a built-in CMS. It looks quite useful. Not very powerful though.  It just evaluates html code and not php. In most of the time this is prefered by the way. Because of security risks and things like that. But there are also cases which you want to do if you are importing something to magento or if you are just wanting to do in a quick and dirty way!

Well, the workarounds you find are not quite good. Simply they just take the CMS code, write it to a temporary file and include it, or writing a bit complicated parser inspired from joomla/mambo. Check out here for the details of the workaround. The tutorial I’m putting here is from there to, however I will also try to write solutions to problems that I have encountered (mostly because of magento versions).

This tutorial is working for magento version 1.1.8. You can download the latest version from here.

Now open up your favorite php/xml/html editor. While it will open up, also open your magento installation folder. Magento has a lots of folders for lots of things, which I do not know why. The code part is in app/ directory as you may guess. In the app folder you will find the etc/ where there are configurations for magento generally. Our PHP Code will be a module to the Magento, so we have to go modules directory and create a new xml file, named according to your module. The syntax is like: Parkyeri_customPHP. The first part till underscore means the name of the module, int this case Parkyeri. The second part indicates the component of the module. So you may have different components in one module, in this case customPHP, If we are talking about custom codded PHP pages, so in this module named Parkyeri, there can be different pages like: About, Contact, Jobs. And these are all different components, doing different things. If we were trying to create the About page then we had to use something like Parkyeri_About. You can add all of them in the same xml file.

Open up /app/etc/modules/ and create a file named Parkyeri.xml (the name of the module) and add these lines to file:




    
        true
        local
        
    

But what this means? You declare a new module named Parkyeri to the global site by naming the file Parkyeri.xml. Then you define in the module config file that, this module has a component named customPHP. You say that this component is active and the system can find it in the app/code/local directory by looking at the codepool

As you get it now we pass to the next stage. Now open up app/code/local/Parkyeri/customPHP/etc/ and create a file named config.xml (the default configuration file name for every module.) and add these lines in it.



    
        
            
                Parkyeri_customPHP_Block
           
       
    

What does it mean? This is more of a mapping file than a configuration file. What you are doing here is to map (you may also think bind) a class named Parkyeri_customPHP_Block to a block called parkyeri_customphp. So whenever a block of type parkyeri_customphp will be called, this block will look out for the class you defined in this configuration/mapping/binding file. But also you are defining the location of the component files that you want to call. In most of the cases, you want to call a component’s block from a module. So when calling you will say that I want a component named customPHP, and a block named test and this component is under the module Parkyeri. We move on…

Now the fun part, open up app/code/local/Parkyeri/customPHP/Block and create a file named Test.php and add these lines in it:


class Parkyeri_customPHP_Block_Test extends Mage_Core_Block_Abstract
{
    protected function _toHtml()
    {
        //put here your custom PHP code with output in $html;
        //use arguments like $this->getMyParam1() , $this->getAnotherParam()
        $html = "Hello" . $this->getWorld();
        return $html;
    }
}

So, we have finally reached the part which you are most comfortable, writing code! You will write down all the code you want to evaluate in this file. The _toHtml() method is called when you call it in the cms part. A magical function just like toString(). As you may remark the name of the class, is conventional according to the location of the class. That was how the previous configuration knew where to look when mapping.

And the final part is to embed it in the CMS, this is easy part. But the part where you get dissapointed most. Add this to page you want to show your custom php code:

{{block type="parkyeri_customphp/test" world="World"}}

So, it’s obvious isn’t it? In this code part, you say that, you want to put a block here. The type of this block is parkyeri_customphp/test. So actually you want a block named test under the customphp component which also a part of parkyeri module.  But you may be curious of the rest, on what I mean by saying, world. I’m sending a parameter to the class. Remember the code where there was $this->getWorld(). This world is that world!. So the code will print out “Hello World” (without an exclamation).
So this was it! Did it worked? No I didn’t did it? The real reason that it did not worked, is because, you did not refresh the caching system!! Yes, it’s that simple! It gave me a lots of headache though, hope it won’t to you. You can disable the caching from System -> Cache Management by the way.  Better then refreshing every time :)
If you are stuck on somewhere you should really check out magento forums. There are really helpful people out there from real developers, ceos and community experts.

Update: I have tested this code sample with 1.1.8 and all works fine.

First Day in GUADEC

As some of you may know Gnome Users and Developers Congress (a.k.a. GUADEC) is taking place in Istanbul, Turkey at Bahcesehir University. And on it’s 4th day, it was my first day!

I had not have too much time to join discussions but, I’m sure I’m gonna attend tomorrow in the morning too. I had signed up over the internet and, GUADEC had some great stuff to give me as an attendee. A messenger back, a GUADEC 2008 t-shirt, a notepad (moleskin copy but red and written Google on it), a lots of commercial stuff about Turkey, Fedora 9 Live DVD, Ubuntu 8.04 CD and a 4G Mandriva bootable flash disk!

I had the chance to attend only two presentations, one was about Telepathy, a network framework that can be used to create multi user chat applications and other things. While at first I was intrested, the guy who was talking was not as good as a spokesman as a geek. I did not understand much of things he talked about but It was a good thing I guess.

The second one was about Moonlight, a Linux emulation of Silverlight. It was better than the previous one. The presentator was talking better but he had experienced some issues with his laptop. Anyway, It was intresting to see a Microsoft presentation in a Linux related congress. As Novell had some contracts with Microsoft, I guess this Silverlight emulation was about this as there is a group, full time worker in Novell who only does this job. I guess it’s also about that Adobe is not yet implemented Flash in Linux too. And this gap will be filled with Moonlight. It was funny to watch animations by the way. With it’s power to use C++ API and XAML markup language, it’s intresting. You have to use Mono for easily developing it. But any editors goes. But Mono is the easiest and most powerful I guess, at least that was what the man told.

It was great too see people in the conference room using their laptops during the presentation. You can argue that this is a disrespect to the man who is talking, but It was not the case in here. We are all geeks!! I really felt that I was not alone in the world and there is a lots of people around the globe that was like me!

While listening, some light bulbs had come on my mind that, joining these kind of congress open your way and mind up. When I had gone there, I was hoping there was somethings about Online Desktops but I had mistaken and I encountered something completely differently there and I entered to the discussion which I liked it’s name and it was enlightening and enjoying to do that.

Tomorrow I will go there just for that!

Development Diaries in Weburunu

Weburunu, also named as SanalDoku (meaning virtual texture/pattern in turkish) is a good framework for creating web sites and also a simple content management system.

It has its dead ends too of course. Even it’s a powerful tool, it’s not too easy to develop with it. You have to get some practice on it. While you practice writing code with it, you enjoy using it and fascinate with ideas in it, at least I had. Even it started to get a little older it’s still powerful and flexible.

During trainings about Weburunu I mentioned some things that can be added, since we need them but don’t have too much time to write their code or design them. These are some of my thoughts and they may take Weburunu one step ahead or it can be needles as there are some different projects going on.

Well in passing days I worked on the search part of WBU and added a template search in it. Now you are able to search in display templates and it will show you the results. It can be a useful tool or just nothing. Anyone who wants to try can try it from my home directory from any wbu website’s admin panel. Just search something and below the content results there will be template results.

One more thing that can be added is, the editing of these templates from the web browser. Without the need to open an html editor or emacs or vi, we should be able to edit these display types from admin panel. This will give us more freedom and perhaps more control over our product. Or it will just create a security hole.

Another thing is to add a form generator script to content types. Which will really take Weburunu one step ahead, and it will be really more user friendly and perhaps developer friendly. By this way we can really create content types that are different than others. How these values can be defined is a different discussion anyway, but if someone must intend to write them down, then we can discuss it anyway.

Not many people tries to change or improve the Weburunu’s core as many people try to use it as tools and not to get involved deep in it. But this will help you more to understand it and also will give more insight on how to write a framework :)

Code Smells

In recent days I have read an article about “Code Smells”. I was not aware of such a thing since then. I mean I was aware that people was writing bad code but I had not known there was a name for it. Anyway after reading the 3 part of the article at metapundit.net, I was really surprised to see people like me who actually think like me when writing code.

I know that development is about functionality, most often also about scalability and maintainability. Well most of the time you write a class constructor like:


public function Foo($a, $b, $c) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
}

In most of the time you write down this kind of things. The part itself is repeating! Yuu just do the same thing, copy and paste. It’s perfectly understandable to do this. Because we are in the creation of it. How about something like this:


public function($a, $b, $c) {
$args = array("a", "b", "c");
foreach ($args as $arg)
$this->$arg = $$arg;
}

Is it cool? I know that it looks like lame. But isn’t the previous one looked lame too. I have not even reduced the lines of code. But think of adding another parameter to the function, then you have to copy paste another line doing the same thing but you just add another item in the array and lines are decreased. Instead of defining the arguments on the place where you define functions, you may use “func_get_args()” to get them and use the $args array to set them as you know which param is coming. Same thing can be done for most of the set and get functions


public function get($arg) {
return $this->$arg;
}
public function set($arg, $value) {
$this->$arg = $value;
}

Is it lame? Kind of, as you have lost lots of methods about getting and setting. I know that gets and sets are not used by this way. You may add some validations to it or some protections on it. Maybe you want to set to a new date and check if the day is 32 or not or if the month is 13 or something else. But that’s not the point. What I try to mean is, by this way you write less and create a more maintainable code I guess. It’s about functionality I suppose.

As metapundit mentioned, first thing we learn about programming is “loops”. The basic loops and, in most of the cases we pass down this first lesson and keep hard coding. How many of you created a form like:


<table>
<tr>
<td>Field 1</td>
<td><input type="text" name="field1"></td>
</tr>
<tr>
<td>Field 2</td>
<td><input type="text" name="field2"></td>
</tr>
<tr>
<td>Field 3</td>
<td><input type="text" name="field3"></td>
</tr>
</table>

Have you though of:


<table>
<?php
$fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ($fields as $key => $val) {
echo '<tr><td><?=$val?></td><td><input type="text" name="<?=$key?>"></td></tr>';
}
?>
</table>

Isn’t this doing the same thing while giving you an understandable and maintainable code? I know that logic and design must be separated. So in this case our sample will change into something like:


<?php
$fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ($fields as $key => $val) {
$template->setBlock("TEMPLATE1");
$template->setVariable(array("KEY"=>$key, "VAL" => $val));
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

Syntax may change but the idea remains. You can also improve the way you define your form field elements in a way that template’s variable setting method accept it as it is.


<?php
$fields = array(array("key" => "field1", "val" => "Field 1"), array("key" => "field2", "val" => "Field 2"),array( "key" => "field3", "val" =>"Field 3");
for( $i = 0 ; $i < count($fields) ; $i++ ) {
$template->setBlock("TEMPLATE1");
$template->setVariable($fields[$i]);
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

Remarked something? I repeat the way I define my variables. So:


<?php
$old_fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ( $old_fields as $key => $val) {
$fields[] array("key" => $key, "val" => $val);
}
for( $i = 0 ; $i < count($fields) ; $i++ ) {
$template->setBlock("TEMPLATE1");
$template->setVariable($fields[$i]);
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

You may not want to the last part, but I hope that I was able to explain what I meant.

Anyway I really try to write code like this. Whenever I copy paste a part of a code to somewhere else, I think of creating a function or a loop from it. But in most of the time as my time is limited, I note it down to do it in a later time which I don’t. Only in Boop I try to refactor the code I have written as I’m not pressed on releasing it in a mean time.

Anyway I have given some examples from metapundit and also created my owns too. I hope I was able to explain what I thought when I read about his article. I was really happy to find out someone who was thinking like me but still making that mistakes, just like me!