/**
  * Executor
  *
  * @throws UpdateDomainContactTask_Exception
  */
 public function Run($userid = null)
 {
     try {
         $Factory = RegistryModuleFactory::GetInstance();
         $Registry = $Factory->GetRegistryByExtension($this->Domain->Extension);
         $Manifest = $Registry->GetManifest();
         if ($userid && $this->Domain->UserID != $userid) {
             throw new UpdateDomainContactAction_Exception(_("You don't have permissions for manage this domain"), UpdateDomainContactAction_Exception::DOMAIN_NOT_BELONGS_TO_USER);
         }
         $OldContact = $this->Domain->GetContact($this->contact_type);
         if ($this->NewContact && $this->NewContact->UserID != $this->Domain->UserID) {
             throw new UpdateDomainContactAction_Exception(_("You don't have permissions for using this contact"), UpdateDomainContactAction_Exception::CONTACT_NOT_BELONGS_TO_USER);
         }
         $trade = $Manifest->GetRegistryOptions()->ability->trade == '1';
         if ($this->contact_type != CONTACT_TYPE::REGISTRANT || !$trade) {
             try {
                 $Registry->UpdateDomainContact($this->Domain, $this->contact_type, $OldContact, $this->NewContact);
                 return $this->Domain->HasPendingOperation(Registry::OP_UPDATE) ? UpdateDomainContactAction_Result::PENDING : UpdateDomainContactAction_Result::OK;
             } catch (Exception $e) {
                 throw new UpdateDomainContactAction_Exception(sprintf(_("Cannot change contact. Reason: %s"), $e->getMessage()));
             }
         } else {
             // Execute trade when:
             // - Trade invoice is paid
             // - Previous trade failed
             if ($this->Domain->IncompleteOrderOperation == INCOMPLETE_OPERATION::DOMAIN_TRADE || $this->Invoice && $this->Invoice->Status == INVOICE_STATUS::PAID) {
                 $this->Domain->SetContact($this->NewContact, CONTACT_TYPE::REGISTRANT);
                 $this->Domain->IncompleteOrderOperation = null;
                 $extra["requesttype"] = "ownerChange";
                 try {
                     $trade = $Registry->ChangeDomainOwner($this->Domain, 1, $extra);
                     return $this->Domain->HasPendingOperation(Registry::OP_TRADE) ? UpdateDomainContactAction_Result::PENDING : UpdateDomainContactAction_Result::OK;
                 } catch (Exception $e) {
                     $this->Domain->IncompleteOrderOperation = INCOMPLETE_OPERATION::DOMAIN_TRADE;
                     if ($this->Invoice) {
                         $this->Invoice->ActionStatus = INVOICE_ACTION_STATUS::FAILED;
                         $this->Invoice->ActionFailReason = $e->getMessage();
                     }
                     throw new UpdateDomainContactAction_Exception(sprintf(_("Cannot change contact. Reason: %s"), $e->getMessage()));
                 }
             } else {
                 // Issue an invoice for trade operation
                 $invoiceid = $this->Db->GetOne("SELECT * FROM invoices WHERE status=? AND itemid=? AND purpose=?", array(INVOICE_STATUS::PENDING, $this->Domain->ID, INVOICE_PURPOSE::DOMAIN_TRADE));
                 if (!$invoiceid) {
                     $this->Domain->SetExtraField("NewRegistrantCLID", $this->NewContact->CLID);
                     DBDomain::GetInstance()->Save($this->Domain);
                     $Invoice = new Invoice(INVOICE_PURPOSE::DOMAIN_TRADE, $this->Domain->ID, $userid);
                     $Invoice->Description = sprintf(_("%s domain name trade"), $this->Domain->GetHostName());
                     $Invoice->Save();
                     $this->Invoice = $Invoice;
                     return UpdateDomainContactAction_Result::INVOICE_GENERATED;
                 } else {
                     throw new UpdateDomainContactAction_Exception(_("Another domain trade is already initiated"));
                 }
             }
         }
     } catch (Exception $e) {
         throw new UpdateDomainContactAction_Exception($e->getMessage());
     }
 }
 public function Run($userid)
 {
     try {
         $Factory = RegistryModuleFactory::GetInstance();
         $Registry = $Factory->GetRegistryByExtension($this->Domain->Extension);
         $host_as_attr = (bool) $Registry->GetManifest()->GetRegistryOptions()->ability->hostattr;
     } catch (Exception $e) {
         throw new UpdateDomainNameserversAction_Exception(sprintf(_("Cannot change nameservers. Reason: %s"), $e->getMessage()));
     }
     // Check that all nameserver hosts are registered
     foreach ($this->nslist as &$NS) {
         $glue_record = preg_match("/^(.*)\\.{$this->Domain->GetHostName()}\$/", $NS->HostName, $matches);
         if ($glue_record) {
             $known_ns = $this->Db->GetRow("SELECT * FROM nhosts WHERE hostname=? AND domainid=?", array($matches[1], $this->Domain->ID));
             if (!$known_ns) {
                 if ($host_as_attr) {
                     // No need to register nameserver hosts
                     $this->Db->Execute("INSERT INTO nhosts SET domainid = ?, hostname = ?, ipaddr = ?", array($this->Domain->ID, $matches[1], $NS->IPAddr));
                 } else {
                     throw new UpdateDomainNameserversAction_Exception(sprintf(_("Nameserver %s does not exist"), $NS->HostName), UpdateDomainNameserversAction_Exception::NAMESERVERHOST_NOT_REGISTERED);
                 }
             } else {
                 $NS = new NameserverHost($NS->HostName, $host_as_attr ? $NS->IPAddr : $known_ns['ipaddr']);
                 $NS->ID = $known_ns['id'];
             }
         }
     }
     // Perform nameservers update
     try {
         $Changes = new Changelist($this->Domain->GetNameserverList(), $this->nslist);
         $Registry->UpdateDomainNameservers($this->Domain, $Changes);
         $DBNSHost = DBNameserverHost::GetInstance();
         $DBNSHost->SaveList($this->Domain->GetNameserverHostList(), $this->Domain->ID);
         return $this->Domain->HasPendingOperation(Registry::OP_UPDATE) ? UpdateDomainNameserversAction_Result::PENDING : UpdateDomainNameserversAction_Result::OK;
     } catch (Exception $e) {
         throw new UpdateDomainNameserversAction_Exception(sprintf(_("Cannot change nameservers. Reason: %s"), $e->getMessage()));
     }
 }