public function CommerceCardOnFile::save in Commerce Card on File 7.2
Overrides Entity::save().
Overrides Entity::save
File
- includes/
commerce_cardonfile.entity.inc, line 132
Class
- CommerceCardOnFile
- Entity class representing the commerce_cardonfile entity type.
Code
public function save() {
$this->changed = REQUEST_TIME;
// Set the created timestamp during initial save.
if (!$this->card_id) {
$this->created = REQUEST_TIME;
}
if ($this->card_id) {
$this->original = $original = entity_load_unchanged('commerce_cardonfile', $this->card_id);
// Reactivate a declined card after its expiration date has been modified.
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;
}
}
}
// Perform the save.
$save_result = parent::save();
if ($save_result !== FALSE) {
// We cannot consider operation as update when original card has uid 0.
// If we would, one user would end up with multiple default cards.
$is_update = isset($original) && $original->uid != 0;
$value_changed = $is_update && $this->instance_default != $original->instance_default;
// If the card is now instance_default, remove the flag from other cards.
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;
}