Source of file api.lousytariffs.php

Size: 8,557 Bytes - Last Modified: 2026-08-05T17:01:53+03:00

/tmp/current_snapshot/api/libs/api.lousytariffs.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
<?php

/**
 * The class designed to manage rarely used tariffs
 */
class LousyTariffs {

    /**
     * Contains array of all available lousy tariffs as tariffName=>lousyhData[id/tariff]
     * 
     * @var array
     */
    protected $allLousyTariffs = array();

    /**
     * Contains array of all available system tariffs as tariffName=>tariffData
     * 
     * @var array
     */
    protected $allTariffs = array();

    /**
     * Lousy tariffs database abstraction layer placeholder
     * 
     * @var object
     */
    protected $lousyDb = '';

    /**
     * System messages helper instance placeholder
     * 
     * @var object
     */
    protected $messages = '';

    //some other predefined stuff
    const TABLE_LOUSY = 'lousytariffs';
    const RIGHT_CONFIG = 'LOUSYTARIFFS';
    const URL_ME = '?module=lousytariffs';
    const ROUTE_DELETE = 'deletelousytariff';
    const PROUTE_CREATE = 'createlousytariff';

//
//                     _,._
//                 __.'   _)
//                <_,)'.-"a\
//                  /' (    \
//      _.-----..,-'   (`"--^
//     //              |
//    (|   `;      ,   |
//      \   ;.----/  ,/
//       ) // /   | |\ \
//       \ \\`\   | |/ /
//        \ \\ \  | |\/
//         `" `"  `"`
//    
    public function __construct() {
        $this->initMessages();
        $this->initLousyDb();
        $this->loadLousyTariffs();
    }

    /**
     * Inits message helper instance
     * 
     * @return void
     */
    protected function initMessages() {
        $this->messages = new UbillingMessageHelper();
    }

    /**
     * Inits database abstraction layer object for further usage
     * 
     * @return void
     */
    protected function initLousyDb() {
        $this->lousyDb = new NyanORM(self::TABLE_LOUSY);
    }

    /**
     * Loads available lousy tariffs data from database
     * 
     * @return void
     */
    protected function loadLousyTariffs() {
        $this->allLousyTariffs = $this->lousyDb->getAll('tariff');
    }

    /**
     * Loads existing tariffs. Required only for cfg iface.
     * 
     * @return void
     */
    protected function loadAllTariffs() {
        $this->allTariffs = zb_TariffGetAllData();
    }

    /**
     * Checks is tariff marked as lousy?
     * 
     * @param string $tariffName
     * 
     * @return bool
     */
    protected function isLousy($tariffName) {
        $result = false;
        if (isset($this->allLousyTariffs[$tariffName])) {
            $result = true;
        }
        return($result);
    }

    /**
     * Checks is tariff not marked as lousy?
     * 
     * @param string $tariffName
     * 
     * @return bool
     */
    protected function isNotLousy($tariffName) {
        $result = true;
        if (isset($this->allLousyTariffs[$tariffName])) {
            $result = false;
        }
        return($result);
    }

    /**
     * Creates new lousy tariff
     * 
     * @param string $tariffName
     * 
     * @return void/string on error
     */
    public function create($tariffName) {
        $result = '';
        //preloading existing tariffs
        $this->loadAllTariffs();
        $tariffNameF = ubRouting::filters($tariffName, 'mres');
        if (isset($this->allTariffs[$tariffName])) {
            if ($this->isNotLousy($tariffName)) {
                $this->lousyDb->data('tariff', $tariffNameF);
                $this->lousyDb->create();
                log_register('LOUSYTARIFF CREATE `' . $tariffName . '`');
            } else {
                $result .= __('Lousy tariff') . ' `' . $tariffName . '` ' . __('Already exists');
            }
        } else {
            $result .= __('Strange exception') . ': ' . __('Tariff') . ' `' . $tariffName . '` ' . __('Not exists');
        }
        return($result);
    }

