Tofuwarrior
Tofuwarrior

Reputation: 669

how can I add a function to be accessed from multiple modules in single app in sf 1.4

I have a package checking function that needs to check if the logged in user has a valid package. This functionality is identical across all modules but is not needed in every module action.

protected function checkPackage()
{
  if($this->MscdbProject->getCurrentPackage()){
    return;     
  }
  $this->getUser()->setFlash('error',"Your package has expired, to restore access to your project please contact us to renew it.");
  $this->redirect('project/index');
}

I need to access this function in most module actions across multiple modules but it relies on the MscdbProject variable which is pulled in the individual actions, this is sllightly different in different modules so cannot be standardised.

Currently I have this function duplicated in every module and I call it from every action where it is needed.

How can I refactor this to avoid this duplication? It feels like a filter should be able to do this but I have to have pulled the MscdbProject instance before I can check the package status.

Thanks

Upvotes: 1

Views: 31

Answers (1)

Marek
Marek

Reputation: 7433

Change the base class for your action classes to your own:

class myAction extends sfAction {
    abstract protected function getMscdbProject();

    protected function checkPackage()
    {
      if($this->getMscdbProject()->getCurrentPackage()){
        return;     
      }
      $this->getUser()->setFlash('error',"Your package has expired, to restore access to your project please contact us to renew it.");
      $this->redirect('project/index');
    }
}

Now your module action have to implement getMscdbProject() using their own logic.

If you are using generated modules, change this key in generator.yml:

generator:
  param:
    actions_base_class: myAction

Upvotes: 2

Related Questions