/** * Updates a purchase order with transaction information from order events * * @author Jonathan Davis * @since 1.2 * * @param OrderEvent $Event The order event passed by the action hook * @return void **/ public static function event($Event) { $Purchase = $Event->order(); if (!$Purchase) { shopp_debug('Cannot update. No event order.'); return; } // Transaction status is the same as the event, no update needed if ($Purchase->txnstatus == $Event->name) { shopp_debug('Transaction status (' . $Purchase->txnstatus . ') for purchase order #' . $Purchase->id . ' is the same as the new event, no update necessary.'); return; } $status = false; $txnid = false; // Set transaction status from event name $txnstatus = $Event->name; if ('refunded' == $txnstatus) { // Determine if this is fully refunded (previous refunds + this refund amount) if (empty($Purchase->events)) { $Purchase->load_events(); } // Not refunded if less than captured, so don't update txnstatus if ($Purchase->refunded + $Event->amount < $Purchase->captured) { $txnstatus = false; } } if ('voided' == $txnstatus) { // Determine if the transaction has been cancelled if (empty($Purchase->events)) { $Purchase->load_events(); } if ($Purchase->captured) { $txnstatus = false; } // If previously captured, don't mark voided } if ('shipped' == $txnstatus) { $txnstatus = false; } // 'shipped' is not a valid txnstatus // Set order workflow status from status label mapping $labels = (array) shopp_setting('order_status'); $events = (array) shopp_setting('order_states'); $key = array_search($Event->name, $events); if (false !== $key && isset($labels[$key])) { $status = (int) $key; } // Set the transaction ID if available if (isset($Event->txnid) && !empty($Event->txnid)) { $txnid = $Event->txnid; } $updates = compact('txnstatus', 'txnid', 'status'); $updates = array_filter($updates); $data = sDB::escape($updates); $data = array_map(create_function('$value', 'return "\'$value\'";'), $data); $dataset = ShoppDatabaseObject::dataset($data); if (!empty($dataset)) { $table = ShoppDatabaseObject::tablename(self::$table); $query = "UPDATE {$table} SET {$dataset} WHERE id='{$Event->order}' LIMIT 1"; sDB::query($query); } $Purchase->updates($updates); return; }
public function save() { $data = sDB::prepare($this, $this->_map); $id = $this->{$this->_key}; if (!empty($this->_map)) { $remap = array_flip($this->_map); if (isset($remap[$this->_key])) { $id = $this->{$remap[$this->_key]}; } } // Insert new record $data['modified'] = "'" . current_time('mysql') . "'"; $dataset = ShoppDatabaseObject::dataset($data); $query = "INSERT {$this->_table} SET {$dataset} ON DUPLICATE KEY UPDATE {$dataset}"; $id = sDB::query($query); do_action_ref_array('shopp_save_productsummary', array(&$this)); return $id; }
/** * Saves the current state of the ShoppDatabaseObject to the database * * Intelligently saves a ShoppDatabaseObject, using an UPDATE query when the * value for the primary key is set, and using an INSERT query when the * value of the primary key is not set. * * @author Jonathan Davis * @since 1.0 * @version 1.2 * * @return boolean|int Returns true when UPDATEs are successful; returns an integer with the record ID **/ public function save() { $classhook = strtolower(get_class($this)); $data = sDB::prepare($this, $this->_map); $id = isset($this->{$this->_key}) ? $this->{$this->_key} : false; if (!empty($this->_map)) { $remap = array_flip($this->_map); if (isset($remap[$this->_key])) { $id = $this->{$remap[$this->_key]}; } } $time = current_time('mysql'); if (isset($data['modified'])) { $data['modified'] = "'{$time}'"; } if (empty($id)) { // Insert new record if (isset($data['created'])) { $data['created'] = "'{$time}'"; } $dataset = ShoppDatabaseObject::dataset($data); $this->id = sDB::query("INSERT {$this->_table} SET {$dataset}"); do_action_ref_array("shopp_save_{$classhook}", array($this)); do_action_ref_array("shopp_create_{$classhook}", array($this)); return $this->id; } // Update record $dataset = ShoppDatabaseObject::dataset($data); sDB::query("UPDATE {$this->_table} SET {$dataset} WHERE {$this->_key}='{$id}'"); do_action_ref_array("shopp_save_{$classhook}", array($this)); return true; }
/** * Updates the setting in the registry and the database * * @since 1.0 * * @param string $name Name of the setting * @param mixed $value Value of the setting to update * @return boolean **/ public function update($name, $value) { if ($this->get($name) === $value) { return true; } $Setting = $this->setting(); $Setting->name = $name; $Setting->value = sDB::clean($value); $data = sDB::prepare($Setting); // Prepare the data for db entry $dataset = ShoppDatabaseObject::dataset($data); // Format the data in SQL $where = array("context='{$Setting->context}'", "type='{$Setting->type}'"); if (!empty($name)) { $where[] = "name='" . sDB::clean($name) . "'"; } $where = join(' AND ', $where); if (sDB::query("UPDATE {$this->_table} SET {$dataset} WHERE {$where}")) { $this->registry[$name] = $this->restore($value); } else { return false; } return true; }