Can we have "Item Before Shown" event in Gallery 3?

gopu
gopu's picture

Joined: 2009-08-28
Posts: 137
Posted: Sun, 2010-09-12 08:40

Hi,

Currently I am working on multilang module for Gallery 3, feature marked for "Field of Dreams" Milestone

To develop the module I need to have the "Item Before Shown" event, to replace multilang content for an item before it's shown.

Would it possible to have this event raised in core?
Or else is there any way that I can get hold of item to modify its content before it's shown? I did lot of R&D but did not find a way.

thanks,
gopu
--------------------------------------------------------------------------------
my wings of love - work, experince, learnings, open source, technologies on my way to God

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7993
Posted: Sun, 2010-09-12 20:37

Hrm. That's tricky. Let me make sure I understand correctly -- you want it such that if the theme asks for the photo title (eg, <?= $photo->title ?>) we intercept that request and provide the appropriate multilang version, right? We also want to do this in a way that doesn't cause

I believe we can use the Kohana Cascading Filesystem for this. In your module, create models/MY_Item_Model.php and put this in it:

<?
class Item_Model extends Item_Model_Core {
  public function __get($column) {
    if ($column == "title") {
      return "multilang-" . parent::__get($column);
    }
    return parent::__get($column);
  }
}

Then in the __get() function you can have a chance to intercept any requests for Item_Model fields. Note that you have to get the latest experimental code for this to work, because I had to make a small change to enable the override ability for the item model.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git

 
gopu
gopu's picture

Joined: 2009-08-28
Posts: 137
Posted: Mon, 2010-09-13 17:39

Waw !!! this works with the latest code.... thanks Bharat... I should have put this question way back instead scratching my head all through :).

One more question:
Many gallery users prefer a language specific URL also...
For this we need to be able to resolve the multilang URL to right item.
I have found a way to do it. The way I have done it is:

in gallery_ready event register for system.post_routing:

static function gallery_ready() {
    Event::add("system.post_routing", array("multilang3_url", "parse_url"));
  }

Then in multilang3_url

 
class multilang3_url extends url_Core {
  static function parse_url() {
    // if $arguments is set, it means item is a non-multilang one,
    // and is  loaded successfully in url::parse_url
    if (Router::$arguments){
      return;
    }

    if (Router::$controller) {
      return;
    }

    if ($_SERVER["SCRIPT_NAME"] && "/" . Router::$current_uri == $_SERVER["SCRIPT_NAME"]) {
      return;
    }

    $item = self::get_item_from_multilang_uri(Router::$current_uri);
    if ($item && $item->loaded()) {
      Router::$controller = "{$item->type}s";
      Router::$controller_path = MODPATH . "gallery/controllers/{$item->type}s.php";
      Router::$method = "show";
      Router::$arguments = array($item);
    }
  }

  static function get_item_from_multilang_uri($uri) {
    $current_uri = html_entity_decode($uri, ENT_QUOTES);

    $multilang = ORM::factory("multilang")->where("relative_url_cache", "=", $current_uri)->find();
    if (!$multilang->loaded()) {
      return null;
    }

    $id = $multilang->itemid;
    $item = ORM::factory("item")->where("id", "=", $id)->find();
    if ($item->loaded()) {
    }

    return $item;
  }

}

This is clearly an inefficient approach because, the invoking of multilang URL resolution happens when URL parsing fails in url::get_item_from_uri. Ideally it should be invoked when current language is not default language.

Can you please suggest me a better way?

thanks,
gopu

-----------------------------------------
my wings of love - work, experince, learnings, open source, technologies on my way to God

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7993
Posted: Wed, 2010-09-15 03:00

This one is harder. We are currently overloading url.php in modules/gallery/helpers/MY_url.php -- you can't overload it again or you'll replace our code. We'll have to build an API for this, which will take a little while.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git