/**
  * Loads the configuration from the Database
  * @return configurationModel
  */
 public function loadConfiguration()
 {
     $configModel = new configurationModel();
     $config = Configuration::getMultiple(array('PIGMBH_PAYMILL_PUBLICKEY', 'PIGMBH_PAYMILL_PRIVATEKEY', 'PIGMBH_PAYMILL_DEBUG', 'PIGMBH_PAYMILL_LOGGING', 'PIGMBH_PAYMILL_DEBIT', 'PIGMBH_PAYMILL_CREDITCARD', 'PIGMBH_PAYMILL_FASTCHECKOUT', 'PIGMBH_PAYMILL_DEBIT_DAYS', 'PIGMBH_PAYMILL_ACCEPTED_BRANDS'));
     $configModel->setPublicKey(isset($config['PIGMBH_PAYMILL_PUBLICKEY']) ? $config['PIGMBH_PAYMILL_PUBLICKEY'] : '');
     $configModel->setPrivateKey(isset($config['PIGMBH_PAYMILL_PRIVATEKEY']) ? $config['PIGMBH_PAYMILL_PRIVATEKEY'] : '');
     $configModel->setDebug(isset($config['PIGMBH_PAYMILL_DEBUG']) ? $config['PIGMBH_PAYMILL_DEBUG'] : false);
     $configModel->setLogging(isset($config['PIGMBH_PAYMILL_LOGGING']) ? $config['PIGMBH_PAYMILL_LOGGING'] : false);
     $configModel->setDirectdebit(isset($config['PIGMBH_PAYMILL_DEBIT']) ? $config['PIGMBH_PAYMILL_DEBIT'] : false);
     $configModel->setCreditcard(isset($config['PIGMBH_PAYMILL_CREDITCARD']) ? $config['PIGMBH_PAYMILL_CREDITCARD'] : false);
     $configModel->setFastcheckout(isset($config['PIGMBH_PAYMILL_FASTCHECKOUT']) ? $config['PIGMBH_PAYMILL_FASTCHECKOUT'] : false);
     $configModel->setDebitDays(isset($config['PIGMBH_PAYMILL_DEBIT_DAYS']) ? $config['PIGMBH_PAYMILL_DEBIT_DAYS'] : false);
     $configModel->setAccpetedCreditCards(isset($config['PIGMBH_PAYMILL_ACCEPTED_BRANDS']) ? json_decode($config['PIGMBH_PAYMILL_ACCEPTED_BRANDS'], true) : false);
     return $configModel;
 }
 /**
  * Returns the Pluginconfiguration as HTML-string
  *
  *
  * @return string HTML
  */
 public function getContent()
 {
     //configuration
     if (Tools::isSubmit('btnSubmit')) {
         $oldConfig = $this->_configurationHandler->loadConfiguration();
         $newConfig = new configurationModel();
         $toleranz = Tools::getValue('differentamount');
         if (is_numeric($toleranz)) {
             $toleranz = number_format($toleranz, 2, '.', '');
         } else {
             $toleranz = number_format(0, 2, '.', '');
         }
         $acceptedBrands = array();
         foreach (Tools::getValue('accepted_brands') as $acceptedBrand) {
             $acceptedBrands[$acceptedBrand] = true;
         }
         $acceptedBrandsResult = array();
         foreach ($oldConfig->getAccpetedCreditCards() as $key => $value) {
             if (array_key_exists($key, $acceptedBrands)) {
                 $acceptedBrandsResult[$key] = true;
             } else {
                 $acceptedBrandsResult[$key] = false;
             }
         }
         $newConfig->setCreditcard(Tools::getValue('creditcard', 'OFF'));
         $newConfig->setDirectdebit(Tools::getValue('debit', 'OFF'));
         $newConfig->setDebug(Tools::getValue('debug', 'OFF'));
         $newConfig->setFastcheckout(Tools::getValue('fastcheckout', 'OFF'));
         $newConfig->setLogging(Tools::getValue('logging', 'OFF'));
         $newConfig->setPrivateKey(trim(Tools::getValue('privatekey', $oldConfig->getPrivateKey())));
         $newConfig->setPublicKey(trim(Tools::getValue('publickey', $oldConfig->getPublicKey())));
         $newConfig->setDebitDays(Tools::getValue('sepa', '7'));
         $newConfig->setAccpetedCreditCards($acceptedBrandsResult);
         $this->_configurationHandler->updateConfiguration($newConfig);
         $this->registerPaymillWebhook($newConfig->getPrivateKey());
     }
     //logging
     $db = Db::getInstance();
     $data = array();
     $detailData = array();
     $showDetail = false;
     $search = Tools::getValue('searchvalue', false);
     $connectedSearch = Tools::getValue('connectedsearch', "off");
     $limit = 10;
     $where = $search && !empty($search) ? " WHERE `debug` LIKE '%" . $search . "%' OR `message` LIKE '%" . $search . "%'" : null;
     $db->execute("SELECT * FROM `pigmbh_paymill_logging`" . $where, true);
     $maxPage = ceil($db->numRows() / $limit) == 0 ? 1 : range(1, ceil($db->numRows() / $limit));
     $page = $maxPage < Tools::getValue('paymillpage', 1) ? $maxPage : Tools::getValue('paymillpage', 1);
     $start = $page * $limit - $limit;
     //Details
     if (Tools::getValue('paymillid') && Tools::getValue('paymillkey')) {
         $showDetail = true;
         $row = $db->executeS("SELECT * FROM `pigmbh_paymill_logging` WHERE id='" . Tools::getValue('paymillid') . "';", true);
         $detailData['title'] = 'DEBUG';
         $detailData['data'] = $row[0]['debug'];
     }
     //getAll Data
     if ($connectedSearch === "on") {
         $where = "WHERE `identifier` in(SELECT `identifier` FROM `pigmbh_paymill_logging` {$where})";
     }
     $sql = "SELECT `id`,`identifier`,`date`,`message`,`debug` FROM `pigmbh_paymill_logging` {$where} LIMIT {$start}, {$limit}";
     foreach ($db->executeS($sql, true) as $row) {
         foreach ($row as $key => $value) {
             $value = is_array($value) ? $value[1] . "<br><br>" . $value[0] : $value;
             $unsortedPrintData[$key] = strlen($value) >= 300 ? "<a href='" . $_SERVER['REQUEST_URI'] . "&paymillid=" . $row['id'] . "&paymillkey=" . $key . "&searchvalue=" . $search . "'>" . $this->l('see more') . "</a>" : $value;
         }
         $data[] = $unsortedPrintData;
     }
     $this->smarty->assign(array('config' => $this->showConfigurationForm(), 'data' => $data, 'detailData' => $detailData, 'showDetail' => $showDetail, 'paymillMaxPage' => $maxPage, 'paymillCurrentPage' => $page, 'paymillSearchValue' => $search, 'paymillConnectedSearch' => $connectedSearch));
     return $this->display(__FILE__, 'views/templates/admin/logging.tpl');
 }