commerce_cardonfile.entity.inc in Commerce Card on File 7.2
File
includes/commerce_cardonfile.entity.inc
View source
<?php
class CommerceCardOnFile extends Entity {
public $card_id;
public $uid;
public $payment_method;
public $instance_id;
public $remote_id;
public $card_type;
public $card_name;
public $card_number;
public $card_exp_month;
public $card_exp_year;
public $instance_default = 1;
public $status = 1;
public $created;
public $changed;
public function __construct($values = array()) {
parent::__construct($values, 'commerce_cardonfile');
}
protected function defaultLabel() {
$card_type = t('Card');
if (!empty($this->card_type)) {
$card_types = commerce_cardonfile_credit_card_types();
if (isset($card_types[$this->card_type])) {
$card_type = $card_types[$this->card_type];
}
}
$args = array(
'@card_type' => $card_type,
'@card_number' => $this->card_number,
);
return t('@card_type ending in @card_number', $args);
}
public function save() {
$this->changed = REQUEST_TIME;
if (!$this->card_id) {
$this->created = REQUEST_TIME;
}
if ($this->card_id) {
$this->original = $original = entity_load_unchanged('commerce_cardonfile', $this->card_id);
if ($this->status == 3) {
$exp_month_changed = $this->original->card_exp_month != $this->card_exp_month;
$exp_year_changed = $this->original->card_exp_year != $this->card_exp_year;
if ($exp_month_changed || $exp_year_changed) {
$this->status = 1;
}
}
}
$save_result = parent::save();
if ($save_result !== FALSE) {
$is_update = isset($original) && $original->uid != 0;
$value_changed = $is_update && $this->instance_default != $original->instance_default;
if ($this->instance_default && (!$is_update || $value_changed)) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_cardonfile');
$query
->entityCondition('entity_id', $this->card_id, '<>');
$query
->propertyCondition('instance_id', $this->instance_id);
$query
->propertyCondition('uid', $this->uid);
$query
->propertyCondition('instance_default', TRUE);
$result = $query
->execute();
if (isset($result['commerce_cardonfile'])) {
$card_ids = array_keys($result['commerce_cardonfile']);
$other_cards = commerce_cardonfile_load_multiple($card_ids);
foreach ($other_cards as $other_card) {
$other_card->instance_default = 0;
commerce_cardonfile_save($other_card);
}
}
}
}
return $save_result;
}
}