| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * HiPanel core package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link      https://hipanel.com/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package   hipanel-core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace hipanel\widgets; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use yii\helpers\Html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class AmountWithCurrency extends \yii\base\Widget | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public static $widgetClass = 'amount-with-currency-widget'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var string Set this value to use it instead of model currency value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public $selectedCurrencyCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public $currencyAttributeName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public $currencyAttributeOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $attribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public $inputOptions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var array The following keys are used: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * - `readonly`: protects currency input from changes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public $currencyDropdownOptions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->inputOptions = array_merge(['class' => 'form-control'], $this->inputOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function run() | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->initClientScript(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         return $this->render((new \ReflectionClass($this))->getShortName(), [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             'containerClass' => self::$widgetClass, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             'form' => $this->form, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             'model' => $this->model, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             'attribute' => $this->attribute, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             'selectedCurrencyCode' => $this->getSelectedCurrencyCode(), | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             'currencyAttributeOptions' => $this->currencyAttributeOptions, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'currencyDropdownOptions' => $this->currencyDropdownOptions, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function initClientScript() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $widgetClass = self::$widgetClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->getView()->registerJs(<<<"JS" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $(document).on('click', '.{$widgetClass} a', function(e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             var item = $(this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             item.parents('.amount-with-currency-widget').find('.iwd-label').text(item.data('label')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             item.parents('.amount-with-currency-widget').find(':hidden').val(item.data('value')).trigger('change'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     private function getSelectedCurrencyCode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->selectedCurrencyCode ?: Html::getAttributeValue($this->model, $this->currencyAttributeName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |