public function cloneModel($ids = null, $data = array()) { $cName = 'copy_' . $this->campaign_name; $cUrl = ''; // make sure fulfillment is set properly if ($this->attach && !$this->fulfillment_id) { $this->fulfillment_id = '0'; // setting the value to string '0' seems to work, while integer 0 causes the save() to fail } // grab possible passed over values if (count($data)) { if (strlen($data['name'])) { $cName = $data['name']; } if (strlen($data['url']) && !$this->attach) { // check url integrity if (stripos($data['url'], 'http://') !== false) { $data['url'] = str_ireplace('http://', '', $data['url']); } else { if (stripos($data['url'], 'https://') !== false) { $data['url'] = str_ireplace('https://', '', $data['url']); } } // make sure we have a trailing / at the end of the url if (substr($data['url'], -1) != '/') { $data['url'] .= '/'; } $replaceUrl = $this->url; $withThisUrl = $data['url']; } } $this->campaign_name = $cName; // replace urls if (isset($withThisUrl)) { $this->url = str_ireplace($replaceUrl, $withThisUrl, $this->url); $this->order_url = str_ireplace($replaceUrl, $withThisUrl, $this->order_url); $this->return_url = str_ireplace($replaceUrl, $withThisUrl, $this->return_url); } // original camp id $oid = $this->campaign_id; // clone original camp /*if(!parent::cloneModel($ids)) { return FALSE; }*/ if ($this->beforeClone()) { $db = self::$_msql; $pk = $this->getPrimaryField(); $tableName = $this->tableName(); $this->cloneId = $this->{$pk}; // make sure there's something here if (!$this->fulfillment_id) { $this->fulfillment_id = '0'; } if (!$tableName || !$pk) { return FALSE; } unset($this->{$pk}); $this->setIsNewRecord(TRUE); if ($this->save()) { $this->afterClone(); //return true; } else { return false; } } else { return false; } // CampaignShippings $campShip = new CampaignShippings('CampaignShipping'); $campShip->load($oid); foreach ($campShip->shipping as $cs) { $cs->cloneModel(); $cs->campaign_id = $this->campaign_id; if (!$cs->save()) { continue; } } unset($campShip); // CampaignEmails $campEmails = new CampaignEmails('CampaignEmail'); $campEmails->load($oid); foreach ($campEmails->emails as $ce) { $ce->cloneModel(); $ce->campaign_id = $this->campaign_id; if (!$ce->save()) { continue; } } unset($campEmails); // CampaignProducts $campProducts = new CampaignProducts('CampaignProduct'); $campProducts->load($oid); $startingUpsells = array(); foreach ($campProducts->products as $cp) { $cp->cloneModel(); $cp->campaign_id = $this->campaign_id; // check for upsell products if ($cp->upsell_id) { $startingUpsells[] = $cp->upsell_id; } if (!$cp->save()) { continue; } } $upsells = array(); foreach ($startingUpsells as $uid) { $upsells = array_merge($upsells, self::upsellDigger($uid)); } sort($upsells); $upsellsMap = array(); $allUpsells = new Upsells('Upsell'); $allUpsells->loadUpsellsByIds($upsells); // first pass. clone upsells and build mapping array foreach ($allUpsells->upsells as $u) { $uid = $u->upsell_id; // replace url if (isset($withThisUrl)) { $u->url = str_ireplace($replaceUrl, $withThisUrl, $u->url); } $u->campaign_id = $this->campaign_id; $u->cloneModel(); $upsellsMap[$uid] = $u->upsell_id; } // second pass. mapping new upsell ids foreach ($allUpsells->upsells as $u) { if ($u->yes_upsell_id) { $u->yes_upsell_id = $upsellsMap[$u->yes_upsell_id]; } if ($u->no_upsell_id) { $u->no_upsell_id = $upsellsMap[$u->no_upsell_id]; } if (!$u->save()) { continue; } } // fix camp products with proper new upsells foreach ($campProducts->products as $cp) { if ($cp->upsell_id) { $cp->upsell_id = $upsellsMap[$cp->upsell_id]; } if (!$cp->save()) { continue; } } unset($campProducts); unset($allUpsells); return true; }