    /**
     * Deletes existing lousy tariff
     * 
     * @param string $tariffName
     * 
     * @return void/string on error
     */
    public function delete($tariffName) {
        $result = '';
        $tariffNameF = ubRouting::filters($tariffName, 'mres');
        if ($this->isLousy($tariffName)) {
            $this->lousyDb->where('tariff', '=', $tariffNameF);
            $this->lousyDb->delete();
            log_register('LOUSYTARIFF DELETE `' . $tariffName . '`');
        } else {
            $result .= __('Lousy tariff') . ' `' . $tariffName . '` ' . __('Not exists');
        }
        return($result);
    }

    /**
     * Flushes existing lousy tariff on system tariff deletion
     * 
     * @param string $tariffName
     * 
     * @return void
     */
    public function flush($tariffName) {
        $result = '';
        $tariffNameF = ubRouting::filters($tariffName, 'mres');
        $this->lousyDb->where('tariff', '=', $tariffNameF);
        $this->lousyDb->delete();
        log_register('LOUSYTARIFF FLUSH `' . $tariffName . '`');
    }

    /**
     * Returns array copy without lousy tariffs
     * 
     * @param array $tariffsArr
     * 
     * @return array
     */
    public function truncateLousy($tariffsArr) {
        $result = array();

        if (!empty($tariffsArr)) {
            foreach ($tariffsArr as $eachTariff => $eachData) {
                if ($this->isNotLousy($eachTariff)) {
                    $result[$eachTariff] = $eachData;
                }
            }
        }
        return($result);
    }

    /**
     * Renders new  lousy tariff creation form
     * 
     * @return string
     */
    protected function renderCreateForm() {
        $result = '';
        if (!empty($this->allTariffs)) {
            $params = array();
            foreach ($this->allTariffs as $eachTariffName => $tariffData) {
                //excluding already lousy tariffs
                if (!$this->isLousy($eachTariffName)) {
                    $params[$eachTariffName] = $eachTariffName;
                }
            }

            if (!empty($params)) {
                $inputs = wf_Selector(self::PROUTE_CREATE, $params, __('Tariff'), '', false) . ' ';
                $inputs .= wf_Submit(__('Mark this tariff as not popular'));
                $result .= wf_Form('', 'POST', $inputs, 'glamour');
            } else {
                $result .= $this->messages->getStyledMessage(__('All tariffs marked as lousy'), 'info');
            }
        } else {
            $result .= $this->messages->getStyledMessage(__('Any tariffs available'), 'warning');
        }
        return($result);
    }

    /**
     * Renders existing lousy tariff deletion form
     * 
     * @param string $tariffName
     * 
     * @return string
     */
    protected function renderDeleteForm($tariffName) {
        $result = '';
        if ($this->isLousy($tariffName)) {
            $deleteUrl = self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $tariffName;
            $cancelUrl = self::URL_ME;
            $control = web_delete_icon();
            $customTitle = __('Delete') . ' ' . $tariffName . '?';
            $label = $this->messages->getDeleteAlert();
            $result .= wf_ConfirmDialog($deleteUrl, $control, $label, '', $cancelUrl, $customTitle);
        }
        return($result);
    }

    /**
     * Renders list of available lousy tariffs
     * 
     * @return string
     */
    public function renderList() {
        $result = '';
        //preloading system data required in future
        $this->loadAllTariffs();

        if (!empty($this->allLousyTariffs)) {
            $cells = wf_TableCell(__('Tariff'));
            $cells .= wf_TableCell(__('Fee'));
            $cells .= wf_TableCell(__('Actions'));
            $rows = wf_TableRow($cells, 'row1');
            foreach ($this->allLousyTariffs as $io => $each) {
                if (isset($this->allTariffs[$each['tariff']])) {
                    $tariffFee = $this->allTariffs[$each['tariff']]['Fee'];
                    $rowClass = 'row5';
                } else {
                    $tariffFee = __('Deleted');
                    $rowClass = 'sigdeleteduser';
                }

                $cells = wf_TableCell($each['tariff']);
                $cells .= wf_TableCell($tariffFee);
                $cells .= wf_TableCell($this->renderDeleteForm($each['tariff']));
                $rows .= wf_TableRow($cells, $rowClass);
            }
            $result .= wf_TableBody($rows, '100%', 0, 'sortable');
        } else {
            $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
        }
        $result .= wf_delimiter();
        $result .= $this->renderCreateForm();
        return($result);
    }
}