Events hook
Permalink
June 02, 2017 at 6:44 PM
I have an existing system that was built for me. I have a problem in that an event hook fires off when ever an entry is added or edited in the custom object. What I really want is for the event to fire off only when a new entry is added, so not when an existing one is edited. The controller code is below. Any help appreciated.
<?php
defined ( 'C5_EXECUTE' ) or die ( _ ( "Access Denied." ) ) ;
class DashboardChurchesAddChurchController extends Controller {
public function on_start( ) {
$this -> error = Loader:: helper ( 'validation/error' ) ;
Loader:: model ( 'attribute/categories/churches' , 'churches' ) ;
}
public function view( ) {
$html = Loader:: helper ( 'html' ) ;
$form = Loader:: helper ( 'form' ) ;
$this -> set ( 'form' , $form ) ;
}
private function saveData( $Church ) {
Loader:: model ( 'attribute/categories/churches' , 'churches' ) ;
$attribs = ChurchesAttributeKey:: getList ( ) ;
<?php
defined ( 'C5_EXECUTE' ) or die ( _ ( "Access Denied." ) ) ;
class DashboardChurchesAddChurchController extends Controller {
public function on_start( ) {
$this -> error = Loader:: helper ( 'validation/error' ) ;
Loader:: model ( 'attribute/categories/churches' , 'churches' ) ;
}
public function view( ) {
$html = Loader:: helper ( 'html' ) ;
$form = Loader:: helper ( 'form' ) ;
$this -> set ( 'form' , $form ) ;
}
private function saveData( $Church ) {
Loader:: model ( 'attribute/categories/churches' , 'churches' ) ;
$attribs = ChurchesAttributeKey:: getList ( ) ;
foreach ( $attribs as $ak ) {
$ak -> saveAttributeForm ( $Church ) ;
}
$Church -> reindex ( ) ;
Events:: fire ( 'NEW_CHURCH_ADDED' , $Church ) ;
}
public function add( ) {
Loader:: model ( 'churches' , 'churches' ) ;
if ( $this -> isPost ( ) ) {
$this -> validate ( ) ;
if ( ! $this -> error -> has ( ) ) {
$Church = Churches:: add ( ) ;
$this -> saveData ( $Church ) ;
if ( $this -> post ( 'front_side' ) ) {
$this -> redirect ( '/index.php/Churches/Churches/' . $Church -> ChurchesID . '/' ) ;
} else {
$this -> redirect ( '/dashboard/churches/search/' , 'church_added' ) ;
}
}
}
}
public function edit( $ChurchID ) {
Loader:: model ( 'churches' , 'churches' ) ;
$Church = Churches:: getByID ( $ChurchID ) ;
if ( ! empty ( $Church ) ) {
$this -> set ( 'Church' , $Church ) ;
} else {
$this -> redirect ( '/dashboard/churches/search/' ) ;
}
}
public function update( ) {
$this -> edit ( $this -> post ( 'ChurchesID' ) ) ;
if ( $this -> isPost ( ) ) {
$this -> validate ( ) ;
if ( ! $this -> error -> has ( ) ) {
Loader:: model ( 'churches' , 'churches' ) ;
$Church = Churches:: getByID ( $this -> post ( 'ChurchesID' ) ) ;
$this -> saveData ( $Church ) ;
$this -> redirect ( '/dashboard/churches/search/' , 'church_updated' ) ;
}
}
}
public function delete( $ChurchID = null ) {
Loader:: model ( 'churches' , 'churches' ) ;
foreach ( $_GET [ 'ChurchesID' ] as $ChurchID ) {
$Church = Churches:: getByID ( $ChurchID ) ;
if ( is_object ( $Church ) ) {
$Church -> delete ( ) ;
}
}
$this -> redirect ( '/dashboard/churches/search/' , 'church_deleted' ) ;
}
protected function validate( ) {
}
}
below
in the add function that should fix your issue.