Barcode::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\barcode\generator;
4
5
use yii\base\Widget;
6
use yii\helpers\ArrayHelper;
7
use yii\helpers\Html;
8
use yii\helpers\Json;
9
10
/**
11
 * Barcode widget.
12
 * 
13
 * @link [sugeng-sulistiyawan.github.io](sugeng-sulistiyawan.github.io)
14
 * @author Sugeng Sulistiyawan <[email protected]>
15
 * @copyright Copyright (c) 2023
16
 */
17
class Barcode extends Widget
18
{
19
	/** 
20
	 * CODE128 is one of the more versatile barcodes. 
21
	 * It has support for all 128 ASCII characters but does also encode numbers efficiently. 
22
	 * It has three modes (A/B/C) but can switch between them at any time. 
23
	 * CODE128 is the default barcode that JsBarcode will choose if nothing else is specified.
24
	 */
25
	const CODE128  = "CODE128";
26
	const CODE128A = "CODE128A";
27
	const CODE128B = "CODE128B";
28
	const CODE128C = "CODE128C";
29
30
	/** 
31
	 * EAN comes in a variety of forms, most commonly used is EAN-13 (GTIN-13) 
32
	 * that is used on world wide to marking the identity of products.
33
	 * 
34
	 * Supports the formats EAN-13, EAN-8 and UPC as well as 
35
	 * the barcode addons EAN-5 and EAN-2.
36
	 */
37
	const EAN13 = "EAN13";
38
	const UPC   = "UPC";
39
	const EAN8  = "EAN8";
40
	const EAN5  = "EAN5";
41
	const EAN2  = "EAN2";
42
43
	/** 
44
	 * CODE39 is an old barcode type that can encode numbers, uppercase letters 
45
	 * and a number of special characters (-, ., $, /, +, %, and space). 
46
	 * It has been a common barcode type in the past but CODE128 
47
	 * is recommended if not for legacy reasons.
48
	 */
49
	const CODE39 = "CODE39";
50
51
	/** 
52
	 * ITF-14 (Interleaved Two of Five) is the GS1 implementation of 
53
	 * an Interleaved 2 of 5 bar code to encode a Global Trade Item Number. 
54
	 * ITF-14 symbols are generally used on packaging levels of a product, 
55
	 * such as a case box of 24 cans of soup. 
56
	 * The ITF-14 will always encode 14 digits.
57
	 * 
58
	 * The last digit of an ITF-14 barcode is an checksum. 
59
	 * It is normally included but JsBarcode can automatically 
60
	 * calculate it for you if it is left out.
61
	 */
62
	const ITF14 = "ITF14";
63
64
	/** 
65
	 * MSI or Modified Plessey is a barcode developed by the MSI Data Corporation 
66
	 * and is primarily used for inventory control, marking storage containers 
67
	 * and shelves in warehouse environments. It supports digits 0-9. 
68
	 * JsBarcode provides automatic checksum 
69
	 * calculation of Mod 10, Mod 11, Mod 1010 and Mod 1110.
70
	 */
71
	const MSI     = "MSI";
72
	const MSI10   = "MSI10";
73
	const MSI11   = "MSI11";
74
	const MSI1010 = "MSI1010";
75
	const MSI1110 = "MSI1110";
76
77
	/** 
78
	 * Pharmacode is a barcode used in the pharmaceutical industry. 
79
	 * It can encode numbers 3 to 131070.
80
	 */
81
	const PHARMACODE = "pharmacode";
82
83
	/** 
84
	 * Codabar is an old barcode type that can encode numbers 
85
	 * and a number of special characters (-, $, :, /, +, .).
86
	 * 
87
	 * You can set start and stop characters to A, B, C or D 
88
	 * but if no start and stop character is defined A will be used.
89
	 */
90
	const CODABAR = "codabar";
91
92
	/**
93
	 * @var string
94
	 */
95
	public $value;
96
97
	/**
98
	 * @var string
99
	 */
100
	public $format;
101
102
	/**
103
	 * @var string default `svg` available svg, img, canvas
104
	 */
105
	public $tag = 'svg';
106
107
	/**
108
	 * @var string default `self::CODE128`
109
	 */
110
	public $defaultFormat = self::CODE128;
111
112
	/**
113
	 * @var array default `[]`, Custom Barcode Canvas options and override default options
114
	 */
115
	public $options = [];
116
117
	/**
118
	 * @var array default `[]`, for option `JsBarcode(#options['id'], value, pluginOptions);`
119
	 * @see https://github.com/lindell/JsBarcode/wiki/Options
120
	 * 
121
	 * ```php
122
	 * 'pluginOptions' => [
123
	 *     'format'       => "auto"
124
	 *     'width'        => 2
125
	 *     'height'       => 100
126
	 *     'displayValue' => true
127
	 *     'text'         => undefined
128
	 *     'fontOptions'  => ""
129
	 *     'font'         => "monospace"
130
	 *     'textAlign'    => "center"
131
	 *     'textPosition' => "bottom"
132
	 *     'textMargin'   => 2
133
	 *     'fontSize'     => 20
134
	 *     'background'   => "#ffffff"
135
	 *     'lineColor'    => "#000000"
136
	 *     'margin'       => 10
137
	 *     'marginTop'    => undefined
138
	 *     'marginBottom' => undefined
139
	 *     'marginLeft'   => undefined
140
	 *     'marginRight'  => undefined
141
	 *     'flat'         => false
142
	 *     'valid'        => function(valid){}
143
	 * ],
144
	 * ```
145
	 */
146
	public $pluginOptions = [];
147
148
	/**
149
	 * @inheritdoc
150
	 */
151
	public function init()
152
	{
153
		$this->view->registerAssetBundle(BarcodeAsset::class);
154
155
		$this->pluginOptions['format'] = $this->format ?? ArrayHelper::getValue($this->pluginOptions, 'format', $this->defaultFormat);
156
		$this->pluginOptions['format'] = strtolower($this->pluginOptions['format']) === 'auto' ? $this->defaultFormat : $this->pluginOptions['format'];
157
158
		if (!isset($this->options['id'])) {
159
			$this->options['id'] = $this->getId();
160
		}
161
162
		parent::init();
163
	}
164
165
	/**
166
	 * @inheritdoc
167
	 */
168
	public function run()
169
	{
170
		$pluginOptions = Json::encode($this->pluginOptions);
171
172
		$this->view->registerJs("JsBarcode(\"#{$this->options['id']}\", \"{$this->value}\", {$pluginOptions});");
173
174
		return Html::tag($this->tag, '', $this->options);
175
	}
176
}
177