26 Aralık 2009 Cumartesi

ADVTemplateList Class Test

ADVTemplateList sınıfımızı test edebilmek için hazırladığım kod da aşağıdaki gibi.

<?php
/**
  * This is the tester of ADVTemplateList class
  * 
  * @author Behçet MUTLU
  * @copyright 2009
  */

    include_once("../system.core.php");
    
    $list = new ADVTemplateList($_SERVER["DOCUMENT_ROOT"]."ADVManager/adv_templates/");
    
    if($list->isReady())
          pre($list->getList());
    else
        print("List is not ready!");
    
?>

Bu kodun çıktısı şu şekilde.

DEBUG:  Template at "C:/wamp/www/ADVManager/adv_templates/Kopyas� template1" does not contain template.xml schema file.


DEBUG:  XML file, "C:/wamp/www/ADVManager/adv_templates/template1/template.xml" loaded successfully.

Array
(
    [0] => ADVTemplate Object
        (
            [name:ADVTemplate:private] => Template 1
            [author:ADVTemplate:private] => Behçet MUTLU
            [datecreated:ADVTemplate:private] => 25-12-2009
            [copyright:ADVTemplate:private] => 2009 Adres Gezgini
            [website:ADVTemplate:private] => www.adresgezgini.com.tr
            [thumbnail:ADVTemplate:private] => adv_templates/template1/thumb.jpg
            [schema:ADVTemplate:private] => C:/wamp/www/ADVManager/adv_templates/template1/template.xml
            [isReady:ADVTemplate:private] => 1
            [xml:ADVTemplate:private] => Array
                (
                    [template] => Array
                        (
                            [0] => 

                            [info] => Array
                                (
                                    [0] => 
    
                                    [name] => Template 1
                                    [author] => Behçet MUTLU
                                    [copyright] => 2009 Adres Gezgini
                                    [website] => www.adresgezgini.com.tr
                                    [thumbnail] => adv_templates/template1/thumb.jpg
                                    [datecreated] => 25-12-2009
                                )

                            [fields] => Array
                                (
                                    [0] => 
    
                                    [image] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => 
        
                                                    [title] => Logo Resmi
                                                    [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                                                    [default] => library/image_64x64.png
                                                    [@id] => image1
                                                    [@width] => 64
                                                    [@height] => 64
                                                )

                                            [1] => Array
                                                (
                                                    [0] => 
        
                                                    [title] => Ürün Resmi
                                                    [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                                                    [default] => library/image_128x169.png
                                                    [@id] => image2
                                                    [@width] => 128
                                                    [@height] => 169
                                                )

                                        )

                                    [text] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => 
        
                                                    [title] => Başlık Yazısı
                                                    [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                                                    [default] => Başlık Yazısı
                                                    [font] => Arial
                                                    [color] => 000000
                                                    [size] => 20
                                                    [@id] => text1
                                                    [@max] => 100
                                                    [@min] => 10
                                                )

                                            [1] => Array
                                                (
                                                    [0] => 
        
                                                    [title] => Web Site Adresi
                                                    [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                                                    [default] => www.websiteniz.com.tr
                                                    [font] => Arial
                                                    [color] => 000000
                                                    [size] => 12
                                                    [@id] => text2
                                                    [@max] => 100
                                                    [@min] => 10
                                                )

                                            [2] => Array
                                                (
                                                    [0] => 
        
                                                    [title] => Buton Yazısı
                                                    [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                                                    [default] => Bilgi İçin Tıklayınız
                                                    [font] => Impact
                                                    [color] => 000000
                                                    [size] => 18
                                                    [@id] => text3
                                                    [@max] => 100
                                                    [@min] => 10
                                                )

                                        )

                                )

                        )

                )

            [fields:ADVTemplate:private] => Array
                (
                    [0] => 
    
                    [image] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 
        
                                    [title] => Logo Resmi
                                    [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                                    [default] => library/image_64x64.png
                                    [@id] => image1
                                    [@width] => 64
                                    [@height] => 64
                                )

                            [1] => Array
                                (
                                    [0] => 
        
                                    [title] => Ürün Resmi
                                    [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                                    [default] => library/image_128x169.png
                                    [@id] => image2
                                    [@width] => 128
                                    [@height] => 169
                                )

                        )

                    [text] => Array
                        (
                            [0] => Array
                                (
                                    [0] => 
        
                                    [title] => Başlık Yazısı
                                    [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                                    [default] => Başlık Yazısı
                                    [font] => Arial
                                    [color] => 000000
                                    [size] => 20
                                    [@id] => text1
                                    [@max] => 100
                                    [@min] => 10
                                )

                            [1] => Array
                                (
                                    [0] => 
        
                                    [title] => Web Site Adresi
                                    [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                                    [default] => www.websiteniz.com.tr
                                    [font] => Arial
                                    [color] => 000000
                                    [size] => 12
                                    [@id] => text2
                                    [@max] => 100
                                    [@min] => 10
                                )

                            [2] => Array
                                (
                                    [0] => 
        
                                    [title] => Buton Yazısı
                                    [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                                    [default] => Bilgi İçin Tıklayınız
                                    [font] => Impact
                                    [color] => 000000
                                    [size] => 18
                                    [@id] => text3
                                    [@max] => 100
                                    [@min] => 10
                                )

                        )

                )

            [values:ADVTemplate:private] => Array
                (
                    [image1] => library/image_64x64.png
                    [image2] => library/image_128x169.png
                    [text1] => Başlık Yazısı
                    [text2] => www.websiteniz.com.tr
                    [text3] => Bilgi İçin Tıklayınız
                )

        )

)

ADVTemplateList Class ile Tema Listesinin Alınması

ADVTemplateList sınıfı reklam yöneticimiz tarafından kullanılacak ve şablon listemizi hazırlayacak olan sınıftır. Amacı belirtilen bir veya birden fazla klasör içerisinde bulunan şablon dosyalarını okuyarak ADVTemplate objeleri listesini hazırlamasıdır.
Bu sınıf oluşturulurken listesini hazırlayacağı klasör konumuna zorunlu olarak ihtiyaç duymaktadır. Hazırladığım sınıfın kaynak kodu şu şekilde geliştirildi.
<?php
/**
  * This class is used to represent the list of Templates that are in a directory.
  * 
  * @author Behçet MUTLU
  * @copyright 2009 Adres Gezgini
  */
  class ADVTemplateList {
    
      private $dir;
      private $list;
      private $ready;
      
      /**
      * This is the default constructor of the class that needs directory to be 
      * traversed of which contains the templates.
      * 
      * @param mixed $dir
      * @return void
      */
      public function __construct($dir)
      {
          $this->dir = $dir;
          $this->_initialize();
      }
      
      /**
      * This method is used to initialize the private variables of the class that
      * are used by this class.
      * 
      * @return void
      */
      private function _initialize()
      {
          $this->list = $this->_getTemplates($this->dir);
          
          $this->ready = $this->list != false;
      }
      
      /**
      * This method is used to traverse the list of the directories of given directory
      * and collect the list of templates in to an array and return, if the directory
      * exist template list, otherwise false.
      * 
      * @param mixed $directory to be traversed
      * @param mixed $exempt to exclude from listing
      * @param mixed $templates the referance to be used 
      *                         if we want collect from multiple directories
      * @return template list or false
      */
      private function _getTemplates($directory, 
                                      $exempt = array('.','..','.ds_store','.svn'), 
                                      &$templates = array()) 
      {
          if( ! ($handle = @opendir($directory))) // with @ character we are avoiding the wrong path error to be displayed as an exception
              return false;
          
          while(false !== ($resource = readdir($handle))) {
              if(!in_array(strtolower($resource),$exempt)) {
                  if(is_dir($directory.$resource.'/'))
                  {
                      if(file_exists($directory.$resource."/template.xml"))
                       $templates[] = new ADVTemplate($directory.$resource."/template.xml");
                      else
                       debug("Template at \"" . $directory.$resource . "\" does not contain  <i>template.xml</i> schema file.");
                  }
              }
          }
          closedir($handle);
          return $templates;
      } 
      
      /**
      * The setter methods of the class
      * @param value to set
      */
      public function setList($value) { $this->list = $value; }
      public function setDir($value) { $this->dir = $value; $this->_initialize();}
      
      /**
      * The getter methods of the class
      * 
      * @return values requested
      */
      public function getList() { return $this->list; }
      public function getDir() { return $this->dir; }
      public function isReady() { return $this->ready; }
  }
?>

Şablon Klasör Taraması

Hazırladığımız şablonların herbirini bir klasör içerisinde barındırıdığımızda, bu klasör altında bulunan tüm şablonların tek tek taranarak sistemde görüntülenmesi için basit bir Klasör Tarama kodu hazırladım. Bu kod, belirtilen klasör altındaki klasörlerin içerisinde eğer varsa template.xml dosyasını şablon olarak yüklenmesini sağlıyor.
Şimdilik bu kodu ufkumuzu genişletmesi için hazırladım, tema yükleme işleminin oldukça kolaylaşacağını ve temaların sisteme uyumluluğunun hat safhada olacağını belirtmeliyim.
Hazırladığım kod şu şekilde:
/**
 * This is the test of directory listing of the adv_templates directory
 * 
 * @author Behçet MUTLU
 * @copyright 2009
 */

require_once("system/system.core.php");
@$dir = $_SERVER["DOCUMENT_ROOT"]."ADVManager/adv_templates/";

function _readdir($dir)
{
    $dh = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
    }
    return $files;
}

$files = _readdir($dir);

foreach($files as $file)
{
    if(is_dir($dir.$file) && $file != "." && $file != "..")
    {
        echo ">$file\n";
        foreach(_readdir($dir.$file) as $temp_file)
        {
            if($temp_file == "template.xml")
            {
                $adv = new ADVTemplate($dir.$file . "/template.xml");
                pre($adv);
            }
        }
    }
}
Bu kodun geri döndürdüğü çıktı ise şu şekildeydi:
>template1

DEBUG:  XML file, "C:/wamp/www/ADVManager/adv_templates/template1/template.xml" loaded successfully.

ADVTemplate Object
(
    [name:ADVTemplate:private] => Template 1
    [author:ADVTemplate:private] => Behçet MUTLU
    [datecreated:ADVTemplate:private] => 25-12-2009
    [copyright:ADVTemplate:private] => 2009 Adres Gezgini
    [website:ADVTemplate:private] => www.adresgezgini.com.tr
    [thumbnail:ADVTemplate:private] => adv_templates/template1/thumb.jpg
    [schema:ADVTemplate:private] => C:/wamp/www/ADVManager/adv_templates/template1/template.xml
    [isReady:ADVTemplate:private] => 1
    [xml:ADVTemplate:private] => Array
        (
            [template] => Array
                (
                    [0] => 

                    [info] => Array
                        (
                            [0] => 
    
                            [name] => Template 1
                            [author] => Behçet MUTLU
                            [copyright] => 2009 Adres Gezgini
                            [website] => www.adresgezgini.com.tr
                            [thumbnail] => adv_templates/template1/thumb.jpg
                            [datecreated] => 25-12-2009
                        )

                    [fields] => Array
                        (
                            [0] => 
    
                            [image] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 
        
                                            [title] => Logo Resmi
                                            [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                                            [default] => library/image_64x64.png
                                            [@id] => image1
                                            [@width] => 64
                                            [@height] => 64
                                        )

                                    [1] => Array
                                        (
                                            [0] => 
        
                                            [title] => Ürün Resmi
                                            [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                                            [default] => library/image_128x169.png
                                            [@id] => image2
                                            [@width] => 128
                                            [@height] => 169
                                        )

                                )

                            [text] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 
        
                                            [title] => Başlık Yazısı
                                            [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                                            [default] => Başlık Yazısı
                                            [font] => Arial
                                            [color] => 000000
                                            [size] => 20
                                            [@id] => text1
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                    [1] => Array
                                        (
                                            [0] => 
        
                                            [title] => Web Site Adresi
                                            [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                                            [default] => www.websiteniz.com.tr
                                            [font] => Arial
                                            [color] => 000000
                                            [size] => 12
                                            [@id] => text2
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                    [2] => Array
                                        (
                                            [0] => 
        
                                            [title] => Buton Yazısı
                                            [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                                            [default] => Bilgi İçin Tıklayınız
                                            [font] => Impact
                                            [color] => 000000
                                            [size] => 18
                                            [@id] => text3
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                )

                        )

                )

        )

    [fields:ADVTemplate:private] => Array
        (
            [0] => 
    
            [image] => Array
                (
                    [0] => Array
                        (
                            [0] => 
        
                            [title] => Logo Resmi
                            [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                            [default] => library/image_64x64.png
                            [@id] => image1
                            [@width] => 64
                            [@height] => 64
                        )

                    [1] => Array
                        (
                            [0] => 
        
                            [title] => Ürün Resmi
                            [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                            [default] => library/image_128x169.png
                            [@id] => image2
                            [@width] => 128
                            [@height] => 169
                        )

                )

            [text] => Array
                (
                    [0] => Array
                        (
                            [0] => 
        
                            [title] => Başlık Yazısı
                            [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                            [default] => Başlık Yazısı
                            [font] => Arial
                            [color] => 000000
                            [size] => 20
                            [@id] => text1
                            [@max] => 100
                            [@min] => 10
                        )

                    [1] => Array
                        (
                            [0] => 
        
                            [title] => Web Site Adresi
                            [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                            [default] => www.websiteniz.com.tr
                            [font] => Arial
                            [color] => 000000
                            [size] => 12
                            [@id] => text2
                            [@max] => 100
                            [@min] => 10
                        )

                    [2] => Array
                        (
                            [0] => 
        
                            [title] => Buton Yazısı
                            [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                            [default] => Bilgi İçin Tıklayınız
                            [font] => Impact
                            [color] => 000000
                            [size] => 18
                            [@id] => text3
                            [@max] => 100
                            [@min] => 10
                        )

                )

        )

    [values:ADVTemplate:private] => Array
        (
            [image1] => library/image_64x64.png
            [image2] => library/image_128x169.png
            [text1] => Başlık Yazısı
            [text2] => www.websiteniz.com.tr
            [text3] => Bilgi İçin Tıklayınız
        )

)

ADVTemplate Class Test

ADVTemplate sınıfımızın test edilmesini sağlamak ve değerlerin bir listesini görmek amaçlı bir kod parçası hazırladım. Bu kod, hem temamızın yaratılması sırasında hem de yaratıldıktan sonraki zamanlarda veri değişikliği yaparak sonçları gösteriyor.
Test kodumuz şu şekilde;
/**
 * This is the tester file of the ADVTemplate class.
 * 
 * @author Behçet MUTLU
 * @copyright 2009 Adres Gezgini
 */

include_once("../system.core.php");

$adv = new ADVTemplate("template-1.xml", array("image1"=>"image1.png", "image2"=>"test2.png"));

$adv->setValueOf("image1","test1.png");
echo "< pre>";
$adv->toString();
echo "< /pre>"; 
Test kodumuzun sonuçlanmış olduğu ekran çıktısı da şöyle;
DEBUG:  XML file, "template-1.xml" loaded successfully.

ADVTemplate Object
(
    [name:ADVTemplate:private] => Template 1
    [author:ADVTemplate:private] => Behçet MUTLU
    [datecreated:ADVTemplate:private] => 25-12-2009
    [copyright:ADVTemplate:private] => 2009 Adres Gezgini
    [website:ADVTemplate:private] => www.adresgezgini.com.tr
    [schema:ADVTemplate:private] => template-1.xml
    [isReady:ADVTemplate:private] => 1
    [xml:ADVTemplate:private] => Array
        (
            [template] => Array
                (
                    [0] => 

                    [info] => Array
                        (
                            [0] => 
    
                            [name] => Template 1
                            [author] => Behçet MUTLU
                            [copyright] => 2009 Adres Gezgini
                            [website] => www.adresgezgini.com.tr
                            [datecreated] => 25-12-2009
                        )

                    [fields] => Array
                        (
                            [0] => 
    
                            [image] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 
        
                                            [title] => Logo Resmi
                                            [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                                            [default] => library/image_64x64.png
                                            [@id] => image1
                                            [@width] => 64
                                            [@height] => 64
                                        )

                                    [1] => Array
                                        (
                                            [0] => 
        
                                            [title] => Ürün Resmi
                                            [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                                            [default] => library/image_128x169.png
                                            [@id] => image2
                                            [@width] => 128
                                            [@height] => 169
                                        )

                                )

                            [text] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 
        
                                            [title] => Başlık Yazısı
                                            [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                                            [default] => Başlık Yazısı
                                            [font] => Arial
                                            [color] => 000000
                                            [size] => 20
                                            [@id] => text1
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                    [1] => Array
                                        (
                                            [0] => 
        
                                            [title] => Web Site Adresi
                                            [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                                            [default] => www.websiteniz.com.tr
                                            [font] => Arial
                                            [color] => 000000
                                            [size] => 12
                                            [@id] => text2
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                    [2] => Array
                                        (
                                            [0] => 
        
                                            [title] => Buton Yazısı
                                            [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                                            [default] => Bilgi İçin Tıklayınız
                                            [font] => Impact
                                            [color] => 000000
                                            [size] => 18
                                            [@id] => text3
                                            [@max] => 100
                                            [@min] => 10
                                        )

                                )

                        )

                )

        )

    [fields:ADVTemplate:private] => Array
        (
            [0] => 
    
            [image] => Array
                (
                    [0] => Array
                        (
                            [0] => 
        
                            [title] => Logo Resmi
                            [description] => Eklemek istediğiniz logo resmini dosyadan seçiniz.
                            [default] => library/image_64x64.png
                            [@id] => image1
                            [@width] => 64
                            [@height] => 64
                        )

                    [1] => Array
                        (
                            [0] => 
        
                            [title] => Ürün Resmi
                            [description] => Eklemek istediğiniz ürün resmini dosyadan seçiniz.
                            [default] => library/image_128x169.png
                            [@id] => image2
                            [@width] => 128
                            [@height] => 169
                        )

                )

            [text] => Array
                (
                    [0] => Array
                        (
                            [0] => 
        
                            [title] => Başlık Yazısı
                            [description] => Eklemek istediğiniz başlık yazısını belirtiniz.
                            [default] => Başlık Yazısı
                            [font] => Arial
                            [color] => 000000
                            [size] => 20
                            [@id] => text1
                            [@max] => 100
                            [@min] => 10
                        )

                    [1] => Array
                        (
                            [0] => 
        
                            [title] => Web Site Adresi
                            [description] => Eklemek istediğiniz web site adresinizi belirtiniz.
                            [default] => www.websiteniz.com.tr
                            [font] => Arial
                            [color] => 000000
                            [size] => 12
                            [@id] => text2
                            [@max] => 100
                            [@min] => 10
                        )

                    [2] => Array
                        (
                            [0] => 
        
                            [title] => Buton Yazısı
                            [description] => Eklemek istediğiniz buton yazısını belirtiniz.
                            [default] => Bilgi İçin Tıklayınız
                            [font] => Impact
                            [color] => 000000
                            [size] => 18
                            [@id] => text3
                            [@max] => 100
                            [@min] => 10
                        )

                )

        )

    [values:ADVTemplate:private] => Array
        (
            [image1] => test.png
            [image2] => test2.png
            [text1] => Başlık Yazısı
            [text2] => www.websiteniz.com.tr
            [text3] => Bilgi İçin Tıklayınız
        )

)

ADVTemplate Class

Reklam temamızı temsil etmesi amacı ile bir sınıf hazırladık. Bu sınıfın görevi, reklam temasının şemasını XML dosyasından okuyarak, gerekli bilgileri kendi içerisinde barındırmasını sağlamak. Reklam yöneticisi tarafından sürekli kullanılacağı ve bir probelme el vermemesi açısından, isReady fonksiyonu ile de hazır olup olmadığı test edilebilir kılındı. Dolayısı ile, herhangi bir memory overflow olayında veya geç okunma olayında kullanıcıya hata yerine sistem kendi kendini yenileyerek her defasında kesin sonuç geri döndürecek.
Hazırlamış olduğum sınıfın kodu şöyle:
/**
 * This class is used to represent a ADV Template to provide required fileld and values along
 * the reqistration process of the advertisement.
 * This class uses Domdocument object library to load xml schema file.
 * 
 * @author Behçet MUTLU
 * @copyright 2009 Adres Gezgini
 */

class ADVTemplate {
    
    private $name;
    private $author;
    private $datecreated;
    private $copyright;
    private $website;
    private $schema;
 private $isReady;
 private $xml;
 private $fields;
 private $values;

 /**
 * This is the general constructor of the ADV Template class that used to initialize the
 * ADV Template itself with given arguments.
 */
 public function __construct($schema = "", $values = array())
 {
  $this->schema = $schema;
        $this->isReady = false;
  
  if($this->schema != "")
   $this->load();
   
        if(sizeof($values) > 0)
            $this->setValues($values);
 }
    
    /**
 * This method is used to initialize the Template from the schema file and set the values
 */
 private function initialize()
 {
  $this->name = $this->xml["template"]["info"]["name"];
        $this->author = $this->xml["template"]["info"]["author"];
        $this->datecreated = $this->xml["template"]["info"]["datecreated"];
        $this->copyright = $this->xml["template"]["info"]["copyright"];
        $this->website = $this->xml["template"]["info"]["website"];
        $this->fields = $this->xml["template"]["fields"];
        
        foreach($this->fields as $fieldType)
        {
            if($fieldType != 0)
                foreach($fieldType as $field)
                {
                    if($field != 0)
                        $this->values[$field["@id"]] = isset($field["default"]) ? $field["default"] : "";
                }
        }
 }

 /**
 * This method is used to initialize the Template from the schema file and set the values
 */
 public function load()
 {
  $xml = new CArray2xml2array();

  if($xml->setXMLFile($this->schema))
        {
            $this->xml = $xml->toArray();
            
            $this->isReady = true;
            
            $this->initialize();
   debug("XML file, \"" . $xml->getXMLFile() . "\" loaded successfully.","pre");
  }
        else
  {
            $this->isReady = false;
   debug("Error: XML file could not be loaded!");
  }
 }
    
    /**
 * This method is used to learn if the xml file is ready or not
 */
 public function ready()
    {
        return $this->isReady;
    }
 
 
 /**
 * This method is used to set value of the class
 */
 public function setName($value) { $this->name = $value; }
 public function setAuthor($value) { $this->name = $value; }
 public function setWebsite($value) { $this->website = $value; }
 public function setDatecreated($value) { $this->datecreated = $value; }
 public function setCopyright($value) { $this->copyright = $value; }
 public function setSchema($value) { $this->schema = $value; }
    public function setFields($value) { $this->fields = $value; }
    public function setXml($value) { $this->xml = $value; }
    
    /**
 * This method is used to get value of the class
 */
 public function getName() { return $this->name; }
    public function getAuthor() { return $this->author; }
    public function getWebsite() { return $this->website; }
    public function getDatecreated() { return $this->datecreated; }
    public function getCopyright() { return $this->copyright; }
    public function getSchema() { return $this->schema; }
    public function getFields() { return $this->fields; }
    public function getXml() { return $this->xml; }
    
    /**
 * This method is used to set value of the class
 */
 public function setValueOf($id, $value)
 {
  $this->values[$id] = $value;
 }
    
    /**
 * This method is used to set values of the class
    * The spacial thing here is setting only given indexes but keeping the rest of
    * the values remain same.
 */
 public function setValues($values)
 {
        foreach($values as $id=>$value)
            $this->setValueOf($id,$value); 
 }
    
    
    /**
 * This method is used to get a string output of this object.
 */
 public function toString()
 {
        print_r($this);
 }
}

25 Aralık 2009 Cuma

XML Reklam Tema Şablonu

Reklamların her biri için bir xml şablon tanım dosyası olması gerektiğinden daha önce bahsetmiştik. Bu xml şablonların en önemli özelliklerinden biri reklam şablonumuz ile ilgili bilgileri ve ihtiyaç duyduğu verileri tanımlamasıydı. Bu doğrultuda ilk reklam şablonumuzu tanıtan bir XML şablonunu hazırladım.
XML şablonumuzun görünümü şöyle;
<?xml version="1.0" encoding="utf-8"?>
<template>
    <info>
        <name>Template 1</name>
        <author>Behçet MUTLU</author>
        <copyright>2009 Adres Gezgini</copyright>
        <website>www.adresgezgini.com.tr</website>
        <datecreated>25-12-2009</datecreated>
    </info>
<fields>
    <image id="image1" width="64" height="64">
        <title>Logo Resmi</title>
        <description>Eklemek istediğiniz logo resmini dosyadan seçiniz.</description>
        <default>library/image_64x64.png</default>
    </image>
    <image id="image2" width="128" height="169">
        <title>Ürün Resmi</title>
        <description>Eklemek istediğiniz ürün resmini dosyadan seçiniz.</description>
        <default>library/image_128x169.png</default>
    </image>
    <text id="text1" max="100" min="10">
        <title>Başlık Yazısı</title>
        <description>Eklemek istediğiniz başlık yazısını belirtiniz.</description>
        <default>Başlık Yazısı</default>
        <font>Arial</font>
        <color>000000</color>
        <size>20</size>
    </text>
    <text id="text2" max="100" min="10">
        <title>Web Site Adresi</title>
        <description>Eklemek istediğiniz web site adresinizi belirtiniz.</description>
        <default>www.websiteniz.com.tr</default>
        <font>Arial</font>
        <color>000000</color>
        <size>12</size>
    </text>
    <text id="text3" max="100" min="10">
        <title>Buton Yazısı</title>
        <description>Eklemek istediğiniz buton yazısını belirtiniz.</description>
        <default>Bilgi İçin TıklayınIz</default>
        <font>Impact</font>
        <color>000000</color>
        <size>18</size>
    </text>
</fields>
</template>

Class AutoLoader

PHP çekirdeğinde bulunan _autoload fonksiyonuna biraz daha esneklik ve yetenek kazandıran bir fonksiyon üstüne yazma(overwrite) işlemini gerçekleştirdik. Bu fonksiyon sayesinde, belirttiğimiz özel klasörler altındaki sınıfların ve yine belirtebileceğimiz birden fazla format ile kullanıldıkları anda otomatik olarak PHP dosyamıza yüklenmesini sağlayacağız.
AutoLoad fonksiyonumuzun içeriği şöyle:
/**
 *
 * @param string $className Class or Interface name automatically
 *              passed to this function by the PHP Interpreter
 */
function autoLoader($className){
    //Directories added here must be
//relative to the script going to use this file.
//New entries can be added to this list
    $directories = array(
      '',
      'classes/'
    );

    //Add your file naming formats here
    $fileNameFormats = array(
      '%s.php',
      '%s.class.php',
      'class.%s.php',
      '%s.inc.php'
    );

    // this is to take care of the PEAR style of naming classes
    $path = str_ireplace('_', '/', $className);
    if(@include_once $path.'.php'){
        return;
    }
   
    foreach($directories as $directory){
        foreach($fileNameFormats as $fileNameFormat){
            $path = $directory.sprintf($fileNameFormat, $className);
            if(file_exists($path)){
                include_once $path;
                return;
            }
        }
    }
}

spl_autoload_register('autoLoader');
Yukarıdaki kod PHP.net web sitesinde örneklerden edinilmiştir.

22 Aralık 2009 Salı

XML Dosyasından Veri Alma

Reklam Yöneticimizin en önde yeteneklerinden biri olacak olan şema okuma işlemi için şemaların XML formatından gelen tanımlarının okunarak bir veri yapısında tutulması gerekiyor. Bunun için yine PHP'de domdocument() nesne kütüphanesini kullanarak hazırladığımız XML sınıfına eklenti hazırladım. Eklenti kodu aşağıdaki gibi geliştirildi:

public function toArray(DOMNode$oDomNode = null)
    {
        // return empty array if dom is blank
        if (is_null($oDomNode) && !$this->doc->hasChildNodes()) {
            return array();
        }
        $oDomNode = (is_null($oDomNode)) ? $this->doc->documentElement : $oDomNode;
        if (!$oDomNode->hasChildNodes()) {
            $mResult = $oDomNode->nodeValue;
        } else {
            $mResult = array();
            foreach ($oDomNode->childNodes as $oChildNode) {
                // how many of these child nodes do we have?
                // this will give us a clue as to what the result structure should be
                $oChildNodeList = $oDomNode->getElementsByTagName($oChildNode->nodeName);
                $iChildCount = 0;
                // there are x number of childs in this node that have the same tag name
                // however, we are only interested in the # of siblings with the same tag name
                foreach ($oChildNodeList as $oNode) {
                    if ($oNode->parentNode->isSameNode($oChildNode->parentNode)) {
                        $iChildCount++;
                    }
                }
                $mValue = $this->toArray($oChildNode);
                $sKey = ($oChildNode->nodeName {0}== '#') ? 0 : $oChildNode->nodeName;
                $mValue = is_array($mValue) ? $mValue[$oChildNode->nodeName] : $mValue;
                // how many of thse child nodes do we have?
                if ($iChildCount > 1) { // more than 1 child - make numeric array
                    $mResult[$sKey][] = $mValue;
                } else {
                    $mResult[$sKey] = $mValue;
                }
            }
            // if the child is < foo>bar< /foo>, the result will be array(bar)
            // make the result just 'bar'
            if (count($mResult) == 1 && isset($mResult[0]) && !is_array($mResult[0])) {
                $mResult = $mResult[0];
            }
        }
        // get our attributes if we have any
        $arAttributes = array();
        if ($oDomNode->hasAttributes()) {
            foreach ($oDomNode->attributes as $sAttrName => $oAttrNode) {
                // retain namespace prefixes
                $arAttributes["@{$oAttrNode->nodeName}"] = $oAttrNode->nodeValue;
            }
        }
        // check for namespace attribute - Namespaces will not show up in the attributes list
        if ($oDomNode instanceof DOMElement && $oDomNode->getAttribute('xmlns')) {
            $arAttributes["@xmlns"] = $oDomNode->getAttribute('xmlns');
        }
        if (count($arAttributes)) {
            if (!is_array($mResult)) {
                $mResult = (trim($mResult)) ? array($mResult) : array();
            }
            $mResult = array_merge($mResult, $arAttributes);
        }
        $arResult = array($oDomNode->nodeName => $mResult);
        return $arResult;
    }

Bu kodun test edilmesinde kullandığım örnek kod ise;

<?php

    /**
     * The tester of the XML domdocument toArray method.
     * 
     * @author Behçet MUTLU
     * @copyright 2009
     */
     
    
    require_once("CArray2xml2array.class.php");
    
    $xml = new CArray2xml2array();
    
    if($xml->setXMLFile("library/file.xml"))
        echo "< p>XML file, < i>\"" . $xml->getXMLFile() . "\"< /i> loaded successfully.< /p>";
    else
    {
        echo "< p>Error: XML file could not be loaded!< /p>";
        exit();
    }
    
    echo "< pre>";
    print_r($xml->toArray());
    echo "< /pre>";



?>

Bu örnek kodun verdiği çıktı da şu şekildeydi:

XML file, "library/file.xml" loaded successfully.

Array
(
    [movie] => Array
        (
            [0] => 

            [clip] => Array
                (
                    [0] => Array
                        (
                            [@id] => upState
                            [@import] => up.png
                        )

                    [1] => Array
                        (
                            [@id] => downState
                            [@import] => down.png
                        )

                    [2] => Array
                        (
                            [@id] => overState
                            [@import] => over.png
                        )

                    [3] => Array
                        (
                            [@id] => disabledState
                            [@import] => disabled.png
                        )

                )

            [frame] => Array
                (
                    [0] => 
  
                    [library] => Array
                        (
                            [0] => 
    
                            [clip] => Array
                                (
                                    [0] => 
      
                                    [frame] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => upState
                                                            [@depth] => 1
                                                        )

                                                    [stop] => 
                                                    [@name] => Up
                                                )

                                            [1] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => downState
                                                            [@depth] => 2
                                                        )

                                                    [stop] => 
                                                    [@name] => Down
                                                )

                                            [2] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => overState
                                                            [@depth] => 3
                                                        )

                                                    [stop] => 
                                                    [@name] => Over
                                                )

                                            [3] => Array
                                                (
                                                    [0] => 
        
                                                    [place] => Array
                                                        (
                                                            [@id] => disabledState
                                                            [@depth] => 4
                                                        )

                                                    [stop] => 
                                                    [@name] => Disabled
                                                )

                                        )

                                    [@id] => testButton
                                )

                        )

                )

            [place] => Array
                (
                    [@id] => testButton
                    [@depth] => 1
                )

            [@width] => 320
            [@height] => 240
            [@framerate] => 12
        )

)