public function deleteAction()
 {
     if (!isset($_GET['id'])) {
         $this->redirect();
     }
     $landing_page_id = $_GET['id'];
     $lp = LandingPageModel::model()->getRow(array('conditions' => array('landing_page_id' => $landing_page_id)));
     if (!$lp) {
         $this->redirect();
     }
     $lp->delete();
     $this->redirect();
 }
 protected function editCampaign()
 {
     DB::startTransaction();
     if (!($campaign = CampaignModel::model()->getRowFromPk($_POST['campaign_id']))) {
         echo json_encode(array('message' => 'Could not save: invalid campaign ID'));
         DB::rollback();
         BTApp::end();
     }
     $campaign->name = $_POST['name'];
     $campaign->cloaker_id = $_POST['cloaker_id'];
     $campaign->slug = $_POST['slug'];
     $campaign->save();
     switch ($campaign->type) {
         case 1:
             foreach ($campaign->offers as $offer) {
                 if (!in_array($offer->id(), $_POST['campaign_lpoffer_id'])) {
                     $offer->delete();
                 }
             }
             for ($i = 0, $cnt = count($_POST['campaign_lpoffer_id']); $i < $cnt; $i++) {
                 $id = $_POST['campaign_lpoffer_id'][$i];
                 if (!$id) {
                     //new
                     $offer = OfferModel::model();
                     $offer->aff_network_id = $_POST['lpoffer_aff_network_id'][$i];
                     $offer->name = $_POST['lpoffer_name'][$i];
                     $offer->url = $_POST['lpoffer_url'][$i];
                     $offer->payout = $_POST['lpoffer_payout'][$i];
                     if (!$offer->save()) {
                         echo json_encode(array('message' => 'Could not add offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                     $campoffer = CampaignOfferModel::model();
                     $campoffer->campaign_id = $campaign->id();
                     $campoffer->position = 0;
                     $campoffer->weight = 0;
                     $campoffer->offer_id = $offer->id();
                     if (!$campoffer->save()) {
                         echo json_encode(array('message' => 'Could not add campaign offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                 } else {
                     //edit
                     $campoffer = CampaignOfferModel::model()->getRowFRomPk($id);
                     $offer = $campoffer->offer;
                     $offer->aff_network_id = $_POST['lpoffer_aff_network_id'][$i];
                     $offer->name = $_POST['lpoffer_name'][$i];
                     $offer->url = $_POST['lpoffer_url'][$i];
                     $offer->payout = $_POST['lpoffer_payout'][$i];
                     if (!$offer->save()) {
                         echo json_encode(array('message' => 'Could not save offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                 }
             }
             foreach ($campaign->landing_pages as $lp) {
                 if (!in_array($lp->id(), $_POST['campaign_lp_id'])) {
                     $lp->delete();
                 }
             }
             for ($i = 0, $cnt = count($_POST['campaign_lp_id']); $i < $cnt; $i++) {
                 $id = $_POST['campaign_lp_id'][$i];
                 if (!$id) {
                     //new
                     $lp = LandingPageModel::model();
                     $lp->name = $_POST['lp_name'][$i];
                     $lp->url = $_POST['lp_url'][$i];
                     if (!$lp->save()) {
                         echo json_encode(array('message' => 'Could not add LPs'));
                         DB::rollback();
                         BTApp::end();
                     }
                     $camp_lp = CampaignLPModel::model();
                     $camp_lp->campaign_id = $campaign->id();
                     $camp_lp->landing_page_id = $lp->id();
                     $camp_lp->weight = $_POST['lp_weight'][$i];
                     if (!$camp_lp->save()) {
                         echo json_encode(array('message' => 'Could not add campaign LPs'));
                         DB::rollback();
                         BTApp::end();
                     }
                 } else {
                     //edit
                     $camp_lp = CampaignLPModel::model()->getRowFromPk($id);
                     $camp_lp->weight = $_POST['lp_weight'][$i];
                     if (!$camp_lp->save()) {
                         echo json_encode(array('message' => 'Could not save campaign LPs'));
                         DB::rollback();
                         BTApp::end();
                     }
                     $lp = $camp_lp->landing_page;
                     $lp->name = $_POST['lp_name'][$i];
                     $lp->url = $_POST['lp_url'][$i];
                     if (!$lp->save()) {
                         echo json_encode(array('message' => 'Could not save LPs'));
                         DB::rollback();
                         BTApp::end();
                     }
                 }
             }
             break;
         case 2:
             foreach ($campaign->offers as $offer) {
                 if (!in_array($offer->id(), $_POST['campaign_offer_id'])) {
                     $offer->delete();
                 }
             }
             for ($i = 0, $cnt = count($_POST['campaign_offer_id']); $i < $cnt; $i++) {
                 $id = $_POST['campaign_offer_id'][$i];
                 if (!$id) {
                     //new
                     $offer = OfferModel::model();
                     $offer->aff_network_id = $_POST['offer_aff_network_id'][$i];
                     $offer->name = $_POST['offer_name'][$i];
                     $offer->url = $_POST['offer_url'][$i];
                     $offer->payout = $_POST['offer_payout'][$i];
                     if (!$offer->save()) {
                         echo json_encode(array('message' => 'Could not save offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                     $campoffer = CampaignOfferModel::model();
                     $campoffer->campaign_id = $campaign->id();
                     $campoffer->position = 0;
                     $campoffer->weight = $_POST['offer_weight'][$i];
                     $campoffer->offer_id = $offer->id();
                     if (!$campoffer->save()) {
                         echo json_encode(array('message' => 'Could not save offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                 } else {
                     //edit
                     $campoffer = CampaignOfferModel::model()->getRowFRomPk($id);
                     $campoffer->weight = $_POST['offer_weight'][$i];
                     if (!$campoffer->save()) {
                         echo json_encode(array('message' => 'Could not save offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                     $offer = $campoffer->offer;
                     $offer->name = $_POST['offer_name'][$i];
                     $offer->aff_network_id = $_POST['offer_aff_network_id'][$i];
                     $offer->url = $_POST['offer_url'][$i];
                     $offer->payout = $_POST['offer_payout'][$i];
                     if (!$offer->save()) {
                         echo json_encode(array('message' => 'Could not save offers'));
                         DB::rollback();
                         BTApp::end();
                     }
                 }
             }
             break;
     }
     foreach ($_POST['opt'] as $name => $val) {
         $opt = $campaign->options[$name];
         $opt->value = $val;
         if (!$opt->save()) {
             DB::rollback();
             echo json_encode(array('message' => 'Could not save options'));
             BTApp::end();
         }
     }
     foreach ($campaign->options as $option) {
         if (strpos($option->name, 'pass_') === 0) {
             $option->delete();
         }
     }
     for ($j = 0, $cnt = count($_POST['variable_name']); $j < $cnt; $j++) {
         $name = $_POST['variable_name'][$j];
         $name = 'pass_' . $name;
         if ($campaign->type == 1) {
             $pass_lp = $_POST['variable_lp'][$j];
             $pass_offer = $_POST['variable_offer'][$j];
             $val = json_encode(array('lp' => $pass_lp, 'offer' => $pass_offer));
         } else {
             $val = json_encode(array('offer' => '1'));
         }
         if (!$campaign->addOption($name, $val)) {
             DB::rollback();
             echo json_encode(array('message' => 'Could not save Variables Passthroughs'));
             BTApp::end();
         }
     }
     DB::commit();
     echo json_encode(array('message' => '2', 'campaign_id' => $campaign->id()));
 }