Raza Mehdi's Blog

Archive for January, 2011

How to properly automate a MySQL database with triggers and stored procedure

by on Jan.28, 2011, under PHP

Hi guys!

I would like to share with you this great article on implementing stored procedures with triggers …

http://suite101.com/article/mysql-stored-procedures-and-triggers-a71091

2 Comments more...

Get list of all modules with all controllers and actions in Zend Framework

by on Jan.06, 2011, under Zend Framework

Hi guys!

Finally back after a long time. I have been really busy at work.  In one of the projects, i had to get list of modules plus controllers and their respective actions for ACL configuration purposes .. I implemented the said task using action helper and controller plugin in Zend Framework. I am listing the Action helper code here. Also, when done with ACL, i will post that code here as well …

Before getting started, i am going to assume that you have configured Zend Framework installation on your local machine and setup your application through a virtual host.

First, open the Bootstrap.php in your application directory and put the following code:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initAutoLoad(){
        $autoLoader = Zend_Loader_Autoloader::getInstance();
        $autoLoader->registerNamespace('Application_');

        return $autoLoader;
    }

    protected function _initActionHelpers() {
        Zend_Controller_Action_HelperBroker::addHelper(new Application_Action_Helper_AssetsList());
    }

Now do the following:

  1. Create a folder called Application in your site’s library folder.
  2. Then create sub-folder called Actionin the Applicationfolder.
  3. Again create a sub-folder called Helperin the Actionfolder.
  4. Now a create a file called AssetsList.phpin the plugin folder.

In the AssetsList.php folder, write the following code:

<?php
class Application_Action_Helper_AssetsList extends Zend_Controller_Action_Helper_Abstract {

    public function  direct() {
    }

    public function getList() {
        $module_dir = $this->getFrontController()->getControllerDirectory();
        $resources = array();

        foreach($module_dir as $dir=>$dirpath) {
            $diritem = new DirectoryIterator($dirpath);
            foreach($diritem as $item) {
                if($item->isFile()) {
                    if(strstr($item->getFilename(),'Controller.php')!=FALSE) {
                        include_once $dirpath.'/'.$item->getFilename();
                    }
                }
            }

            foreach(get_declared_classes() as $class){
                if(is_subclass_of($class, 'Zend_Controller_Action')) {
                    $functions = array();

                    foreach(get_class_methods($class) as $method) {
                        if(strstr($method, 'Action')!=false) {
                            array_push($functions,substr($method,0,strpos($method,"Action")));
                        }
                    }
                    $c = strtolower(substr($class,0,strpos($class,"Controller")));
                    $resources[$dir][$c] = $functions;
                }
            }
        }
        return $resources;
    }
}

To get the list, simply put the following line in any controller action:

$resources = $this->_helper->AssetsList->getList();

Hopefully, this helps out everyone. I will post the dynamic ACL plugin code in coming days when i am finished with it.

6 Comments more...

How to create Filefield Entries in Drupal?

by on Jan.06, 2011, under Drupal

Hey guys! I wrote a tutorial on how to programmatically create nodes in Drupal. In this tutorial, i will show you how to write FileField entries using a Drupal module.

function YOURMODULE_CALLBACK() {

	$arrfiles = return filePaths(YOUR_IMAGES_DIRPATH);

	$filesinfo = array();

	foreach($arrfiles as $file) {

		$fn = basename($file);

		//copying from source folder to drupal's sites/default/files

		$cp = copy($file,'/sites/default/files'.$fn);

		if($cp) {
			$files = new stdClass();
			$files->filename = basename($file);
			$files->filepath = 'sites/default/files'.$fn;
			$files->filemime = file_get_mimetype(basename($file));
			$files->filesize = filesize($file);
			$files->uid = 1;
			$files->status = FILE_STATUS_PERMANENT;
			$files->timestamp = time();

			drupal_write_record('files', $files);

			$filesinfo[] = array(
				'fid' => $files->fid,
				'title' => basename($files->filename),
				'filename' => $files->filename,
				'filepath' => $files->filepath,
				'filesize' => $files->filesize,
				'mimetype' => file_get_mimetype(basename($file)),
				'description' => basename($file),
				'list' => 1,
			);
		}
	}

	$node = node_load(NODEID);

	$node->field_images = $filesinfo;

	node_save($node);

}

function filePaths($path) {

	$dir = new DirectoryIterator($path);
	$filelist = array();

	foreach($dir as $file) {
		if($file->isFile()) {
			$filelist[] = $file->getFilename();

		}

		if($file->isDir()) {
			filePaths($file->getPath());
		}
	}
	return $filelist;
}
2 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!