public function execute() { try { $discountcard = waRequest::post('discountcard', array()); $model = new shopDiscountcardsPluginModel(); if (!empty($discountcard['id'])) { $model->updateById($discountcard['id'], $discountcard); $discountcard = $model->getById($discountcard['id']); } elseif (empty($discountcard['discountcard'])) { throw new waException('Ошибка: Не указан номер дисконтной карты'); } else { if ($model->getByField('discountcard', $discountcard['discountcard'])) { throw new waException('Ошибка: Номер дисконтной карты не уникален'); } $id = $model->insert($discountcard); $discountcard = $model->getById($id); } if (!empty($discountcard['contact_id'])) { $contact = new waContact($discountcard['contact_id']); $discountcard['contact_name'] = $contact->get('name'); } $discountcard['amount'] = shop_currency($discountcard['amount']); $this->response = $discountcard; } catch (Exception $ex) { $this->setError($ex->getMessage()); } }
public function execute() { try { $filepath = wa()->getCachePath('plugins/discountcards/import-discountcards.csv', 'shop'); if (!file_exists($filepath)) { throw new waException('Ошибка загрузки файла'); } $delimiter = waRequest::post('delimiter'); $enclosure = waRequest::post('enclosure'); $inserted = 0; $model = new shopDiscountcardsPluginModel(); if (waRequest::post('empty')) { $model->truncate(); } $f = fopen($filepath, "r"); while (($data = fgetcsv($f, null, $delimiter, $enclosure)) !== FALSE) { if (count($data) == 3) { $discountcard = array('discountcard' => $data[0], 'discount' => $data[1], 'amount' => $data[2]); } else { $discountcard = array('contact_id' => $data[0], 'discountcard' => $data[1], 'discount' => $data[2], 'amount' => $data[3]); } if (empty($discountcard['discountcard'])) { continue; } if ($model->getByField('discountcard', $discountcard['discountcard'])) { throw new waException('Ошибка: Номер дисконтной карты не уникален ' . $discountcard['discountcard']); } if ($model->insert($discountcard)) { $inserted++; } } fclose($f); $this->response['html'] = 'Добавлено записей: ' . $inserted; } catch (Exception $ex) { $this->setError($ex->getMessage()); } }