<?php

class SP_LogicHookSPCampaign
{
    
    /**
     * 
     * @param SugarBean $bean
     * @param string $event
     * @param array $args
     */
    public function beforeDelete($bean, $event, $args)
    {
        if ($bean->load_relationship("sp_campaign_sp_campaignmember"))
        {
            global $db;

            $query = "SELECT `sp_campaign_sp_campaignmembersp_campaignmember_idb` FROM `sp_campaign_sp_campaignmember_c` WHERE " .
                "`deleted` = 0 AND `sp_campaign_sp_campaignmembersp_campaign_ida` = '" . $bean->id . "'";
            $result = $db->query($query);

            while ($row = $result->fetch_row())
            {
                BeanFactory::deleteBean("SP_CampaignMember", $row[0]);
            }
        }
    }
}
