Lib_Varien - Version 1.6.0.0

Version Notes

1.6.0.0

Download this release

Release Info

Developer Magento Core Team
Extension Lib_Varien
Version 1.6.0.0
Comparing to
See all releases


Version 1.6.0.0

Files changed (156) hide show
  1. lib/Varien/Autoload.php +170 -0
  2. lib/Varien/Cache/Backend/Database.php +544 -0
  3. lib/Varien/Cache/Backend/Eaccelerator.php +340 -0
  4. lib/Varien/Cache/Core.php +130 -0
  5. lib/Varien/Convert.php +51 -0
  6. lib/Varien/Convert/Action.php +38 -0
  7. lib/Varien/Convert/Action/Abstract.php +199 -0
  8. lib/Varien/Convert/Action/Interface.php +43 -0
  9. lib/Varien/Convert/Adapter/Abstract.php +65 -0
  10. lib/Varien/Convert/Adapter/Db/Table.php +54 -0
  11. lib/Varien/Convert/Adapter/Http.php +98 -0
  12. lib/Varien/Convert/Adapter/Http/Curl.php +71 -0
  13. lib/Varien/Convert/Adapter/Interface.php +40 -0
  14. lib/Varien/Convert/Adapter/Io.php +81 -0
  15. lib/Varien/Convert/Adapter/Soap.php +46 -0
  16. lib/Varien/Convert/Adapter/Std.php +53 -0
  17. lib/Varien/Convert/Adapter/Zend/Cache.php +56 -0
  18. lib/Varien/Convert/Adapter/Zend/Db.php +54 -0
  19. lib/Varien/Convert/Container/Abstract.php +166 -0
  20. lib/Varien/Convert/Container/Collection.php +73 -0
  21. lib/Varien/Convert/Container/Generic.php +38 -0
  22. lib/Varien/Convert/Container/Interface.php +38 -0
  23. lib/Varien/Convert/Exception.php +80 -0
  24. lib/Varien/Convert/Mapper/Abstract.php +38 -0
  25. lib/Varien/Convert/Mapper/Column.php +60 -0
  26. lib/Varien/Convert/Mapper/Interface.php +38 -0
  27. lib/Varien/Convert/Parser/Abstract.php +38 -0
  28. lib/Varien/Convert/Parser/Csv.php +170 -0
  29. lib/Varien/Convert/Parser/Interface.php +40 -0
  30. lib/Varien/Convert/Parser/Serialize.php +48 -0
  31. lib/Varien/Convert/Parser/Xml/Excel.php +228 -0
  32. lib/Varien/Convert/Profile.php +38 -0
  33. lib/Varien/Convert/Profile/Abstract.php +120 -0
  34. lib/Varien/Convert/Profile/Collection.php +160 -0
  35. lib/Varien/Convert/Validator/Abstract.php +38 -0
  36. lib/Varien/Convert/Validator/Column.php +38 -0
  37. lib/Varien/Convert/Validator/Dryrun.php +46 -0
  38. lib/Varien/Convert/Validator/Interface.php +38 -0
  39. lib/Varien/Crypt.php +49 -0
  40. lib/Varien/Crypt/Abstract.php +38 -0
  41. lib/Varien/Crypt/Mcrypt.php +138 -0
  42. lib/Varien/Data/Collection.php +805 -0
  43. lib/Varien/Data/Collection/Db.php +793 -0
  44. lib/Varien/Data/Collection/Filesystem.php +701 -0
  45. lib/Varien/Data/Form.php +258 -0
  46. lib/Varien/Data/Form/Abstract.php +211 -0
  47. lib/Varien/Data/Form/Element/Abstract.php +296 -0
  48. lib/Varien/Data/Form/Element/Button.php +42 -0
  49. lib/Varien/Data/Form/Element/Checkbox.php +79 -0
  50. lib/Varien/Data/Form/Element/Checkboxes.php +202 -0
  51. lib/Varien/Data/Form/Element/Collection.php +209 -0
  52. lib/Varien/Data/Form/Element/Column.php +41 -0
  53. lib/Varien/Data/Form/Element/Date.php +190 -0
  54. lib/Varien/Data/Form/Element/Editor.php +399 -0
  55. lib/Varien/Data/Form/Element/Fieldset.php +186 -0
  56. lib/Varien/Data/Form/Element/File.php +42 -0
  57. lib/Varien/Data/Form/Element/Gallery.php +186 -0
  58. lib/Varien/Data/Form/Element/Hidden.php +51 -0
  59. lib/Varien/Data/Form/Element/Image.php +124 -0
  60. lib/Varien/Data/Form/Element/Imagefile.php +45 -0
  61. lib/Varien/Data/Form/Element/Label.php +51 -0
  62. lib/Varien/Data/Form/Element/Link.php +68 -0
  63. lib/Varien/Data/Form/Element/Multiline.php +105 -0
  64. lib/Varien/Data/Form/Element/Multiselect.php +148 -0
  65. lib/Varien/Data/Form/Element/Note.php +49 -0
  66. lib/Varien/Data/Form/Element/Obscure.php +65 -0
  67. lib/Varien/Data/Form/Element/Password.php +48 -0
  68. lib/Varien/Data/Form/Element/Radio.php +42 -0
  69. lib/Varien/Data/Form/Element/Radios.php +86 -0
  70. lib/Varien/Data/Form/Element/Renderer/Interface.php +37 -0
  71. lib/Varien/Data/Form/Element/Reset.php +42 -0
  72. lib/Varien/Data/Form/Element/Select.php +123 -0
  73. lib/Varien/Data/Form/Element/Submit.php +48 -0
  74. lib/Varien/Data/Form/Element/Text.php +53 -0
  75. lib/Varien/Data/Form/Element/Textarea.php +59 -0
  76. lib/Varien/Data/Form/Element/Time.php +92 -0
  77. lib/Varien/Data/Form/Filter/Date.php +109 -0
  78. lib/Varien/Data/Form/Filter/Escapehtml.php +58 -0
  79. lib/Varien/Data/Form/Filter/Interface.php +52 -0
  80. lib/Varien/Data/Form/Filter/Striptags.php +58 -0
  81. lib/Varien/Data/Tree.php +225 -0
  82. lib/Varien/Data/Tree/Db.php +305 -0
  83. lib/Varien/Data/Tree/Dbp.php +394 -0
  84. lib/Varien/Data/Tree/Node.php +275 -0
  85. lib/Varien/Data/Tree/Node/Collection.php +132 -0
  86. lib/Varien/Date.php +182 -0
  87. lib/Varien/Db/Adapter/Interface.php +965 -0
  88. lib/Varien/Db/Adapter/Mysqli.php +283 -0
  89. lib/Varien/Db/Adapter/Pdo/Mysql.php +3488 -0
  90. lib/Varien/Db/Ddl/Table.php +694 -0
  91. lib/Varien/Db/Exception.php +29 -0
  92. lib/Varien/Db/Helper.php +110 -0
  93. lib/Varien/Db/Select.php +474 -0
  94. lib/Varien/Db/Statement/Parameter.php +248 -0
  95. lib/Varien/Db/Statement/Pdo/Mysql.php +113 -0
  96. lib/Varien/Db/Tree.php +561 -0
  97. lib/Varien/Db/Tree/Exception.php +31 -0
  98. lib/Varien/Db/Tree/Node.php +108 -0
  99. lib/Varien/Db/Tree/Node/Exception.php +29 -0
  100. lib/Varien/Db/Tree/NodeSet.php +83 -0
  101. lib/Varien/Db/Tree/NodeSet/Exception.php +29 -0
  102. lib/Varien/Debug.php +204 -0
  103. lib/Varien/Directory/Collection.php +461 -0
  104. lib/Varien/Directory/Factory.php +58 -0
  105. lib/Varien/Directory/IFactory.php +44 -0
  106. lib/Varien/Directory/a.txt +1352 -0
  107. lib/Varien/Event.php +122 -0
  108. lib/Varien/Event/Collection.php +147 -0
  109. lib/Varien/Event/Observer.php +115 -0
  110. lib/Varien/Event/Observer/Collection.php +111 -0
  111. lib/Varien/Event/Observer/Cron.php +158 -0
  112. lib/Varien/Event/Observer/Regex.php +47 -0
  113. lib/Varien/Exception.php +52 -0
  114. lib/Varien/File/Csv.php +171 -0
  115. lib/Varien/File/CsvMulty.php +75 -0
  116. lib/Varien/File/Object.php +294 -0
  117. lib/Varien/File/Transfer/Adapter/Http.php +153 -0
  118. lib/Varien/File/Uploader.php +542 -0
  119. lib/Varien/File/Uploader/Image.php +366 -0
  120. lib/Varien/Filter/Array.php +56 -0
  121. lib/Varien/Filter/Array/Grid.php +38 -0
  122. lib/Varien/Filter/Email.php +34 -0
  123. lib/Varien/Filter/Money.php +41 -0
  124. lib/Varien/Filter/Object.php +60 -0
  125. lib/Varien/Filter/Object/Grid.php +40 -0
  126. lib/Varien/Filter/Sprintf.php +51 -0
  127. lib/Varien/Filter/Template.php +272 -0
  128. lib/Varien/Filter/Template/Simple.php +44 -0
  129. lib/Varien/Filter/Template/Tokenizer/Abstract.php +121 -0
  130. lib/Varien/Filter/Template/Tokenizer/Parameter.php +98 -0
  131. lib/Varien/Filter/Template/Tokenizer/Variable.php +185 -0
  132. lib/Varien/Http/Adapter/Curl.php +238 -0
  133. lib/Varien/Http/Client.php +93 -0
  134. lib/Varien/Image.php +318 -0
  135. lib/Varien/Image/Adapter.php +59 -0
  136. lib/Varien/Image/Adapter/Abstract.php +259 -0
  137. lib/Varien/Image/Adapter/Gd2.php +538 -0
  138. lib/Varien/Io/Abstract.php +118 -0
  139. lib/Varien/Io/Exception.php +37 -0
  140. lib/Varien/Io/File.php +813 -0
  141. lib/Varien/Io/Ftp.php +327 -0
  142. lib/Varien/Io/Interface.php +114 -0
  143. lib/Varien/Io/Sftp.php +236 -0
  144. lib/Varien/Object.php +918 -0
  145. lib/Varien/Object/Cache.php +457 -0
  146. lib/Varien/Object/Mapper.php +120 -0
  147. lib/Varien/Pear.php +297 -0
  148. lib/Varien/Pear/Frontend.php +127 -0
  149. lib/Varien/Pear/Package.php +221 -0
  150. lib/Varien/Pear/Registry.php +73 -0
  151. lib/Varien/Profiler.php +185 -0
  152. lib/Varien/Simplexml/Config.php +604 -0
  153. lib/Varien/Simplexml/Config/Cache/Abstract.php +98 -0
  154. lib/Varien/Simplexml/Config/Cache/File.php +110 -0
  155. lib/Varien/Simplexml/Element.php +631 -0
  156. package.xml +18 -0
lib/Varien/Autoload.php ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Autoload
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Classes source autoload
29
+ */
30
+ class Varien_Autoload
31
+ {
32
+ const SCOPE_FILE_PREFIX = '__';
33
+
34
+ static protected $_instance;
35
+ static protected $_scope = 'default';
36
+
37
+ protected $_isIncludePathDefined= null;
38
+ protected $_collectClasses = false;
39
+ protected $_collectPath = null;
40
+ protected $_arrLoadedClasses = array();
41
+
42
+ /**
43
+ * Class constructor
44
+ */
45
+ public function __construct()
46
+ {
47
+ $this->_isIncludePathDefined = defined('COMPILER_INCLUDE_PATH');
48
+ if (defined('COMPILER_COLLECT_PATH')) {
49
+ $this->_collectClasses = true;
50
+ $this->_collectPath = COMPILER_COLLECT_PATH;
51
+ }
52
+ self::registerScope(self::$_scope);
53
+ }
54
+
55
+ /**
56
+ * Singleton pattern implementation
57
+ *
58
+ * @return Varien_Autoload
59
+ */
60
+ static public function instance()
61
+ {
62
+ if (!self::$_instance) {
63
+ self::$_instance = new Varien_Autoload();
64
+ }
65
+ return self::$_instance;
66
+ }
67
+
68
+ /**
69
+ * Register SPL autoload function
70
+ */
71
+ static public function register()
72
+ {
73
+ spl_autoload_register(array(self::instance(), 'autoload'));
74
+ }
75
+
76
+ /**
77
+ * Load class source code
78
+ *
79
+ * @param string $class
80
+ */
81
+ public function autoload($class)
82
+ {
83
+ if ($this->_collectClasses) {
84
+ $this->_arrLoadedClasses[self::$_scope][] = $class;
85
+ }
86
+ if ($this->_isIncludePathDefined) {
87
+ $classFile = $class;
88
+ } else {
89
+ $classFile = str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class)));
90
+ }
91
+ $classFile.= '.php';
92
+ //echo $classFile;die();
93
+ return include $classFile;
94
+ }
95
+
96
+ /**
97
+ * Register autoload scope
98
+ * This process allow include scope file which can contain classes
99
+ * definition which are used for this scope
100
+ *
101
+ * @param string $code scope code
102
+ */
103
+ static public function registerScope($code)
104
+ {
105
+ self::$_scope = $code;
106
+ if (defined('COMPILER_INCLUDE_PATH')) {
107
+ @include_once self::SCOPE_FILE_PREFIX.$code.'.php';
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Get current autoload scope
113
+ *
114
+ * @return string
115
+ */
116
+ static public function getScope()
117
+ {
118
+ return self::$_scope;
119
+ }
120
+
121
+ /**
122
+ * Class destructor
123
+ */
124
+ public function __destruct()
125
+ {
126
+ if ($this->_collectClasses) {
127
+ $this->_saveCollectedStat();
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Save information about used classes per scope with class popularity
133
+ * Class_Name:popularity
134
+ *
135
+ * @return Varien_Autoload
136
+ */
137
+ protected function _saveCollectedStat()
138
+ {
139
+ if (!is_dir($this->_collectPath)) {
140
+ @mkdir($this->_collectPath);
141
+ @chmod($this->_collectPath, 0777);
142
+ }
143
+
144
+ if (!is_writeable($this->_collectPath)) {
145
+ return $this;
146
+ }
147
+
148
+ foreach ($this->_arrLoadedClasses as $scope => $classes) {
149
+ $file = $this->_collectPath.DIRECTORY_SEPARATOR.$scope.'.csv';
150
+ $data = array();
151
+ if (file_exists($file)) {
152
+ $data = explode("\n", file_get_contents($file));
153
+ foreach ($data as $index => $class) {
154
+ $class = explode(':', $class);
155
+ $searchIndex = array_search($class[0], $classes);
156
+ if ($searchIndex !== false) {
157
+ $class[1]+=1;
158
+ unset($classes[$searchIndex]);
159
+ }
160
+ $data[$index] = $class[0].':'.$class[1];
161
+ }
162
+ }
163
+ foreach ($classes as $class) {
164
+ $data[] = $class . ':1';
165
+ }
166
+ file_put_contents($file, implode("\n", $data));
167
+ }
168
+ return $this;
169
+ }
170
+ }
lib/Varien/Cache/Backend/Database.php ADDED
@@ -0,0 +1,544 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Cache
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ Tables declaration:
29
+
30
+ CREATE TABLE IF NOT EXISTS `core_cache` (
31
+ `id` VARCHAR(255) NOT NULL,
32
+ `data` mediumblob,
33
+ `create_time` int(11),
34
+ `update_time` int(11),
35
+ `expire_time` int(11),
36
+ PRIMARY KEY (`id`),
37
+ KEY `IDX_EXPIRE_TIME` (`expire_time`)
38
+ )ENGINE=InnoDB DEFAULT CHARSET=utf8;
39
+
40
+ CREATE TABLE IF NOT EXISTS `core_cache_tag` (
41
+ `tag` VARCHAR(255) NOT NULL,
42
+ `cache_id` VARCHAR(255) NOT NULL,
43
+ KEY `IDX_TAG` (`tag`),
44
+ KEY `IDX_CACHE_ID` (`cache_id`),
45
+ CONSTRAINT `FK_CORE_CACHE_TAG` FOREIGN KEY (`cache_id`) REFERENCES `core_cache` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
46
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
47
+ */
48
+
49
+ /**
50
+ * Database cache backend
51
+ */
52
+ class Varien_Cache_Backend_Database extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
53
+ {
54
+ /**
55
+ * Available options
56
+ *
57
+ * @var array available options
58
+ */
59
+ protected $_options = array(
60
+ 'adapter' => '',
61
+ 'adapter_callback' => '',
62
+ 'data_table' => '',
63
+ 'tags_table' => '',
64
+ 'store_data' => true,
65
+ );
66
+
67
+ protected $_adapter = null;
68
+
69
+ /**
70
+ * Constructor
71
+ *
72
+ * @param array $options associative array of options
73
+ */
74
+ public function __construct($options = array())
75
+ {
76
+ parent::__construct($options);
77
+ if (empty($this->_options['adapter_callback'])) {
78
+ if (!($this->_options['adapter'] instanceof Zend_Db_Adapter_Abstract)) {
79
+ Zend_Cache::throwException('Option "adapter" should be declared and extend Zend_Db_Adapter_Abstract!');
80
+ }
81
+ }
82
+ if (empty($this->_options['data_table']) || empty ($this->_options['tags_table'])) {
83
+ Zend_Cache::throwException('Options "data_table" and "tags_table" should be declared!');
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Get DB adapter
89
+ *
90
+ * @return Zend_Db_Adapter_Abstract
91
+ */
92
+ protected function _getAdapter()
93
+ {
94
+ if (!$this->_adapter) {
95
+ if (!empty($this->_options['adapter_callback'])) {
96
+ $adapter = call_user_func($this->_options['adapter_callback']);
97
+ } else {
98
+ $adapter = $this->_options['adapter'];
99
+ }
100
+ if (!($adapter instanceof Zend_Db_Adapter_Abstract)) {
101
+ Zend_Cache::throwException('DB Adapter should be declared and extend Zend_Db_Adapter_Abstract');
102
+ } else {
103
+ $this->_adapter = $adapter;
104
+ }
105
+ }
106
+ return $this->_adapter;
107
+ }
108
+
109
+ /**
110
+ * Get table name where data is stored
111
+ *
112
+ * @return string
113
+ */
114
+ protected function _getDataTable()
115
+ {
116
+ return $this->_options['data_table'];
117
+ }
118
+
119
+ /**
120
+ * Get table name where tags are stored
121
+ *
122
+ * @return string
123
+ */
124
+ protected function _getTagsTable()
125
+ {
126
+ return $this->_options['tags_table'];
127
+ }
128
+
129
+ /**
130
+ * Test if a cache is available for the given id and (if yes) return it (false else)
131
+ *
132
+ * Note : return value is always "string" (unserialization is done by the core not by the backend)
133
+ *
134
+ * @param string $id Cache id
135
+ * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
136
+ * @return string|false cached datas
137
+ */
138
+ public function load($id, $doNotTestCacheValidity = false)
139
+ {
140
+ if ($this->_options['store_data']) {
141
+ $select = $this->_getAdapter()->select()
142
+ ->from($this->_getDataTable(), 'data')
143
+ ->where('id=:cache_id');
144
+
145
+ if (!$doNotTestCacheValidity) {
146
+ $select->where('expire_time=0 OR expire_time>?', time());
147
+ }
148
+ return $this->_getAdapter()->fetchOne($select, array('cache_id'=>$id));
149
+ } else {
150
+ return false;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Test if a cache is available or not (for the given id)
156
+ *
157
+ * @param string $id cache id
158
+ * @return mixed|false (a cache is not available) or "last modified" timestamp (int) of the available cache record
159
+ */
160
+ public function test($id)
161
+ {
162
+ if ($this->_options['store_data']) {
163
+ $select = $this->_getAdapter()->select()
164
+ ->from($this->_getDataTable(), 'update_time')
165
+ ->where('id=:cache_id')
166
+ ->where('expire_time=0 OR expire_time>?', time());
167
+ return $this->_getAdapter()->fetchOne($select, array('cache_id'=>$id));
168
+ } else {
169
+ return false;
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Save some string datas into a cache record
175
+ *
176
+ * Note : $data is always "string" (serialization is done by the
177
+ * core not by the backend)
178
+ *
179
+ * @param string $data Datas to cache
180
+ * @param string $id Cache id
181
+ * @param array $tags Array of strings, the cache record will be tagged by each string entry
182
+ * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
183
+ * @return boolean true if no problem
184
+ */
185
+ public function save($data, $id, $tags = array(), $specificLifetime = false)
186
+ {
187
+ if ($this->_options['store_data']) {
188
+ $adapter = $this->_getAdapter();
189
+ $dataTable = $this->_getDataTable();
190
+
191
+ $lifetime = $this->getLifetime($specificLifetime);
192
+ $time = time();
193
+ $expire = ($lifetime === 0 || $lifetime === null) ? 0 : $time+$lifetime;
194
+
195
+ $dataCol = $adapter->quoteIdentifier('data');
196
+ $expireCol = $adapter->quoteIdentifier('expire_time');
197
+ $query = "INSERT INTO {$dataTable} (
198
+ {$adapter->quoteIdentifier('id')},
199
+ {$dataCol},
200
+ {$adapter->quoteIdentifier('create_time')},
201
+ {$adapter->quoteIdentifier('update_time')},
202
+ {$expireCol})
203
+ VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE
204
+ {$dataCol}=VALUES({$dataCol}),
205
+ {$expireCol}=VALUES({$expireCol})";
206
+
207
+ $result = $adapter->query($query, array($id, $data, $time, $time, $expire))->rowCount();
208
+ if (!$result) {
209
+ return false;
210
+ }
211
+ }
212
+ $tagRes = $this->_saveTags($id, $tags);
213
+ return $tagRes;
214
+ }
215
+
216
+ /**
217
+ * Remove a cache record
218
+ *
219
+ * @param string $id Cache id
220
+ * @return boolean True if no problem
221
+ */
222
+ public function remove($id)
223
+ {
224
+ if ($this->_options['store_data']) {
225
+ $adapter = $this->_getAdapter();
226
+ $result = $adapter->delete($this->_getDataTable(), array('id=?'=>$id));
227
+ return $result;
228
+ } else {
229
+ return false;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Clean some cache records
235
+ *
236
+ * Available modes are :
237
+ * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used)
238
+ * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used)
239
+ * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags
240
+ * ($tags can be an array of strings or a single string)
241
+ * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags}
242
+ * ($tags can be an array of strings or a single string)
243
+ * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags
244
+ * ($tags can be an array of strings or a single string)
245
+ *
246
+ * @param string $mode Clean mode
247
+ * @param array $tags Array of tags
248
+ * @return boolean true if no problem
249
+ */
250
+ public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
251
+ {
252
+ $adapter = $this->_getAdapter();
253
+ switch($mode) {
254
+ case Zend_Cache::CLEANING_MODE_ALL:
255
+ if ($this->_options['store_data']) {
256
+ $result = $adapter->query('TRUNCATE TABLE '.$this->_getDataTable());
257
+ } else {
258
+ $result = true;
259
+ }
260
+ $result = $result && $adapter->query('TRUNCATE TABLE '.$this->_getTagsTable());
261
+ break;
262
+ case Zend_Cache::CLEANING_MODE_OLD:
263
+ if ($this->_options['store_data']) {
264
+ $result = $adapter->delete($this->_getDataTable(), array(
265
+ 'expire_time> ?' => 0,
266
+ 'expire_time<= ?' => time()
267
+ ));
268
+ } else {
269
+ $result = true;
270
+ }
271
+ break;
272
+ case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
273
+ case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
274
+ case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
275
+ $result = $this->_cleanByTags($mode, $tags);
276
+ break;
277
+ default:
278
+ Zend_Cache::throwException('Invalid mode for clean() method');
279
+ break;
280
+ }
281
+
282
+ return $result;
283
+ }
284
+
285
+ /**
286
+ * Return an array of stored cache ids
287
+ *
288
+ * @return array array of stored cache ids (string)
289
+ */
290
+ public function getIds()
291
+ {
292
+ if ($this->_options['store_data']) {
293
+ $select = $this->_getAdapter()->select()
294
+ ->from($this->_getDataTable(), 'id');
295
+ return $this->_getAdapter()->fetchCol($select);
296
+ } else {
297
+ return array();
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Return an array of stored tags
303
+ *
304
+ * @return array array of stored tags (string)
305
+ */
306
+ public function getTags()
307
+ {
308
+ $select = $this->_getAdapter()->select()
309
+ ->from($this->_getTagsTable(), 'tag')
310
+ ->distinct(true);
311
+ return $this->_getAdapter()->fetchCol($select);
312
+ }
313
+
314
+ /**
315
+ * Return an array of stored cache ids which match given tags
316
+ *
317
+ * In case of multiple tags, a logical AND is made between tags
318
+ *
319
+ * @param array $tags array of tags
320
+ * @return array array of matching cache ids (string)
321
+ */
322
+ public function getIdsMatchingTags($tags = array())
323
+ {
324
+ $select = $this->_getAdapter()->select()
325
+ ->from($this->_getTagsTable(), 'cache_id')
326
+ ->distinct(true)
327
+ ->where('tag IN(?)', $tags)
328
+ ->group('cache_id')
329
+ ->having('COUNT(cache_id)='.count($tags));
330
+ return $this->_getAdapter()->fetchCol($select);
331
+ }
332
+
333
+ /**
334
+ * Return an array of stored cache ids which don't match given tags
335
+ *
336
+ * In case of multiple tags, a logical OR is made between tags
337
+ *
338
+ * @param array $tags array of tags
339
+ * @return array array of not matching cache ids (string)
340
+ */
341
+ public function getIdsNotMatchingTags($tags = array())
342
+ {
343
+ return array_diff($this->getIds(), $this->getIdsMatchingAnyTags($tags));
344
+ }
345
+
346
+ /**
347
+ * Return an array of stored cache ids which match any given tags
348
+ *
349
+ * In case of multiple tags, a logical AND is made between tags
350
+ *
351
+ * @param array $tags array of tags
352
+ * @return array array of any matching cache ids (string)
353
+ */
354
+ public function getIdsMatchingAnyTags($tags = array())
355
+ {
356
+ $select = $this->_getAdapter()->select()
357
+ ->from($this->_getTagsTable(), 'cache_id')
358
+ ->distinct(true)
359
+ ->where('tag IN(?)', $tags);
360
+ return $this->_getAdapter()->fetchCol($select);
361
+ }
362
+
363
+ /**
364
+ * Return the filling percentage of the backend storage
365
+ *
366
+ * @return int integer between 0 and 100
367
+ */
368
+ public function getFillingPercentage()
369
+ {
370
+ return 1;
371
+ }
372
+
373
+ /**
374
+ * Return an array of metadatas for the given cache id
375
+ *
376
+ * The array must include these keys :
377
+ * - expire : the expire timestamp
378
+ * - tags : a string array of tags
379
+ * - mtime : timestamp of last modification time
380
+ *
381
+ * @param string $id cache id
382
+ * @return array array of metadatas (false if the cache id is not found)
383
+ */
384
+ public function getMetadatas($id)
385
+ {
386
+ $select = $this->_getAdapter()->select()
387
+ ->from($this->_getTagsTable(), 'tag')
388
+ ->where('cache_id=?', $id);
389
+ $tags = $this->_getAdapter()->fetchCol($select);
390
+
391
+ $select = $this->_getAdapter()->select()
392
+ ->from($this->_getDataTable())
393
+ ->where('id=?', $id);
394
+ $data = $this->_getAdapter()->fetchRow($select);
395
+ $res = false;
396
+ if ($data) {
397
+ $res = array (
398
+ 'expire'=> $data['expire_time'],
399
+ 'mtime' => $data['update_time'],
400
+ 'tags' => $tags
401
+ );
402
+ }
403
+ return $res;
404
+ }
405
+
406
+ /**
407
+ * Give (if possible) an extra lifetime to the given cache id
408
+ *
409
+ * @param string $id cache id
410
+ * @param int $extraLifetime
411
+ * @return boolean true if ok
412
+ */
413
+ public function touch($id, $extraLifetime)
414
+ {
415
+ if ($this->_options['store_data']) {
416
+ return $this->_getAdapter()->update(
417
+ $this->_getDataTable(),
418
+ array('expire_time'=>new Zend_Db_Expr('expire_time+'.$extraLifetime)),
419
+ array('id=?'=>$id, 'expire_time = 0 OR expire_time>'=>time())
420
+ );
421
+ } else {
422
+ return true;
423
+ }
424
+ }
425
+
426
+ /**
427
+ * Return an associative array of capabilities (booleans) of the backend
428
+ *
429
+ * The array must include these keys :
430
+ * - automatic_cleaning (is automating cleaning necessary)
431
+ * - tags (are tags supported)
432
+ * - expired_read (is it possible to read expired cache records
433
+ * (for doNotTestCacheValidity option for example))
434
+ * - priority does the backend deal with priority when saving
435
+ * - infinite_lifetime (is infinite lifetime can work with this backend)
436
+ * - get_list (is it possible to get the list of cache ids and the complete list of tags)
437
+ *
438
+ * @return array associative of with capabilities
439
+ */
440
+ public function getCapabilities()
441
+ {
442
+ return array(
443
+ 'automatic_cleaning' => true,
444
+ 'tags' => true,
445
+ 'expired_read' => true,
446
+ 'priority' => false,
447
+ 'infinite_lifetime' => true,
448
+ 'get_list' => true
449
+ );
450
+ }
451
+
452
+ /**
453
+ * Save tags related to specific id
454
+ *
455
+ * @param string $id
456
+ * @param array $tags
457
+ * @return bool
458
+ */
459
+ protected function _saveTags($id, $tags)
460
+ {
461
+ if (!is_array($tags)) {
462
+ $tags = array($tags);
463
+ }
464
+ if (empty($tags)) {
465
+ return true;
466
+ }
467
+
468
+ $adapter = $this->_getAdapter();
469
+ $tagsTable = $this->_getTagsTable();
470
+ $select = $adapter->select()
471
+ ->from($tagsTable, 'tag')
472
+ ->where('cache_id=?', $id)
473
+ ->where('tag IN(?)', $tags);
474
+
475
+ $existingTags = $adapter->fetchCol($select);
476
+ $insertTags = array_diff($tags, $existingTags);
477
+ if (!empty($insertTags)) {
478
+ $query = 'INSERT IGNORE INTO ' . $tagsTable . ' (tag, cache_id) VALUES ';
479
+ $bind = array();
480
+ $lines = array();
481
+ foreach ($insertTags as $tag) {
482
+ $lines[] = '(?, ?)';
483
+ $bind[] = $tag;
484
+ $bind[] = $id;
485
+ }
486
+ $query.= implode(',', $lines);
487
+ $adapter->query($query, $bind);
488
+ }
489
+ $result = true;
490
+ return $result;
491
+ }
492
+
493
+ /**
494
+ * Remove cache data by tags with specified mode
495
+ *
496
+ * @param string $mode
497
+ * @param array $tags
498
+ * @return bool
499
+ */
500
+ protected function _cleanByTags($mode, $tags)
501
+ {
502
+ if ($this->_options['store_data']) {
503
+ $adapter = $this->_getAdapter();
504
+ $select = $adapter->select()
505
+ ->from($this->_getTagsTable(), 'cache_id');
506
+ switch ($mode) {
507
+ case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
508
+ $select->where('tag IN (?)', $tags)
509
+ ->group('cache_id')
510
+ ->having('COUNT(cache_id)='.count($tags));
511
+ break;
512
+ case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
513
+ $select->where('tag NOT IN (?)', $tags);
514
+ break;
515
+ case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
516
+ $select->where('tag IN (?)', $tags);
517
+ break;
518
+ default:
519
+ Zend_Cache::throwException('Invalid mode for _cleanByTags() method');
520
+ break;
521
+ }
522
+
523
+ $result = true;
524
+ $ids = array();
525
+ $counter = 0;
526
+ $stmt = $adapter->query($select);
527
+ while ($row = $stmt->fetch()) {
528
+ $ids[] = $row['cache_id'];
529
+ $counter++;
530
+ if ($counter>100) {
531
+ $result = $result && $adapter->delete($this->_getDataTable(), array('id IN (?)' => $ids));
532
+ $ids = array();
533
+ $counter = 0;
534
+ }
535
+ }
536
+ if (!empty($ids)) {
537
+ $result = $result && $adapter->delete($this->_getDataTable(), array('id IN (?)' => $ids));
538
+ }
539
+ return $result;
540
+ } else {
541
+ return true;
542
+ }
543
+ }
544
+ }
lib/Varien/Cache/Backend/Eaccelerator.php ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Cache
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Varien_Cache_Backend_Eaccelerator extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
28
+ {
29
+ /**
30
+ * Log message
31
+ */
32
+ const TAGS_UNSUPPORTED_BY_CLEAN_OF_EACCELERATOR_BACKEND = 'Varien_Cache_Backend_Eaccelerator::clean() : tags are unsupported by the Eaccelerator backend';
33
+ const TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND = 'Varien_Cache_Backend_Eaccelerator::save() : tags are unsupported by the Eaccelerator backend';
34
+
35
+ /**
36
+ * Constructor
37
+ *
38
+ * @param array $options associative array of options
39
+ * @throws Zend_Cache_Exception
40
+ * @return void
41
+ */
42
+ public function __construct(array $options = array())
43
+ {
44
+ if (!extension_loaded('eaccelerator')) {
45
+ Zend_Cache::throwException('The eaccelerator extension must be loaded for using this backend !');
46
+ }
47
+ parent::__construct($options);
48
+ }
49
+
50
+ /**
51
+ * Test if a cache is available for the given id and (if yes) return it (false else)
52
+ *
53
+ * WARNING $doNotTestCacheValidity=true is unsupported by the Eaccelerator backend
54
+ *
55
+ * @param string $id cache id
56
+ * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
57
+ * @return string cached datas (or false)
58
+ */
59
+ public function load($id, $doNotTestCacheValidity = false)
60
+ {
61
+ $tmp = eaccelerator_get($id);
62
+ if (is_array($tmp)) {
63
+ return $tmp[0];
64
+ }
65
+ return false;
66
+ }
67
+
68
+ /**
69
+ * Test if a cache is available or not (for the given id)
70
+ *
71
+ * @param string $id cache id
72
+ * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record
73
+ */
74
+ public function test($id)
75
+ {
76
+ $tmp = eaccelerator_get($id);
77
+ if (is_array($tmp)) {
78
+ return $tmp[1];
79
+ }
80
+ return false;
81
+ }
82
+
83
+ /**
84
+ * Save some string datas into a cache record
85
+ *
86
+ * Note : $data is always "string" (serialization is done by the
87
+ * core not by the backend)
88
+ *
89
+ * @param string $data datas to cache
90
+ * @param string $id cache id
91
+ * @param array $tags array of strings, the cache record will be tagged by each string entry
92
+ * @param int $specificLifetime if != false, set a specific lifetime for this cache record (null => infinite lifetime)
93
+ * @return boolean true if no problem
94
+ */
95
+ public function save($data, $id, $tags = array(), $specificLifetime = false)
96
+ {
97
+ $lifetime = $this->getLifetime($specificLifetime);
98
+ $result = eaccelerator_put($id, array($data, time(), $lifetime), $lifetime);
99
+ if (count($tags) > 0) {
100
+ $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
101
+ }
102
+ return $result;
103
+ }
104
+
105
+ /**
106
+ * Remove a cache record
107
+ *
108
+ * @param string $id cache id
109
+ * @return boolean true if no problem
110
+ */
111
+ public function remove($id)
112
+ {
113
+ return eaccelerator_rm($id);
114
+ }
115
+
116
+ /**
117
+ * Clean some cache records
118
+ *
119
+ * Available modes are :
120
+ * 'all' (default) => remove all cache entries ($tags is not used)
121
+ * 'old' => unsupported
122
+ * 'matchingTag' => unsupported
123
+ * 'notMatchingTag' => unsupported
124
+ * 'matchingAnyTag' => unsupported
125
+ *
126
+ * @param string $mode clean mode
127
+ * @param array $tags array of tags
128
+ * @throws Zend_Cache_Exception
129
+ * @return boolean true if no problem
130
+ */
131
+ public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
132
+ {
133
+ switch ($mode) {
134
+ case Zend_Cache::CLEANING_MODE_ALL:
135
+ return eaccelerator_clean();
136
+ break;
137
+ case Zend_Cache::CLEANING_MODE_OLD:
138
+ $this->_log("Varien_Cache_Backend_Eaccelerator::clean() : CLEANING_MODE_OLD is unsupported by the Eaccelerator backend");
139
+ break;
140
+ case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
141
+ case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
142
+ case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
143
+ $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_EACCELERATOR_BACKEND);
144
+ break;
145
+ default:
146
+ Zend_Cache::throwException('Invalid mode for clean() method');
147
+ break;
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Return true if the automatic cleaning is available for the backend
153
+ *
154
+ * DEPRECATED : use getCapabilities() instead
155
+ *
156
+ * @deprecated
157
+ * @return boolean
158
+ */
159
+ public function isAutomaticCleaningAvailable()
160
+ {
161
+ return false;
162
+ }
163
+
164
+ /**
165
+ * Return the filling percentage of the backend storage
166
+ *
167
+ * @throws Zend_Cache_Exception
168
+ * @return int integer between 0 and 100
169
+ */
170
+ public function getFillingPercentage()
171
+ {
172
+ $mem = eaccelerator_info();
173
+ $memSize = $mem['memorySize'];
174
+ $memAvailable= $mem['memoryAvailable'];
175
+ $memUsed = $memSize - $memAvailable;
176
+ if ($memSize == 0) {
177
+ Zend_Cache::throwException('can\'t get eaccelerator memory size');
178
+ }
179
+ if ($memUsed > $memSize) {
180
+ return 100;
181
+ }
182
+ return ((int) (100. * ($memUsed / $memSize)));
183
+ }
184
+
185
+ /**
186
+ * Return an array of stored tags
187
+ *
188
+ * @return array array of stored tags (string)
189
+ */
190
+ public function getTags()
191
+ {
192
+ $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
193
+ return array();
194
+ }
195
+
196
+ /**
197
+ * Return an array of stored cache ids which match given tags
198
+ *
199
+ * In case of multiple tags, a logical AND is made between tags
200
+ *
201
+ * @param array $tags array of tags
202
+ * @return array array of matching cache ids (string)
203
+ */
204
+ public function getIdsMatchingTags($tags = array())
205
+ {
206
+ $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
207
+ return array();
208
+ }
209
+
210
+ /**
211
+ * Return an array of stored cache ids which don't match given tags
212
+ *
213
+ * In case of multiple tags, a logical OR is made between tags
214
+ *
215
+ * @param array $tags array of tags
216
+ * @return array array of not matching cache ids (string)
217
+ */
218
+ public function getIdsNotMatchingTags($tags = array())
219
+ {
220
+ $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
221
+ return array();
222
+ }
223
+
224
+ /**
225
+ * Return an array of stored cache ids which match any given tags
226
+ *
227
+ * In case of multiple tags, a logical AND is made between tags
228
+ *
229
+ * @param array $tags array of tags
230
+ * @return array array of any matching cache ids (string)
231
+ */
232
+ public function getIdsMatchingAnyTags($tags = array())
233
+ {
234
+ $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_EACCELERATOR_BACKEND);
235
+ return array();
236
+ }
237
+
238
+ /**
239
+ * Return an array of stored cache ids
240
+ *
241
+ * @return array array of stored cache ids (string)
242
+ */
243
+ public function getIds()
244
+ {
245
+ $res = array();
246
+ $array = eaccelerator_list_keys();
247
+ foreach ($array as $key => $info) {
248
+ $res[] = $key;
249
+ }
250
+ return $res;
251
+ }
252
+
253
+ /**
254
+ * Return an array of metadatas for the given cache id
255
+ *
256
+ * The array must include these keys :
257
+ * - expire : the expire timestamp
258
+ * - tags : a string array of tags
259
+ * - mtime : timestamp of last modification time
260
+ *
261
+ * @param string $id cache id
262
+ * @return array array of metadatas (false if the cache id is not found)
263
+ */
264
+ public function getMetadatas($id)
265
+ {
266
+ $tmp = eaccelerator_get($id);
267
+ if (is_array($tmp)) {
268
+ $data = $tmp[0];
269
+ $mtime = $tmp[1];
270
+ if (!isset($tmp[2])) {
271
+ // because this record is only with 1.7 release
272
+ // if old cache records are still there...
273
+ return false;
274
+ }
275
+ $lifetime = $tmp[2];
276
+ return array(
277
+ 'expire' => $mtime + $lifetime,
278
+ 'tags' => array(),
279
+ 'mtime' => $mtime
280
+ );
281
+ }
282
+ return false;
283
+ }
284
+
285
+ /**
286
+ * Give (if possible) an extra lifetime to the given cache id
287
+ *
288
+ * @param string $id cache id
289
+ * @param int $extraLifetime
290
+ * @return boolean true if ok
291
+ */
292
+ public function touch($id, $extraLifetime)
293
+ {
294
+ $tmp = eaccelerator_get($id);
295
+ if (is_array($tmp)) {
296
+ $data = $tmp[0];
297
+ $mtime = $tmp[1];
298
+ if (!isset($tmp[2])) {
299
+ // because this record is only with 1.7 release
300
+ // if old cache records are still there...
301
+ return false;
302
+ }
303
+ $lifetime = $tmp[2];
304
+ $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
305
+ if ($newLifetime <=0) {
306
+ return false;
307
+ }
308
+ eaccelerator_put($id, array($data, time(), $newLifetime), $newLifetime);
309
+ return true;
310
+ }
311
+ return false;
312
+ }
313
+
314
+ /**
315
+ * Return an associative array of capabilities (booleans) of the backend
316
+ *
317
+ * The array must include these keys :
318
+ * - automatic_cleaning (is automating cleaning necessary)
319
+ * - tags (are tags supported)
320
+ * - expired_read (is it possible to read expired cache records
321
+ * (for doNotTestCacheValidity option for example))
322
+ * - priority does the backend deal with priority when saving
323
+ * - infinite_lifetime (is infinite lifetime can work with this backend)
324
+ * - get_list (is it possible to get the list of cache ids and the complete list of tags)
325
+ *
326
+ * @return array associative of with capabilities
327
+ */
328
+ public function getCapabilities()
329
+ {
330
+ return array(
331
+ 'automatic_cleaning' => false,
332
+ 'tags' => false,
333
+ 'expired_read' => false,
334
+ 'priority' => false,
335
+ 'infinite_lifetime' => false,
336
+ 'get_list' => true
337
+ );
338
+ }
339
+
340
+ }
lib/Varien/Cache/Core.php ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Cache
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ class Varien_Cache_Core extends Zend_Cache_Core
28
+ {
29
+ /**
30
+ * Make and return a cache id
31
+ *
32
+ * Checks 'cache_id_prefix' and returns new id with prefix or simply the id if null
33
+ *
34
+ * @param string $id Cache id
35
+ * @return string Cache id (with or without prefix)
36
+ */
37
+ protected function _id($id)
38
+ {
39
+ if ($id !== null) {
40
+ $id = preg_replace('/([^a-zA-Z0-9_]{1,1})/', '_', $id);
41
+ if (isset($this->_options['cache_id_prefix'])) {
42
+ $id = $this->_options['cache_id_prefix'] . $id;
43
+ }
44
+ }
45
+ return $id;
46
+ }
47
+
48
+ /**
49
+ * Prepare tags
50
+ *
51
+ * @param array $tags
52
+ * @return array
53
+ */
54
+ protected function _tags($tags)
55
+ {
56
+ foreach ($tags as $key=>$tag) {
57
+ $tags[$key] = $this->_id($tag);
58
+ }
59
+ return $tags;
60
+ }
61
+
62
+ /**
63
+ * Save some data in a cache
64
+ *
65
+ * @param mixed $data Data to put in cache (can be another type than string if automatic_serialization is on)
66
+ * @param string $id Cache id (if not set, the last cache id will be used)
67
+ * @param array $tags Cache tags
68
+ * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime)
69
+ * @param int $priority integer between 0 (very low priority) and 10 (maximum priority) used by some particular backends
70
+ * @throws Zend_Cache_Exception
71
+ * @return boolean True if no problem
72
+ */
73
+ public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8)
74
+ {
75
+ $tags = $this->_tags($tags);
76
+ return parent::save($data, $id, $tags, $specificLifetime, $priority);
77
+ }
78
+
79
+ /**
80
+ * Clean cache entries
81
+ *
82
+ * Available modes are :
83
+ * 'all' (default) => remove all cache entries ($tags is not used)
84
+ * 'old' => remove too old cache entries ($tags is not used)
85
+ * 'matchingTag' => remove cache entries matching all given tags
86
+ * ($tags can be an array of strings or a single string)
87
+ * 'notMatchingTag' => remove cache entries not matching one of the given tags
88
+ * ($tags can be an array of strings or a single string)
89
+ * 'matchingAnyTag' => remove cache entries matching any given tags
90
+ * ($tags can be an array of strings or a single string)
91
+ *
92
+ * @param string $mode
93
+ * @param array|string $tags
94
+ * @throws Zend_Cache_Exception
95
+ * @return boolean True if ok
96
+ */
97
+ public function clean($mode = 'all', $tags = array())
98
+ {
99
+ $tags = $this->_tags($tags);
100
+ return parent::clean($mode, $tags);
101
+ }
102
+
103
+ /**
104
+ * Return an array of stored cache ids which match given tags
105
+ *
106
+ * In case of multiple tags, a logical AND is made between tags
107
+ *
108
+ * @param array $tags array of tags
109
+ * @return array array of matching cache ids (string)
110
+ */
111
+ public function getIdsMatchingTags($tags = array())
112
+ {
113
+ $tags = $this->_tags($tags);
114
+ return parent::getIdsMatchingTags($tags);
115
+ }
116
+
117
+ /**
118
+ * Return an array of stored cache ids which don't match given tags
119
+ *
120
+ * In case of multiple tags, a logical OR is made between tags
121
+ *
122
+ * @param array $tags array of tags
123
+ * @return array array of not matching cache ids (string)
124
+ */
125
+ public function getIdsNotMatchingTags($tags = array())
126
+ {
127
+ $tags = $this->_tags($tags);
128
+ return parent::getIdsNotMatchingTags($tags);
129
+ }
130
+ }
lib/Varien/Convert.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert factory
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert
36
+ {
37
+ static public function convert($class, $method, $data, array $vars=array())
38
+ {
39
+ if (is_string($class)) {
40
+ $class = new $class();
41
+ }
42
+ $action = new Varien_Convert_Action();
43
+ $action->setParam('method', $method)->setParam('class', $class);
44
+
45
+ $container = $action->getContainer();
46
+ $container->setData($data)->setVars($vars);
47
+
48
+ $action->run();
49
+ return $action->getData();
50
+ }
51
+ }
lib/Varien/Convert/Action.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert action
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Action extends Varien_Convert_Action_Abstract
36
+ {
37
+
38
+ }
lib/Varien/Convert/Action/Abstract.php ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert action abstract
30
+ *
31
+ * Instances of this class are used as actions in profile
32
+ *
33
+ * @category Varien
34
+ * @package Varien_Convert
35
+ * @author Magento Core Team <core@magentocommerce.com>
36
+ */
37
+ abstract class Varien_Convert_Action_Abstract implements Varien_Convert_Action_Interface
38
+ {
39
+ /**
40
+ * Action parameters
41
+ *
42
+ * Hold information about action container
43
+ *
44
+ * @var array
45
+ */
46
+ protected $_params;
47
+
48
+ /**
49
+ * Reference to profile this action belongs to
50
+ *
51
+ * @var Varien_Convert_Profile_Abstract
52
+ */
53
+ protected $_profile;
54
+
55
+ /**
56
+ * Action's container
57
+ *
58
+ * @var Varien_Convert_Container_Abstract
59
+ */
60
+ protected $_container;
61
+
62
+ /**
63
+ * Get action parameter
64
+ *
65
+ * @param string $key
66
+ * @param mixed $default
67
+ * @return mixed
68
+ */
69
+ public function getParam($key, $default=null)
70
+ {
71
+ if (!isset($this->_params[$key])) {
72
+ return $default;
73
+ }
74
+ return $this->_params[$key];
75
+ }
76
+
77
+ /**
78
+ * Set action parameter
79
+ *
80
+ * @param string $key
81
+ * @param mixed $value
82
+ * @return Varien_Convert_Action_Abstract
83
+ */
84
+ public function setParam($key, $value=null)
85
+ {
86
+ if (is_array($key) && is_null($value)) {
87
+ $this->_params = $key;
88
+ } else {
89
+ $this->_params[$key] = $value;
90
+ }
91
+ return $this;
92
+ }
93
+
94
+ /**
95
+ * Get all action parameters
96
+ *
97
+ * @return array
98
+ */
99
+ public function getParams()
100
+ {
101
+ return $this->_params;
102
+ }
103
+
104
+ /**
105
+ * Set all action parameters
106
+ *
107
+ * @param array $params
108
+ * @return Varien_Convert_Action_Abstract
109
+ */
110
+ public function setParams($params)
111
+ {
112
+ $this->_params = $params;
113
+ return $this;
114
+ }
115
+
116
+ /**
117
+ * Get profile instance the action belongs to
118
+ *
119
+ * @return Varien_Convert_Profile_Abstract
120
+ */
121
+ public function getProfile()
122
+ {
123
+ return $this->_profile;
124
+ }
125
+
126
+ /**
127
+ * Set profile instance the action belongs to
128
+ *
129
+ * @param Varien_Convert_Profile_Abstract $profile
130
+ * @return Varien_Convert_Action_Abstract
131
+ */
132
+ public function setProfile(Varien_Convert_Profile_Abstract $profile)
133
+ {
134
+ $this->_profile = $profile;
135
+ return $this;
136
+ }
137
+
138
+ /**
139
+ * Set action's container
140
+ *
141
+ * @param Varien_Convert_Container_Abstract $container
142
+ * @return Varien_Convert_Action_Abstract
143
+ */
144
+ public function setContainer(Varien_Convert_Container_Interface $container)
145
+ {
146
+ $this->_container = $container;
147
+ $this->_container->setProfile($this->getProfile());
148
+ return $this;
149
+ }
150
+
151
+ /**
152
+ * Get action's container
153
+ *
154
+ * @param string $name
155
+ * @return Varien_Convert_Container_Abstract
156
+ */
157
+ public function getContainer($name=null)
158
+ {
159
+ if (!is_null($name)) {
160
+ return $this->getProfile()->getContainer($name);
161
+ }
162
+
163
+ if (!$this->_container) {
164
+ $class = $this->getParam('class');
165
+ $this->setContainer(new $class());
166
+ }
167
+ return $this->_container;
168
+ }
169
+
170
+ /**
171
+ * Run current action
172
+ *
173
+ * @return Varien_Convert_Action_Abstract
174
+ */
175
+ public function run()
176
+ {
177
+ if ($method = $this->getParam('method')) {
178
+ if (!is_callable(array($this->getContainer(), $method))) {
179
+ $this->addException('Unable to run action method: '.$method, Varien_Convert_Exception::FATAL);
180
+ }
181
+
182
+ $this->getContainer()->addException('Starting '.get_class($this->getContainer()).' :: '.$method);
183
+
184
+ if ($this->getParam('from')) {
185
+ $this->getContainer()->setData($this->getContainer($this->getParam('from'))->getData());
186
+ }
187
+
188
+ $this->getContainer()->$method();
189
+
190
+ if ($this->getParam('to')) {
191
+ $this->getContainer($this->getParam('to'))->setData($this->getContainer()->getData());
192
+ }
193
+ } else {
194
+ $this->addException('No method specified', Varien_Convert_Exception::FATAL);
195
+ }
196
+ return $this;
197
+ }
198
+
199
+ }
lib/Varien/Convert/Action/Interface.php ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert action interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Action_Interface
36
+ {
37
+ /**
38
+ * Run current action
39
+ *
40
+ * @return Varien_Convert_Action_Abstract
41
+ */
42
+ public function run();
43
+ }
lib/Varien/Convert/Adapter/Abstract.php ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert abstract adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Adapter_Abstract extends Varien_Convert_Container_Abstract implements Varien_Convert_Adapter_Interface
36
+ {
37
+ /**
38
+ * Adapter resource instance
39
+ *
40
+ * @var object
41
+ */
42
+ protected $_resource;
43
+
44
+ /**
45
+ * Retrieve resource generic method
46
+ *
47
+ * @return object
48
+ */
49
+ public function getResource()
50
+ {
51
+ return $this->_resource;
52
+ }
53
+
54
+ /**
55
+ * Set resource for the adapter
56
+ *
57
+ * @param object $resource
58
+ * @return Varien_Convert_Adapter_Abstract
59
+ */
60
+ public function setResource($resource)
61
+ {
62
+ $this->_resource = $resource;
63
+ return $this;
64
+ }
65
+ }
lib/Varien/Convert/Adapter/Db/Table.php ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert db table adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Db_Table extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function getResource()
38
+ {
39
+ if (!$this->_resource) {
40
+ $this->_resource = Zend_Db::factory($this->getVar('type'), $this->getVars());
41
+ }
42
+ return $this->_resource;
43
+ }
44
+
45
+ public function load()
46
+ {
47
+
48
+ }
49
+
50
+ public function save()
51
+ {
52
+
53
+ }
54
+ }
lib/Varien/Convert/Adapter/Http.php ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert HTTP adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Http extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function load()
38
+ {
39
+ if (!$_FILES) {
40
+ ?>
41
+ <form method="POST" enctype="multipart/form-data">
42
+ File to upload: <input type="file" name="io_file"/> <input type="submit" value="Upload"/>
43
+ </form>
44
+ <?php
45
+ exit;
46
+ }
47
+ if (!empty($_FILES['io_file']['tmp_name'])) {
48
+ $this->setData(file_get_contents($_FILES['io_file']['tmp_name']));
49
+ }
50
+ return $this;
51
+ }
52
+
53
+ public function save()
54
+ {
55
+ if ($this->getVars()) {
56
+ foreach ($this->getVars() as $key=>$value) {
57
+ header($key.': '.$value);
58
+ }
59
+ }
60
+ echo $this->getData();
61
+ return $this;
62
+ }
63
+
64
+ // experimental code
65
+ public function loadFile()
66
+ {
67
+ if (!$_FILES) {
68
+ ?>
69
+ <form method="POST" enctype="multipart/form-data">
70
+ File to upload: <input type="file" name="io_file"/> <input type="submit" value="Upload"/>
71
+ </form>
72
+ <?php
73
+ exit;
74
+ }
75
+ if (!empty($_FILES['io_file']['tmp_name'])) {
76
+ //$this->setData(file_get_contents($_FILES['io_file']['tmp_name']));
77
+ $uploader = new Varien_File_Uploader('io_file');
78
+ $uploader->setAllowedExtensions(array('csv','xml'));
79
+ $path = Mage::app()->getConfig()->getTempVarDir().'/import/';
80
+ $uploader->save($path);
81
+ if ($uploadFile = $uploader->getUploadedFileName()) {
82
+ $session = Mage::getModel('dataflow/session');
83
+ $session->setCreatedDate(date('Y-m-d H:i:s'));
84
+ $session->setDirection('import');
85
+ $session->setUserId(Mage::getSingleton('admin/session')->getUser()->getId());
86
+ $session->save();
87
+ $sessionId = $session->getId();
88
+ $newFilename = 'import_'.$sessionId.'_'.$uploadFile;
89
+ rename($path.$uploadFile, $path.$newFilename);
90
+ $session->setFile($newFilename);
91
+ $session->save();
92
+ $this->setData(file_get_contents($path.$newFilename));
93
+ Mage::register('current_dataflow_session_id', $sessionId);
94
+ }
95
+ }
96
+ return $this;
97
+ } // end
98
+ }
lib/Varien/Convert/Adapter/Http/Curl.php ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert CURL HTTP adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Http_Curl extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ // load method
38
+ public function load()
39
+ {
40
+ // we expect <var name="uri">http://...</var>
41
+ $uri = $this->getVar('uri');
42
+
43
+ // validate input parameter
44
+ if (!Zend_Uri::check($uri)) {
45
+ $this->addException("Expecting a valid 'uri' parameter");
46
+ }
47
+
48
+ // use Varien curl adapter
49
+ $http = new Varien_Http_Adapter_Curl;
50
+
51
+ // send GET request
52
+ $http->write('GET', $uri);
53
+
54
+ // read the remote file
55
+ $data = $http->read();
56
+
57
+ $data = preg_split('/^\r?$/m', $data, 2);
58
+ $data = trim($data[1]);
59
+
60
+ // save contents into container
61
+ $this->setData($data);
62
+
63
+ return $this;
64
+ }
65
+
66
+ public function save()
67
+ {
68
+ // no save implemented
69
+ return $this;
70
+ }
71
+ }
lib/Varien/Convert/Adapter/Interface.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert adapter interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Adapter_Interface
36
+ {
37
+ public function load();
38
+
39
+ public function save();
40
+ }
lib/Varien/Convert/Adapter/Io.php ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert IO adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Io extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function getResource()
38
+ {
39
+ if (!$this->_resource) {
40
+ $type = $this->getVar('type', 'file');
41
+ $className = 'Varien_Io_'.ucwords($type);
42
+ $this->_resource = new $className();
43
+ try {
44
+ $this->_resource->open($this->getVars());
45
+ } catch (Exception $e) {
46
+ $this->addException('Error occured during file opening: '.$e->getMessage(), Varien_Convert_Exception::FATAL);
47
+ }
48
+ }
49
+ return $this->_resource;
50
+ }
51
+
52
+ public function load()
53
+ {
54
+ $data = $this->getResource()->read($this->getVar('filename'));
55
+ $filename = $this->getResource()->pwd().'/'.$this->getVar('filename');
56
+ if (false===$data) {
57
+ $this->addException('Could not load file: '.$filename, Varien_Convert_Exception::FATAL);
58
+ } else {
59
+ $this->addException('Loaded successfully: '.$filename.' ['.strlen($data).' byte(s)]');
60
+ }
61
+ $this->setData($data);
62
+ return $this;
63
+ }
64
+
65
+ public function save()
66
+ {
67
+ $data = $this->getData();
68
+ $filename = $this->getResource()->pwd().'/'.$this->getVar('filename');
69
+ $result = $this->getResource()->write($filename, $data, 0777);
70
+ if (false===$result) {
71
+ $this->addException('Could not save file: '.$filename, Varien_Convert_Exception::FATAL);
72
+ } else {
73
+ $text = 'Saved successfully: '.$filename.' ['.strlen($data).' byte(s)]';
74
+ if ($this->getVar('link')) {
75
+ $text .= ' <a href="'.$this->getVar('link').'" target="_blank">Link</a>';
76
+ }
77
+ $this->addException($text);
78
+ }
79
+ return $this;
80
+ }
81
+ }
lib/Varien/Convert/Adapter/Soap.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert SOAP adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Soap extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function load()
38
+ {
39
+ return $this;
40
+ }
41
+
42
+ public function save()
43
+ {
44
+ return $this;
45
+ }
46
+ }
lib/Varien/Convert/Adapter/Std.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert STDIO adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Std extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function load()
38
+ {
39
+ $data = '';
40
+ $stdin = fopen('php://STDIN', 'r');
41
+ while ($text = fread($stdin, 1024)) {
42
+ $data .= $text;
43
+ }
44
+ $this->setData($data);
45
+ return $this;
46
+ }
47
+
48
+ public function save()
49
+ {
50
+ echo $this->getData();
51
+ return $this;
52
+ }
53
+ }
lib/Varien/Convert/Adapter/Zend/Cache.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert zend cache adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Zend_Cache extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function getResource()
38
+ {
39
+ if (!$this->_resource) {
40
+ $this->_resource = Zend_Cache::factory($this->getVar('frontend', 'Core'), $this->getVar('backend', 'File'));
41
+ }
42
+ return $this->_resource;
43
+ }
44
+
45
+ public function load()
46
+ {
47
+ $this->setData($this->getResource()->load($this->getVar('id')));
48
+ return $this;
49
+ }
50
+
51
+ public function save()
52
+ {
53
+ $this->getResource()->save($this->getData(), $this->getVar('id'));
54
+ return $this;
55
+ }
56
+ }
lib/Varien/Convert/Adapter/Zend/Db.php ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert zend db adapter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Adapter_Zend_Db extends Varien_Convert_Adapter_Abstract
36
+ {
37
+ public function getResource()
38
+ {
39
+ if (!$this->_resource) {
40
+ $this->_resource = Zend_Db::factory($this->getVar('adapter', 'Pdo_Mysql'), $this->getVars());
41
+ }
42
+ return $this->_resource;
43
+ }
44
+
45
+ public function load()
46
+ {
47
+ return $this;
48
+ }
49
+
50
+ public function save()
51
+ {
52
+ return $this;
53
+ }
54
+ }
lib/Varien/Convert/Container/Abstract.php ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert container abstract
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Container_Abstract implements Varien_Convert_Container_Interface
36
+ {
37
+ protected $_vars;
38
+ protected $_profile;
39
+ protected $_data;
40
+ protected $_position;
41
+
42
+ public function getVar($key, $default=null)
43
+ {
44
+ if (!isset($this->_vars[$key])) {
45
+ return $default;
46
+ }
47
+ return $this->_vars[$key];
48
+ }
49
+
50
+ public function getVars()
51
+ {
52
+ return $this->_vars;
53
+ }
54
+
55
+ public function setVar($key, $value=null)
56
+ {
57
+ if (is_array($key) && is_null($value)) {
58
+ $this->_vars = $key;
59
+ } else {
60
+ $this->_vars[$key] = $value;
61
+ }
62
+ return $this;
63
+ }
64
+
65
+ public function getProfile()
66
+ {
67
+ return $this->_profile;
68
+ }
69
+
70
+ public function setProfile(Varien_Convert_Profile_Abstract $profile)
71
+ {
72
+ $this->_profile = $profile;
73
+ return $this;
74
+ }
75
+
76
+ public function getData()
77
+ {
78
+ if (is_null($this->_data) && $this->getProfile()) {
79
+ $this->_data = $this->getProfile()->getContainer()->getData();
80
+ }
81
+ return $this->_data;
82
+ }
83
+
84
+ public function setData($data)
85
+ {
86
+ if ($this->getProfile()) {
87
+ $this->getProfile()->getContainer()->setData($data);
88
+ }
89
+ $this->_data = $data;
90
+ return $this;
91
+ }
92
+
93
+ public function validateDataString($data=null)
94
+ {
95
+ if (is_null($data)) {
96
+ $data = $this->getData();
97
+ }
98
+ if (!is_string($data)) {
99
+ $this->addException("Invalid data type, expecting string.", Varien_Convert_Exception::FATAL);
100
+ }
101
+ return true;
102
+ }
103
+
104
+ public function validateDataArray($data=null)
105
+ {
106
+ if (is_null($data)) {
107
+ $data = $this->getData();
108
+ }
109
+ if (!is_array($data)) {
110
+ $this->addException("Invalid data type, expecting array.", Varien_Convert_Exception::FATAL);
111
+ }
112
+ return true;
113
+ }
114
+
115
+ public function validateDataGrid($data=null)
116
+ {
117
+ if (is_null($data)) {
118
+ $data = $this->getData();
119
+ }
120
+ if (!is_array($data) || !is_array(current($data))) {
121
+ if (count($data)==0) {
122
+ return true;
123
+ }
124
+ $this->addException("Invalid data type, expecting 2D grid array.", Varien_Convert_Exception::FATAL);
125
+ }
126
+ return true;
127
+ }
128
+
129
+ public function getGridFields($grid)
130
+ {
131
+ $fields = array();
132
+ foreach ($grid as $i=>$row) {
133
+ foreach ($row as $fieldName=>$data) {
134
+ if (!in_array($fieldName, $fields)) {
135
+ $fields[] = $fieldName;
136
+ }
137
+ }
138
+ }
139
+ return $fields;
140
+ }
141
+
142
+ public function addException($error, $level=null)
143
+ {
144
+ $e = new Varien_Convert_Exception($error);
145
+ $e->setLevel(!is_null($level) ? $level : Varien_Convert_Exception::NOTICE);
146
+ $e->setContainer($this);
147
+ $e->setPosition($this->getPosition());
148
+
149
+ if ($this->getProfile()) {
150
+ $this->getProfile()->addException($e);
151
+ }
152
+
153
+ return $e;
154
+ }
155
+
156
+ public function getPosition()
157
+ {
158
+ return $this->_position;
159
+ }
160
+
161
+ public function setPosition($position)
162
+ {
163
+ $this->_position = $position;
164
+ return $this;
165
+ }
166
+ }
lib/Varien/Convert/Container/Collection.php ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert component collection
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Container_Collection
36
+ {
37
+ protected $_items = array();
38
+ protected $_defaultClass = 'Varien_Convert_Container_Generic';
39
+
40
+ public function setDefaultClass($className)
41
+ {
42
+ $this->_defaultClass = $className;
43
+ return $this;
44
+ }
45
+
46
+ public function addItem($name, Varien_Convert_Container_Interface $item)
47
+ {
48
+ if (is_null($name)) {
49
+ if ($item->getName()) {
50
+ $name = $item->getName();
51
+ } else {
52
+ $name = sizeof($this->_items);
53
+ }
54
+ }
55
+
56
+ $this->_items[$name] = $item;
57
+
58
+ return $item;
59
+ }
60
+
61
+ public function getItem($name)
62
+ {
63
+ if (!isset($this->_items[$name])) {
64
+ $this->addItem($name, new $this->_defaultClass());
65
+ }
66
+ return $this->_items[$name];
67
+ }
68
+
69
+ public function hasItem($name)
70
+ {
71
+ return isset($this->_items[$name]);
72
+ }
73
+ }
lib/Varien/Convert/Container/Generic.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert generic container
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Container_Generic extends Varien_Convert_Container_Abstract
36
+ {
37
+
38
+ }
lib/Varien/Convert/Container/Interface.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert container interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Container_Interface
36
+ {
37
+
38
+ }
lib/Varien/Convert/Exception.php ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert exception
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Exception extends Varien_Exception
36
+ {
37
+ const NOTICE = 'NOTICE';
38
+ const WARNING = 'WARNING';
39
+ const ERROR = 'ERROR';
40
+ const FATAL = 'FATAL';
41
+
42
+ protected $_container;
43
+
44
+ protected $_level;
45
+
46
+ protected $_position;
47
+
48
+ public function setContainer($container)
49
+ {
50
+ $this->_container = $container;
51
+ return $this;
52
+ }
53
+
54
+ public function getContainer()
55
+ {
56
+ return $this->_container;
57
+ }
58
+
59
+ public function getLevel()
60
+ {
61
+ return $this->_level;
62
+ }
63
+
64
+ public function setLevel($level)
65
+ {
66
+ $this->_level = $level;
67
+ return $this;
68
+ }
69
+
70
+ public function getPosition()
71
+ {
72
+ return $this->_position;
73
+ }
74
+
75
+ public function setPosition($position)
76
+ {
77
+ $this->_position = $position;
78
+ return $this;
79
+ }
80
+ }
lib/Varien/Convert/Mapper/Abstract.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert mapper abstract
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Mapper_Abstract extends Varien_Convert_Container_Abstract implements Varien_Convert_Mapper_Interface
36
+ {
37
+
38
+ }
lib/Varien/Convert/Mapper/Column.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert column mapper
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Mapper_Column extends Varien_Convert_Mapper_Abstract
36
+ {
37
+ public function map()
38
+ {
39
+ $data = $this->getData();
40
+ $this->validateDataGrid($data);
41
+ if ($this->getVars() && is_array($this->getVars())) {
42
+ $attributesToSelect = $this->getVars();
43
+ } else {
44
+ $attributesToSelect = array();
45
+ }
46
+ $onlySpecified = (bool)$this->getVar('_only_specified')===true;
47
+ $mappedData = array();
48
+ foreach ($data as $i=>$row) {
49
+ $newRow = array();
50
+ foreach ($row as $field=>$value) {
51
+ if(!$onlySpecified || $onlySpecified && isset($attributesToSelect[$field])) {
52
+ $newRow[$this->getVar($field, $field)] = $value;
53
+ }
54
+ }
55
+ $mappedData[$i] = $newRow;
56
+ }
57
+ $this->setData($mappedData);
58
+ return $this;
59
+ }
60
+ }
lib/Varien/Convert/Mapper/Interface.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert mapper interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Mapper_Interface
36
+ {
37
+ public function map();
38
+ }
lib/Varien/Convert/Parser/Abstract.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert parser abstract
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Parser_Abstract extends Varien_Convert_Container_Abstract implements Varien_Convert_Parser_Interface
36
+ {
37
+
38
+ }
lib/Varien/Convert/Parser/Csv.php ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert csv parser
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Parser_Csv extends Varien_Convert_Parser_Abstract
36
+ {
37
+ public function parse()
38
+ {
39
+ $fDel = $this->getVar('delimiter', ',');
40
+ $fEnc = $this->getVar('enclose', '"');
41
+
42
+ if ($fDel=='\\t') {
43
+ $fDel = "\t";
44
+ }
45
+
46
+ // fixed for multibyte characters
47
+ setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
48
+
49
+ $fp = tmpfile();
50
+ fputs($fp, $this->getData());
51
+ fseek($fp, 0);
52
+
53
+ $data = array();
54
+ for ($i=0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
55
+ if (0==$i) {
56
+ if ($this->getVar('fieldnames')) {
57
+ $fields = $line;
58
+ continue;
59
+ } else {
60
+ foreach ($line as $j=>$f) {
61
+ $fields[$j] = 'column'.($j+1);
62
+ }
63
+ }
64
+ }
65
+ $row = array();
66
+ foreach ($fields as $j=>$f) {
67
+ $row[$f] = $line[$j];
68
+ }
69
+ $data[] = $row;
70
+ }
71
+ fclose($fp);
72
+ $this->setData($data);
73
+ return $this;
74
+ }
75
+
76
+ // experimental code
77
+ public function parseTest()
78
+ {
79
+ $fDel = $this->getVar('delimiter', ',');
80
+ $fEnc = $this->getVar('enclose', '"');
81
+
82
+ if ($fDel=='\\t') {
83
+ $fDel = "\t";
84
+ }
85
+
86
+ // fixed for multibyte characters
87
+ setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
88
+
89
+ $fp = tmpfile();
90
+ fputs($fp, $this->getData());
91
+ fseek($fp, 0);
92
+
93
+ $data = array();
94
+ $sessionId = Mage::registry('current_dataflow_session_id');
95
+ $import = Mage::getModel('dataflow/import');
96
+ $map = new Varien_Convert_Mapper_Column();
97
+ for ($i=0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
98
+ if (0==$i) {
99
+ if ($this->getVar('fieldnames')) {
100
+ $fields = $line;
101
+ continue;
102
+ } else {
103
+ foreach ($line as $j=>$f) {
104
+ $fields[$j] = 'column'.($j+1);
105
+ }
106
+ }
107
+ }
108
+ $row = array();
109
+ foreach ($fields as $j=>$f) {
110
+ $row[$f] = $line[$j];
111
+ }
112
+ $map->setData(array($row));
113
+ $map->map();
114
+ $row = $map->getData();
115
+ $import->setImportId(0);
116
+ $import->setSessionId($sessionId);
117
+ $import->setSerialNumber($i);
118
+ $import->setValue(serialize($row[0]));
119
+ $import->save();
120
+ }
121
+ fclose($fp);
122
+ unset($sessionId);
123
+ return $this;
124
+ } // end
125
+
126
+ public function unparse()
127
+ {
128
+ $csv = '';
129
+
130
+ $fDel = $this->getVar('delimiter', ',');
131
+ $fEnc = $this->getVar('enclose', '"');
132
+ $fEsc = $this->getVar('escape', '\\');
133
+ $lDel = "\r\n";
134
+
135
+ if ($fDel=='\\t') {
136
+ $fDel = "\t";
137
+ }
138
+
139
+ $data = $this->getData();
140
+ $fields = $this->getGridFields($data);
141
+ $lines = array();
142
+
143
+ if ($this->getVar('fieldnames')) {
144
+ $line = array();
145
+ foreach ($fields as $f) {
146
+ $line[] = $fEnc.str_replace(array('"', '\\'), array($fEsc.'"', $fEsc.'\\'), $f).$fEnc;
147
+ }
148
+ $lines[] = join($fDel, $line);
149
+ }
150
+ foreach ($data as $i=>$row) {
151
+ $line = array();
152
+ foreach ($fields as $f) {
153
+ /*
154
+ if (isset($row[$f]) && (preg_match('\"', $row[$f]) || preg_match('\\', $row[$f]))) {
155
+ $tmp = str_replace('\\', '\\\\',$row[$f]);
156
+ echo str_replace('"', '\"',$tmp).'<br>';
157
+ }
158
+ */
159
+ $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEnc.'"', $fEsc.'\\'), $row[$f]) : '';
160
+
161
+ $line[] = $fEnc.$v.$fEnc;
162
+ }
163
+ $lines[] = join($fDel, $line);
164
+ }
165
+ $result = join($lDel, $lines);
166
+ $this->setData($result);
167
+
168
+ return $this;
169
+ }
170
+ }
lib/Varien/Convert/Parser/Interface.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert parser interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Parser_Interface
36
+ {
37
+ public function parse();
38
+
39
+ public function unparse();
40
+ }
lib/Varien/Convert/Parser/Serialize.php ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert php serialize parser
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Parser_Serialize extends Varien_Convert_Parser_Abstract
36
+ {
37
+ public function parse()
38
+ {
39
+ $this->setData(unserialize($this->getData()));
40
+ return $this;
41
+ }
42
+
43
+ public function unparse()
44
+ {
45
+ $this->setData(serialize($this->getData()));
46
+ return $this;
47
+ }
48
+ }
lib/Varien/Convert/Parser/Xml/Excel.php ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert excel xml parser
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Parser_Xml_Excel extends Varien_Convert_Parser_Abstract
36
+ {
37
+ /**
38
+ * XML instance for a cell data
39
+ *
40
+ * @var SimpleXMLElement
41
+ */
42
+ protected $_xmlElement;
43
+
44
+ public function parse()
45
+ {
46
+ $this->validateDataString();
47
+
48
+ $dom = new DOMDocument();
49
+ $dom->loadXML($this->getData());
50
+
51
+ $worksheets = $dom->getElementsByTagName('Worksheet');
52
+
53
+ foreach ($worksheets as $worksheet) {
54
+ $wsName = $worksheet->getAttribute('ss:Name');
55
+ $rows = $worksheet->getElementsByTagName('Row');
56
+ $firstRow = true;
57
+ $fieldNames = array();
58
+ $wsData = array();
59
+ foreach ($rows as $row) {
60
+ $index = 1;
61
+ $cells = $row->getElementsByTagName('Cell');
62
+ $rowData = array();
63
+ foreach ($cells as $cell) {
64
+ $value = $cell->getElementsByTagName('Data')->item(0)->nodeValue;
65
+ $ind = $cell->getAttribute('ss:Index');
66
+ if (!is_null($ind) && $ind>0) {
67
+ $index = $ind;
68
+ }
69
+ if ($firstRow && !$this->getVar('fieldnames')) {
70
+ $fieldNames[$index] = 'column'.$index;
71
+ }
72
+ if ($firstRow && $this->getVar('fieldnames')) {
73
+ $fieldNames[$index] = $value;
74
+ } else {
75
+ $rowData[$fieldNames[$index]] = $value;
76
+ }
77
+ $index++;
78
+ }
79
+ $firstRow = false;
80
+ if (!empty($rowData)) {
81
+ $wsData[] = $rowData;
82
+ }
83
+ }
84
+ $data[$wsName] = $wsData;
85
+ $this->addException('Found worksheet "'.$wsName.'" with '.sizeof($wsData).' row(s)');
86
+ }
87
+ if ($wsName = $this->getVar('single_sheet')) {
88
+ if (isset($data[$wsName])) {
89
+ $data = $data[$wsName];
90
+ } else {
91
+ reset($data);
92
+ $data = current($data);
93
+ }
94
+ }
95
+ $this->setData($data);
96
+ return $this;
97
+ }
98
+
99
+ public function unparse()
100
+ {
101
+ if ($wsName = $this->getVar('single_sheet')) {
102
+ $data = array($wsName => $this->getData());
103
+ } else {
104
+ $data = $this->getData();
105
+ }
106
+
107
+ $this->validateDataGrid();
108
+
109
+ $xml = '<'.'?xml version="1.0"?'.'><'.'?mso-application progid="Excel.Sheet"?'.'>
110
+ <Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
111
+ xmlns="urn:schemas-microsoft-com:office:spreadsheet"
112
+ xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
113
+
114
+ if (is_array($data)) {
115
+ foreach ($data as $wsName=>$wsData) {
116
+ if (!is_array($wsData)) {
117
+ continue;
118
+ }
119
+ $fields = $this->getGridFields($wsData);
120
+
121
+ $xml .= '<Worksheet ss:Name="'.$wsName.'"><ss:Table>';
122
+ if ($this->getVar('fieldnames')) {
123
+ $xml .= '<ss:Row>';
124
+ foreach ($fields as $fieldName) {
125
+ $xml .= '<ss:Cell><Data ss:Type="String">'.$fieldName.'</Data></ss:Cell>';
126
+ }
127
+ $xml .= '</ss:Row>';
128
+ }
129
+ foreach ($wsData as $i=>$row) {
130
+ if (!is_array($row)) {
131
+ continue;
132
+ }
133
+ $xml .= '<ss:Row>';
134
+ foreach ($fields as $fieldName) {
135
+ $data = isset($row[$fieldName]) ? $row[$fieldName] : '';
136
+ $xml .= '<ss:Cell><Data ss:Type="String">'.$data.'</Data></ss:Cell>';
137
+ }
138
+ $xml .= '</ss:Row>';
139
+ }
140
+ $xml .= '</ss:Table></Worksheet>';
141
+ }
142
+ }
143
+
144
+ $xml .= '</Workbook>';
145
+
146
+ $this->setData($xml);
147
+
148
+ return $this;
149
+ }
150
+
151
+ /**
152
+ * Retrieve Excel 2003 XML Document header XML fragment
153
+ *
154
+ * @param string $sheetName the Worksheet name
155
+ * @return string
156
+ */
157
+ public function getHeaderXml($sheetName = '')
158
+ {
159
+ if (empty($sheetName)) {
160
+ $sheetName = 'Sheet 1';
161
+ }
162
+ $sheetName = htmlspecialchars($sheetName);
163
+ $xml = '<'.'?xml version="1.0"?'.'><'.'?mso-application progid="Excel.Sheet"?'
164
+ . '><Workbook'
165
+ . ' xmlns="urn:schemas-microsoft-com:office:spreadsheet"'
166
+ . ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
167
+ . ' xmlns:x="urn:schemas-microsoft-com:office:excel"'
168
+ . ' xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"'
169
+ . ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"'
170
+ . ' xmlns:o="urn:schemas-microsoft-com:office:office"'
171
+ . ' xmlns:html="http://www.w3.org/TR/REC-html40"'
172
+ . ' xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">'
173
+ . '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">'
174
+ . '</OfficeDocumentSettings>'
175
+ . '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">'
176
+ . '</ExcelWorkbook>'
177
+ . '<Worksheet ss:Name="' . $sheetName . '">'
178
+ . '<Table>';
179
+ return $xml;
180
+ }
181
+
182
+ /**
183
+ * Retrieve Excel 2003 XML Document footer XML fragment
184
+ *
185
+ * @return string
186
+ */
187
+ public function getFooterXml()
188
+ {
189
+ return '</Table></Worksheet></Workbook>';
190
+ }
191
+
192
+ /**
193
+ * Convert an array to Excel 2003 XML Document a Row XML fragment
194
+ *
195
+ * @param array $row
196
+ * @return string
197
+ */
198
+ public function getRowXml(array $row)
199
+ {
200
+ $xmlHeader = '<'.'?xml version="1.0"?'.'>' . "\n";
201
+ $xmlRegexp = '/^<cell><row>(.*)?<\/row><\/cell>\s?$/ms';
202
+
203
+ if (is_null($this->_xmlElement)) {
204
+ $xmlString = $xmlHeader . '<cell><row></row></cell>';
205
+ $this->_xmlElement = new SimpleXMLElement($xmlString, LIBXML_NOBLANKS);
206
+ }
207
+
208
+ $xmlData = array();
209
+ $xmlData[] = '<Row>';
210
+ foreach ($row as $value) {
211
+ $this->_xmlElement->row = htmlspecialchars($value);
212
+ $value = str_replace($xmlHeader, '', $this->_xmlElement->asXML());
213
+ $value = preg_replace($xmlRegexp, '\\1', $value);
214
+ $dataType = "String";
215
+ if (is_numeric($value)) {
216
+ $dataType = "Number";
217
+ }
218
+ $value = str_replace("\r\n", '&#10;', $value);
219
+ $value = str_replace("\r", '&#10;', $value);
220
+ $value = str_replace("\n", '&#10;', $value);
221
+
222
+ $xmlData[] = '<Cell><Data ss:Type="'.$dataType.'">'.$value.'</Data></Cell>';
223
+ }
224
+ $xmlData[] = '</Row>';
225
+
226
+ return join('', $xmlData);
227
+ }
228
+ }
lib/Varien/Convert/Profile.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert profile
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Profile extends Varien_Convert_Profile_Abstract
36
+ {
37
+
38
+ }
lib/Varien/Convert/Profile/Abstract.php ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert profile
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Profile_Abstract
36
+ {
37
+ protected $_actions;
38
+ protected $_containers;
39
+ protected $_exceptions = array();
40
+ protected $_dryRun;
41
+
42
+ protected $_actionDefaultClass = 'Varien_Convert_Action';
43
+ protected $_containerCollectionDefaultClass = 'Varien_Convert_Container_Collection';
44
+
45
+ public function addAction(Varien_Convert_Action_Interface $action=null)
46
+ {
47
+ if (is_null($action)) {
48
+ $action = new $this->_actionDefaultClass();
49
+ }
50
+ $this->_actions[] = $action;
51
+ $action->setProfile($this);
52
+ return $action;
53
+ }
54
+
55
+ public function setContainers(Varien_Convert_Container_Collection $containers)
56
+ {
57
+ $this->_containers = $containers;
58
+ return $this;
59
+ }
60
+
61
+ public function getContainers()
62
+ {
63
+ if (!$this->_containers) {
64
+ $this->_containers = new $this->_containerCollectionDefaultClass();
65
+ }
66
+ return $this->_containers;
67
+ }
68
+
69
+ public function getContainer($name=null)
70
+ {
71
+ if (is_null($name)) {
72
+ $name = '_default';
73
+ }
74
+ return $this->getContainers()->getItem($name);
75
+ }
76
+
77
+ public function addContainer($name, Varien_Convert_Container_Interface $container)
78
+ {
79
+ $container = $this->getContainers()->addItem($name, $container);
80
+ $container->setProfile($this);
81
+ return $container;
82
+ }
83
+
84
+ public function getExceptions()
85
+ {
86
+ return $this->_exceptions;
87
+ }
88
+
89
+ public function getDryRun()
90
+ {
91
+ return $this->_dryRun;
92
+ }
93
+
94
+ public function setDryRun($flag)
95
+ {
96
+ $this->_dryRun = $flag;
97
+ return $this;
98
+ }
99
+
100
+ public function addException(Varien_Convert_Exception $e)
101
+ {
102
+ $this->_exceptions[] = $e;
103
+ return $this;
104
+ }
105
+
106
+ public function run()
107
+ {
108
+ if (!$this->_actions) {
109
+ $e = new Varien_Convert_Exception("Could not find any actions for this profile");
110
+ $e->setLevel(Varien_Convert_Exception::FATAL);
111
+ $this->addException($e);
112
+ return;
113
+ }
114
+
115
+ foreach ($this->_actions as $action) {
116
+ $action->run();
117
+ }
118
+ return $this;
119
+ }
120
+ }
lib/Varien/Convert/Profile/Collection.php ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert profile collection
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Profile_Collection
36
+ {
37
+ protected $_xml;
38
+ protected $_containers;
39
+ protected $_profiles = array();
40
+
41
+ protected $_simplexmlDefaultClass = 'Varien_Simplexml_Element';
42
+ protected $_profileDefaultClass = 'Varien_Convert_Profile';
43
+ protected $_profileCollectionDefaultClass = 'Varien_Convert_Profile_Collection';
44
+ protected $_containerDefaultClass = 'Varien_Convert_Container_Generic';
45
+ protected $_containerCollectionDefaultClass = 'Varien_Convert_Container_Collection';
46
+
47
+ public function getContainers()
48
+ {
49
+ if (!$this->_containers) {
50
+ $this->_containers = new $this->_containerCollectionDefaultClass();
51
+ $this->_containers->setDefaultClass($this->_containerDefaultClass);
52
+ }
53
+ return $this->_containers;
54
+ }
55
+
56
+ public function getContainer($name)
57
+ {
58
+ return $this->getContainers()->getItem($name);
59
+ }
60
+
61
+
62
+ public function addContainer($name, Varien_Convert_Container_Interface $container)
63
+ {
64
+ $container = $this->getContainers()->addItem($name, $container);
65
+ return $container;
66
+ }
67
+
68
+ public function getProfiles()
69
+ {
70
+ return $this->_profiles;
71
+ }
72
+
73
+ public function getProfile($name)
74
+ {
75
+ if (!isset($this->_profiles[$name])) {
76
+ $this->importProfileXml($name);
77
+ }
78
+ return $this->_profiles[$name];
79
+ }
80
+
81
+ public function addProfile($name, Varien_Convert_Profile_Abstract $profile=null)
82
+ {
83
+ if (is_null($profile)) {
84
+ $profile = new $this->_profileDefaultClass();
85
+ }
86
+ $this->_profiles[$name] = $profile;
87
+ return $profile;
88
+ }
89
+
90
+ public function run($profile)
91
+ {
92
+ $this->getProfile($profile)->run();
93
+ return $this;
94
+ }
95
+
96
+ public function getClassNameByType($type)
97
+ {
98
+ return $type;
99
+ }
100
+
101
+ public function importXml($xml)
102
+ {
103
+ if (is_string($xml)) {
104
+ $xml = simplexml_load_string($xml, $this->_simplexmlDefaultClass);
105
+ }
106
+ if (!$xml instanceof SimpleXMLElement) {
107
+ return $this;
108
+ }
109
+ $this->_xml = $xml;
110
+
111
+ foreach ($xml->container as $containerNode) {
112
+ if (!$containerNode['name'] || !$containerNode['type']) {
113
+ continue;
114
+ }
115
+ $class = $this->getClassNameByType((string)$containerNode['type']);
116
+ $container = $this->addContainer((string)$containerNode['name'], new $class());
117
+ foreach ($containerNode->var as $varNode) {
118
+ $container->setVar((string)$varNode['name'], (string)$varNode);
119
+ }
120
+ }
121
+ return $this;
122
+ }
123
+
124
+ public function importProfileXml($name)
125
+ {
126
+ if (!$this->_xml) {
127
+ return $this;
128
+ }
129
+ $nodes = $this->_xml->xpath("//profile[@name='".$name."']");
130
+ if (!$nodes) {
131
+ return $this;
132
+ }
133
+ $profileNode = $nodes[0];
134
+
135
+ $profile = $this->addProfile($name);
136
+ $profile->setContainers($this->getContainers());
137
+ foreach ($profileNode->action as $actionNode) {
138
+ $action = $profile->addAction();
139
+ foreach ($actionNode->attributes() as $key=>$value) {
140
+ $action->setParam($key, (string)$value);
141
+ }
142
+
143
+ if ($actionNode['use']) {
144
+ $container = $profile->getContainer((string)$actionNode['use']);
145
+ } else {
146
+ $action->setParam('class', $this->getClassNameByType((string)$actionNode['type']));
147
+ $container = $action->getContainer();
148
+ }
149
+ $action->setContainer($container);
150
+ if ($action->getParam('name')) {
151
+ $this->addContainer($action->getParam('name'), $container);
152
+ }
153
+ foreach ($actionNode->var as $varNode) {
154
+ $container->setVar((string)$varNode['name'], (string)$varNode);
155
+ }
156
+ }
157
+
158
+ return $this;
159
+ }
160
+ }
lib/Varien/Convert/Validator/Abstract.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert validator abstract
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Convert_Validator_Abstract extends Varien_Convert_Container_Abstract implements Varien_Convert_Validator_Interface
36
+ {
37
+
38
+ }
lib/Varien/Convert/Validator/Column.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert column validator
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Convert_Validator_Column extends Varien_Convert_Validator_Abstract
36
+ {
37
+
38
+ }
lib/Varien/Convert/Validator/Dryrun.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert dry run validator
30
+ *
31
+ * Insert where you want to step profile execution if dry run flag is set
32
+ *
33
+ * @category Varien
34
+ * @package Varien_Convert
35
+ * @author Magento Core Team <core@magentocommerce.com>
36
+ */
37
+ class Varien_Convert_Validator_Dryrun extends Varien_Convert_Validator_Abstract
38
+ {
39
+ public function validate()
40
+ {
41
+ if ($this->getVar('dry_run') || $this->getProfile()->getDryRun()) {
42
+ $this->addException('Dry run set, stopping execution', Varien_Convert_Exceptin::FATAL);
43
+ }
44
+ return $this;
45
+ }
46
+ }
lib/Varien/Convert/Validator/Interface.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Convert validator interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Convert
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Convert_Validator_Interface
36
+ {
37
+ public function validate();
38
+ }
lib/Varien/Crypt.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Crypt
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Crypt factory
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Crypt
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Crypt
36
+ {
37
+ /**
38
+ * Factory method to return requested cipher logic
39
+ *
40
+ * @param string $method
41
+ * @return Varien_Crypt_Abstract
42
+ */
43
+ static public function factory($method='mcrypt')
44
+ {
45
+ $uc = str_replace(' ','_',ucwords(str_replace('_',' ',$method)));
46
+ $className = 'Varien_Crypt_'.$uc;
47
+ return new $className;
48
+ }
49
+ }
lib/Varien/Crypt/Abstract.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Crypt
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Abstract class for crypter classes
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Crypt
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Crypt_Abstract extends Varien_Object
36
+ {
37
+
38
+ }
lib/Varien/Crypt/Mcrypt.php ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Crypt
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Mcrypt plugin
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Crypt
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Crypt_Mcrypt extends Varien_Crypt_Abstract
36
+ {
37
+ /**
38
+ * Constuctor
39
+ *
40
+ * @param array $data
41
+ */
42
+ public function __construct(array $data=array())
43
+ {
44
+ parent::__construct($data);
45
+ }
46
+
47
+ /**
48
+ * Initialize mcrypt module
49
+ *
50
+ * @param string $key cipher private key
51
+ * @return Varien_Crypt_Mcrypt
52
+ */
53
+ public function init($key)
54
+ {
55
+ if (!$this->getCipher()) {
56
+ $this->setCipher(MCRYPT_BLOWFISH);
57
+ }
58
+
59
+ if (!$this->getMode()) {
60
+ $this->setMode(MCRYPT_MODE_ECB);
61
+ }
62
+
63
+ $this->setHandler(mcrypt_module_open($this->getCipher(), '', $this->getMode(), ''));
64
+
65
+ if (!$this->getInitVector()) {
66
+ if (MCRYPT_MODE_CBC == $this->getMode()) {
67
+ $this->setInitVector(substr(
68
+ md5(mcrypt_create_iv (mcrypt_enc_get_iv_size($this->getHandler()), MCRYPT_RAND)),
69
+ - mcrypt_enc_get_iv_size($this->getHandler())
70
+ ));
71
+ } else {
72
+ $this->setInitVector(mcrypt_create_iv (mcrypt_enc_get_iv_size($this->getHandler()), MCRYPT_RAND));
73
+ }
74
+ }
75
+
76
+ $maxKeySize = mcrypt_enc_get_key_size($this->getHandler());
77
+
78
+ if (strlen($key) > $maxKeySize) { // strlen() intentionally, to count bytes, rather than characters
79
+ $this->setHandler(null);
80
+ throw new Varien_Exception('Maximum key size must be smaller '.$maxKeySize);
81
+ }
82
+
83
+ mcrypt_generic_init($this->getHandler(), $key, $this->getInitVector());
84
+
85
+ return $this;
86
+ }
87
+
88
+ /**
89
+ * Encrypt data
90
+ *
91
+ * @param string $data source string
92
+ * @return string
93
+ */
94
+ public function encrypt($data)
95
+ {
96
+ if (!$this->getHandler()) {
97
+ throw new Varien_Exception('Crypt module is not initialized.');
98
+ }
99
+ if (strlen($data) == 0) {
100
+ return $data;
101
+ }
102
+ return mcrypt_generic($this->getHandler(), $data);
103
+ }
104
+
105
+ /**
106
+ * Decrypt data
107
+ *
108
+ * @param string $data encrypted string
109
+ * @return string
110
+ */
111
+ public function decrypt($data)
112
+ {
113
+ if (!$this->getHandler()) {
114
+ throw new Varien_Exception('Crypt module is not initialized.');
115
+ }
116
+ if (strlen($data) == 0) {
117
+ return $data;
118
+ }
119
+ return mdecrypt_generic($this->getHandler(), $data);
120
+ }
121
+
122
+ /**
123
+ * Desctruct cipher module
124
+ *
125
+ */
126
+ public function __destruct()
127
+ {
128
+ if ($this->getHandler()) {
129
+ $this->_reset();
130
+ }
131
+ }
132
+
133
+ protected function _reset()
134
+ {
135
+ mcrypt_generic_deinit($this->getHandler());
136
+ mcrypt_module_close($this->getHandler());
137
+ }
138
+ }
lib/Varien/Data/Collection.php ADDED
@@ -0,0 +1,805 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Data collection
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Collection implements IteratorAggregate, Countable
35
+ {
36
+ const SORT_ORDER_ASC = 'ASC';
37
+ const SORT_ORDER_DESC = 'DESC';
38
+
39
+ /**
40
+ * Collection items
41
+ *
42
+ * @var array
43
+ */
44
+ protected $_items = array();
45
+
46
+ /**
47
+ * Item object class name
48
+ *
49
+ * @var string
50
+ */
51
+ protected $_itemObjectClass = 'Varien_Object';
52
+
53
+ /**
54
+ * Order configuration
55
+ *
56
+ * @var array
57
+ */
58
+ protected $_orders = array();
59
+
60
+ /**
61
+ * Filters configuration
62
+ *
63
+ * @var array
64
+ */
65
+ protected $_filters = array();
66
+
67
+ /**
68
+ * Filter rendered flag
69
+ *
70
+ * @var bool
71
+ */
72
+ protected $_isFiltersRendered = false;
73
+
74
+ /**
75
+ * Current page number for items pager
76
+ *
77
+ * @var int
78
+ */
79
+ protected $_curPage = 1;
80
+
81
+ /**
82
+ * Pager page size
83
+ *
84
+ * if page size is false, then we works with all items
85
+ *
86
+ * @var int | false
87
+ */
88
+ protected $_pageSize = false;
89
+
90
+ /**
91
+ * Total items number
92
+ *
93
+ * @var int
94
+ */
95
+ protected $_totalRecords;
96
+
97
+ /**
98
+ * Loading state flag
99
+ *
100
+ * @var bool
101
+ */
102
+ protected $_isCollectionLoaded;
103
+
104
+ protected $_cacheKey;
105
+
106
+ protected $_cacheTags = array();
107
+
108
+ protected $_cacheLifetime = 86400;
109
+
110
+ /**
111
+ * Additional collection flags
112
+ *
113
+ * @var array
114
+ */
115
+ protected $_flags = array();
116
+
117
+ public function __construct()
118
+ {
119
+
120
+ }
121
+
122
+ /**
123
+ * Add collection filter
124
+ *
125
+ * @param string $field
126
+ * @param string $value
127
+ * @param string $type and|or|string
128
+ */
129
+ public function addFilter($field, $value, $type = 'and')
130
+ {
131
+ $filter = new Varien_Object(); // implements ArrayAccess
132
+ $filter['field'] = $field;
133
+ $filter['value'] = $value;
134
+ $filter['type'] = strtolower($type);
135
+
136
+ $this->_filters[] = $filter;
137
+ $this->_isFiltersRendered = false;
138
+ return $this;
139
+ }
140
+
141
+ /**
142
+ * Search for a filter by specified field
143
+ *
144
+ * Multiple filters can be matched if an array is specified:
145
+ * - 'foo' -- get the first filter with field name 'foo'
146
+ * - array('foo') -- get all filters with field name 'foo'
147
+ * - array('foo', 'bar') -- get all filters with field name 'foo' or 'bar'
148
+ * - array() -- get all filters
149
+ *
150
+ * @param string|array $field
151
+ * @return Varien_Object|array|null
152
+ */
153
+ public function getFilter($field)
154
+ {
155
+ if (is_array($field)) {
156
+ // empty array: get all filters
157
+ if (empty($field)) {
158
+ return $this->_filters;
159
+ }
160
+ // non-empty array: collect all filters that match specified field names
161
+ $result = array();
162
+ foreach ($this->_filters as $filter) {
163
+ if (in_array($filter['field'], $field)) {
164
+ $result[] = $filter;
165
+ }
166
+ }
167
+ return $result;
168
+ }
169
+
170
+ // get a first filter by specified name
171
+ foreach ($this->_filters as $filter) {
172
+ if ($filter['field'] === $field) {
173
+ return $filter;
174
+ }
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Retrieve collection loading status
180
+ *
181
+ * @return bool
182
+ */
183
+ public function isLoaded()
184
+ {
185
+ return $this->_isCollectionLoaded;
186
+ }
187
+
188
+ /**
189
+ * Set collection loading status flag
190
+ *
191
+ * @param unknown_type $flag
192
+ * @return unknown
193
+ */
194
+ protected function _setIsLoaded($flag = true)
195
+ {
196
+ $this->_isCollectionLoaded = $flag;
197
+ return $this;
198
+ }
199
+
200
+ /**
201
+ * Get current collection page
202
+ *
203
+ * @param int $displacement
204
+ * @return int
205
+ */
206
+ public function getCurPage($displacement = 0)
207
+ {
208
+ if ($this->_curPage + $displacement < 1) {
209
+ return 1;
210
+ }
211
+ elseif ($this->_curPage + $displacement > $this->getLastPageNumber()) {
212
+ return $this->getLastPageNumber();
213
+ } else {
214
+ return $this->_curPage + $displacement;
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Retrieve collection last page number
220
+ *
221
+ * @return int
222
+ */
223
+ public function getLastPageNumber()
224
+ {
225
+ $collectionSize = (int) $this->getSize();
226
+ if (0 === $collectionSize) {
227
+ return 1;
228
+ }
229
+ elseif($this->_pageSize) {
230
+ return ceil($collectionSize/$this->_pageSize);
231
+ }
232
+ else{
233
+ return 1;
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Retrieve collection page size
239
+ *
240
+ * @return int
241
+ */
242
+ public function getPageSize()
243
+ {
244
+ return $this->_pageSize;
245
+ }
246
+
247
+ /**
248
+ * Retrieve collection all items count
249
+ *
250
+ * @return int
251
+ */
252
+ public function getSize()
253
+ {
254
+ $this->load();
255
+ if (is_null($this->_totalRecords)) {
256
+ $this->_totalRecords = count($this->getItems());
257
+ }
258
+ return intval($this->_totalRecords);
259
+ }
260
+
261
+ /**
262
+ * Retrieve collection first item
263
+ *
264
+ * @return Varien_Object
265
+ */
266
+ public function getFirstItem()
267
+ {
268
+ $this->load();
269
+
270
+ if (count($this->_items)) {
271
+ reset($this->_items);
272
+ return current($this->_items);
273
+ }
274
+
275
+ return new $this->_itemObjectClass();
276
+ }
277
+
278
+ /**
279
+ * Retrieve collection last item
280
+ *
281
+ * @return Varien_Object
282
+ */
283
+ public function getLastItem()
284
+ {
285
+ $this->load();
286
+
287
+ if (count($this->_items)) {
288
+ return end($this->_items);
289
+ }
290
+
291
+ return new $this->_itemObjectClass();
292
+ }
293
+
294
+ /**
295
+ * Retrieve collection items
296
+ *
297
+ * @return array
298
+ */
299
+ public function getItems()
300
+ {
301
+ $this->load();
302
+ return $this->_items;
303
+ }
304
+
305
+ /**
306
+ * Retrieve field values from all items
307
+ *
308
+ * @param string $colName
309
+ * @return array
310
+ */
311
+ public function getColumnValues($colName)
312
+ {
313
+ $this->load();
314
+
315
+ $col = array();
316
+ foreach ($this->getItems() as $item) {
317
+ $col[] = $item->getData($colName);
318
+ }
319
+ return $col;
320
+ }
321
+
322
+ /**
323
+ * Search all items by field value
324
+ *
325
+ * @param string $column
326
+ * @param mixed $value
327
+ * @return array
328
+ */
329
+ public function getItemsByColumnValue($column, $value)
330
+ {
331
+ $this->load();
332
+
333
+ $res = array();
334
+ foreach ($this as $item) {
335
+ if ($item->getData($column)==$value) {
336
+ $res[] = $item;
337
+ }
338
+ }
339
+ return $res;
340
+ }
341
+
342
+ /**
343
+ * Search first item by field value
344
+ *
345
+ * @param string $column
346
+ * @param mixed $value
347
+ * @return Varien_Object || null
348
+ */
349
+ public function getItemByColumnValue($column, $value)
350
+ {
351
+ $this->load();
352
+
353
+ foreach ($this as $item) {
354
+ if ($item->getData($column)==$value) {
355
+ return $item;
356
+ }
357
+ }
358
+ return null;
359
+ }
360
+
361
+ /**
362
+ * Adding item to item array
363
+ *
364
+ * @param Varien_Object $item
365
+ * @return Varien_Data_Collection
366
+ */
367
+ public function addItem(Varien_Object $item)
368
+ {
369
+ $itemId = $this->_getItemId($item);
370
+
371
+ if (!is_null($itemId)) {
372
+ if (isset($this->_items[$itemId])) {
373
+ throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
374
+ }
375
+ $this->_items[$itemId] = $item;
376
+ } else {
377
+ $this->_items[] = $item;
378
+ }
379
+ return $this;
380
+ }
381
+
382
+ /**
383
+ * Retrieve item id
384
+ *
385
+ * @param Varien_Object $item
386
+ * @return mixed
387
+ */
388
+ protected function _getItemId(Varien_Object $item)
389
+ {
390
+ return $item->getId();
391
+ }
392
+
393
+ /**
394
+ * Retrieve ids of all tems
395
+ *
396
+ * @return array
397
+ */
398
+ public function getAllIds()
399
+ {
400
+ $ids = array();
401
+ foreach ($this->getItems() as $item) {
402
+ $ids[] = $this->_getItemId($item);
403
+ }
404
+ return $ids;
405
+ }
406
+
407
+ /**
408
+ * Remove item from collection by item key
409
+ *
410
+ * @param mixed $key
411
+ * @return Varien_Data_Collection
412
+ */
413
+ public function removeItemByKey($key)
414
+ {
415
+ if (isset($this->_items[$key])) {
416
+ unset($this->_items[$key]);
417
+ }
418
+ return $this;
419
+ }
420
+
421
+ /**
422
+ * Clear collection
423
+ *
424
+ * @return Varien_Data_Collection
425
+ */
426
+ public function clear()
427
+ {
428
+ $this->_setIsLoaded(false);
429
+ $this->_items = array();
430
+ return $this;
431
+ }
432
+
433
+ /**
434
+ * Walk through the collection and run model method or external callback
435
+ * with optional arguments
436
+ *
437
+ * Returns array with results of callback for each item
438
+ *
439
+ * @param string $method
440
+ * @param array $args
441
+ * @return array
442
+ */
443
+ public function walk($callback, array $args=array())
444
+ {
445
+ $results = array();
446
+ $useItemCallback = is_string($callback) && strpos($callback, '::')===false;
447
+ foreach ($this->getItems() as $id=>$item) {
448
+ if ($useItemCallback) {
449
+ $cb = array($item, $callback);
450
+ } else {
451
+ $cb = $callback;
452
+ array_unshift($args, $item);
453
+ }
454
+ $results[$id] = call_user_func_array($cb, $args);
455
+ }
456
+ return $results;
457
+ }
458
+
459
+ public function each($obj_method, $args=array())
460
+ {
461
+ foreach ($args->_items as $k => $item) {
462
+ $args->_items[$k] = call_user_func($obj_method, $item);
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Setting data for all collection items
468
+ *
469
+ * @param mixed $key
470
+ * @param mixed $value
471
+ * @return Varien_Data_Collection
472
+ */
473
+ public function setDataToAll($key, $value=null)
474
+ {
475
+ if (is_array($key)) {
476
+ foreach ($key as $k=>$v) {
477
+ $this->setDataToAll($k, $v);
478
+ }
479
+ return $this;
480
+ }
481
+ foreach ($this->getItems() as $item) {
482
+ $item->setData($key, $value);
483
+ }
484
+ return $this;
485
+ }
486
+
487
+ /**
488
+ * Set current page
489
+ *
490
+ * @param int $page
491
+ * @return Varien_Data_Collection
492
+ */
493
+ public function setCurPage($page)
494
+ {
495
+ $this->_curPage = $page;
496
+ return $this;
497
+ }
498
+
499
+ /**
500
+ * Set collection page size
501
+ *
502
+ * @param int $size
503
+ * @return Varien_Data_Collection
504
+ */
505
+ public function setPageSize($size)
506
+ {
507
+ $this->_pageSize = $size;
508
+ return $this;
509
+ }
510
+
511
+ /**
512
+ * Set select order
513
+ *
514
+ * @param string $field
515
+ * @param string $direction
516
+ * @return Varien_Data_Collection
517
+ */
518
+ public function setOrder($field, $direction = self::SORT_ORDER_DESC)
519
+ {
520
+ $this->_orders[$field] = $direction;
521
+ return $this;
522
+ }
523
+
524
+ /**
525
+ * Set collection item class name
526
+ *
527
+ * @param string $className
528
+ * @return Varien_Data_Collection
529
+ */
530
+ function setItemObjectClass($className)
531
+ {
532
+ $className = Mage::getConfig()->getModelClassName($className);
533
+ /**
534
+ * is_subclass_of($className, 'Varien_Object') - Segmentation fault in php 5.2.3
535
+ */
536
+ /*if (!is_subclass_of($className, 'Varien_Object')) {
537
+ throw new Exception($className.' does not extends from Varien_Object');
538
+ }*/
539
+ $this->_itemObjectClass = $className;
540
+ return $this;
541
+ }
542
+
543
+ /**
544
+ * Retrieve collection empty item
545
+ *
546
+ * @return Varien_Object
547
+ */
548
+ public function getNewEmptyItem()
549
+ {
550
+ return new $this->_itemObjectClass();
551
+ }
552
+
553
+ /**
554
+ * Render sql select conditions
555
+ *
556
+ * @return Varien_Data_Collection
557
+ */
558
+ protected function _renderFilters()
559
+ {
560
+ return $this;
561
+ }
562
+
563
+ /**
564
+ * Render sql select orders
565
+ *
566
+ * @return Varien_Data_Collection
567
+ */
568
+ protected function _renderOrders()
569
+ {
570
+ return $this;
571
+ }
572
+
573
+ /**
574
+ * Render sql select limit
575
+ *
576
+ * @return Varien_Data_Collection
577
+ */
578
+ protected function _renderLimit()
579
+ {
580
+ return $this;
581
+ }
582
+
583
+ /**
584
+ * Set select distinct
585
+ *
586
+ * @param bool $flag
587
+ */
588
+ public function distinct($flag)
589
+ {
590
+ return $this;
591
+ }
592
+
593
+ /**
594
+ * Load data
595
+ *
596
+ * @return Varien_Data_Collection
597
+ */
598
+ public function loadData($printQuery = false, $logQuery = false)
599
+ {
600
+ return $this;
601
+ }
602
+
603
+ /**
604
+ * Load data
605
+ *
606
+ * @return Varien_Data_Collection
607
+ */
608
+ public function load($printQuery = false, $logQuery = false)
609
+ {
610
+ return $this->loadData($printQuery, $logQuery);
611
+ }
612
+
613
+ /**
614
+ * Convert collection to XML
615
+ *
616
+ * @return string
617
+ */
618
+ public function toXml()
619
+ {
620
+ $xml = '<?xml version="1.0" encoding="UTF-8"?>
621
+ <collection>
622
+ <totalRecords>'.$this->_totalRecords.'</totalRecords>
623
+ <items>';
624
+
625
+ foreach ($this as $item) {
626
+ $xml.=$item->toXml();
627
+ }
628
+ $xml.= '</items>
629
+ </collection>';
630
+ return $xml;
631
+ }
632
+
633
+ /**
634
+ * Convert collection to array
635
+ *
636
+ * @return array
637
+ */
638
+ public function toArray($arrRequiredFields = array())
639
+ {
640
+ $arrItems = array();
641
+ $arrItems['totalRecords'] = $this->getSize();
642
+
643
+ $arrItems['items'] = array();
644
+ foreach ($this as $item) {
645
+ $arrItems['items'][] = $item->toArray($arrRequiredFields);
646
+ }
647
+ return $arrItems;
648
+ }
649
+
650
+ /**
651
+ * Convert items array to array for select options
652
+ *
653
+ * return items array
654
+ * array(
655
+ * $index => array(
656
+ * 'value' => mixed
657
+ * 'label' => mixed
658
+ * )
659
+ * )
660
+ *
661
+ * @param string $valueField
662
+ * @param string $labelField
663
+ * @return array
664
+ */
665
+ protected function _toOptionArray($valueField='id', $labelField='name', $additional=array())
666
+ {
667
+ $res = array();
668
+ $additional['value'] = $valueField;
669
+ $additional['label'] = $labelField;
670
+
671
+ foreach ($this as $item) {
672
+ foreach ($additional as $code => $field) {
673
+ $data[$code] = $item->getData($field);
674
+ }
675
+ $res[] = $data;
676
+ }
677
+ return $res;
678
+ }
679
+
680
+ public function toOptionArray()
681
+ {
682
+ return $this->_toOptionArray();
683
+ }
684
+
685
+ public function toOptionHash()
686
+ {
687
+ return $this->_toOptionHash();
688
+ }
689
+
690
+ /**
691
+ * Convert items array to hash for select options
692
+ *
693
+ * return items hash
694
+ * array($value => $label)
695
+ *
696
+ * @param string $valueField
697
+ * @param string $labelField
698
+ * @return array
699
+ */
700
+ protected function _toOptionHash($valueField='id', $labelField='name')
701
+ {
702
+ $res = array();
703
+ foreach ($this as $item) {
704
+ $res[$item->getData($valueField)] = $item->getData($labelField);
705
+ }
706
+ return $res;
707
+ }
708
+
709
+ /**
710
+ * Retrieve item by id
711
+ *
712
+ * @param mixed $idValue
713
+ * @return Varien_Object
714
+ */
715
+ public function getItemById($idValue)
716
+ {
717
+ $this->load();
718
+ if (isset($this->_items[$idValue])) {
719
+ return $this->_items[$idValue];
720
+ }
721
+ return null;
722
+ }
723
+
724
+ /**
725
+ * Implementation of IteratorAggregate::getIterator()
726
+ */
727
+ public function getIterator()
728
+ {
729
+ $this->load();
730
+ return new ArrayIterator($this->_items);
731
+ }
732
+
733
+ /**
734
+ * Retireve count of collection loaded items
735
+ *
736
+ * @return int
737
+ */
738
+ public function count()
739
+ {
740
+ $this->load();
741
+ return count($this->_items);
742
+ }
743
+
744
+ public function setCacheKey($key)
745
+ {
746
+ $this->_cacheKey = $key;
747
+ return $this;
748
+ }
749
+
750
+ public function getCacheKey()
751
+ {
752
+ return $this->_cacheKey;
753
+ }
754
+
755
+ public function setCacheTags($tags)
756
+ {
757
+ $this->_cacheTags = $tags;
758
+ return $this;
759
+ }
760
+
761
+ public function getCacheTags()
762
+ {
763
+ return $this->_cacheTags;
764
+ }
765
+
766
+ public function getCacheLifetime()
767
+ {
768
+ return $this->_cacheLifetime;
769
+ }
770
+
771
+ /**
772
+ * Retrieve Flag
773
+ *
774
+ * @param string $flag
775
+ * @return mixed
776
+ */
777
+ public function getFlag($flag)
778
+ {
779
+ return isset($this->_flags[$flag]) ? $this->_flags[$flag] : null;
780
+ }
781
+
782
+ /**
783
+ * Set Flag
784
+ *
785
+ * @param string $flag
786
+ * @param mixed $value
787
+ * @return Varien_Data_Collection
788
+ */
789
+ public function setFlag($flag, $value = null)
790
+ {
791
+ $this->_flags[$flag] = $value;
792
+ return $this;
793
+ }
794
+
795
+ /**
796
+ * Has Flag
797
+ *
798
+ * @param string $flag
799
+ * @return bool
800
+ */
801
+ public function hasFlag($flag)
802
+ {
803
+ return array_key_exists($flag, $this->_flags);
804
+ }
805
+ }
lib/Varien/Data/Collection/Db.php ADDED
@@ -0,0 +1,793 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Base items collection class
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Collection_Db extends Varien_Data_Collection
36
+ {
37
+ /**
38
+ * DB connection
39
+ *
40
+ * @var Zend_Db_Adapter_Abstract
41
+ */
42
+ protected $_conn;
43
+
44
+ /**
45
+ * Select oblect
46
+ *
47
+ * @var Zend_Db_Select
48
+ */
49
+ protected $_select;
50
+
51
+ /**
52
+ * Cache configuration array
53
+ *
54
+ * @var array
55
+ */
56
+ protected $_cacheConf = null;
57
+
58
+ /**
59
+ * Identifier fild name for collection items
60
+ *
61
+ * Can be used by collections with items without defined
62
+ *
63
+ * @var string
64
+ */
65
+ protected $_idFieldName;
66
+
67
+ /**
68
+ * List of binded variables for select
69
+ *
70
+ * @var array
71
+ */
72
+ protected $_bindParams = array();
73
+
74
+ /**
75
+ * All collection data array
76
+ * Used for getData method
77
+ *
78
+ * @var array
79
+ */
80
+ protected $_data = null;
81
+
82
+ /**
83
+ * Fields map for corellation names & real selected fields
84
+ *
85
+ * @var array
86
+ */
87
+ protected $_map = null;
88
+
89
+ /**
90
+ * Database's statement for fetch item one by one
91
+ *
92
+ * @var Zend_Db_Statement_Pdo
93
+ */
94
+ protected $_fetchStmt = null;
95
+
96
+ public function __construct($conn=null)
97
+ {
98
+ parent::__construct();
99
+ if (!is_null($conn)) {
100
+ $this->setConnection($conn);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Add variable to bind list
106
+ *
107
+ * @param string $name
108
+ * @param mixed $value
109
+ * @return Varien_Data_Collection_Db
110
+ */
111
+ public function addBindParam($name, $value)
112
+ {
113
+ $this->_bindParams[$name] = $value;
114
+ return $this;
115
+ }
116
+
117
+ /**
118
+ * Initialize collection cache
119
+ *
120
+ * @param $object
121
+ * @param string $idPrefix
122
+ * @param array $tags
123
+ * @return Varien_Data_Collection_Db
124
+ */
125
+ public function initCache($object, $idPrefix, $tags)
126
+ {
127
+ $this->_cacheConf = array(
128
+ 'object' => $object,
129
+ 'prefix' => $idPrefix,
130
+ 'tags' => $tags
131
+ );
132
+ return $this;
133
+ }
134
+
135
+ /**
136
+ * Specify collection objects id field name
137
+ *
138
+ * @param string $fieldName
139
+ * @return Varien_Data_Collection_Db
140
+ */
141
+ protected function _setIdFieldName($fieldName)
142
+ {
143
+ $this->_idFieldName = $fieldName;
144
+ return $this;
145
+ }
146
+
147
+ /**
148
+ * Id field name getter
149
+ *
150
+ * @return string
151
+ */
152
+ public function getIdFieldName()
153
+ {
154
+ return $this->_idFieldName;
155
+ }
156
+
157
+ /**
158
+ * Get collection item identifier
159
+ *
160
+ * @param Varien_Object $item
161
+ * @return mixed
162
+ */
163
+ protected function _getItemId(Varien_Object $item)
164
+ {
165
+ if ($field = $this->getIdFieldName()) {
166
+ return $item->getData($field);
167
+ }
168
+ return parent::_getItemId($item);
169
+ }
170
+
171
+ /**
172
+ * Set database connection adapter
173
+ *
174
+ * @param Zend_Db_Adapter_Abstract $conn
175
+ * @return Varien_Data_Collection_Db
176
+ */
177
+ public function setConnection($conn)
178
+ {
179
+ if (!$conn instanceof Zend_Db_Adapter_Abstract) {
180
+ throw new Zend_Exception('dbModel read resource does not implement Zend_Db_Adapter_Abstract');
181
+ }
182
+
183
+ $this->_conn = $conn;
184
+ $this->_select = $this->_conn->select();
185
+ return $this;
186
+ }
187
+
188
+ /**
189
+ * Get Zend_Db_Select instance
190
+ *
191
+ * @return Varien_Db_Select
192
+ */
193
+ public function getSelect()
194
+ {
195
+ return $this->_select;
196
+ }
197
+
198
+ /**
199
+ * Retrieve connection object
200
+ *
201
+ * @return Varien_Db_Adapter_Interface
202
+ */
203
+ public function getConnection()
204
+ {
205
+ return $this->_conn;
206
+ }
207
+
208
+ /**
209
+ * Get collection size
210
+ *
211
+ * @return int
212
+ */
213
+ public function getSize()
214
+ {
215
+ if (is_null($this->_totalRecords)) {
216
+ $sql = $this->getSelectCountSql();
217
+ $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
218
+ }
219
+ return intval($this->_totalRecords);
220
+ }
221
+
222
+ /**
223
+ * Get SQL for get record count
224
+ *
225
+ * @return Varien_Db_Select
226
+ */
227
+ public function getSelectCountSql()
228
+ {
229
+ $this->_renderFilters();
230
+
231
+ $countSelect = clone $this->getSelect();
232
+ $countSelect->reset(Zend_Db_Select::ORDER);
233
+ $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
234
+ $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
235
+ $countSelect->reset(Zend_Db_Select::COLUMNS);
236
+
237
+ $countSelect->columns('COUNT(*)');
238
+
239
+ return $countSelect;
240
+ }
241
+
242
+ /**
243
+ * Get sql select string or object
244
+ *
245
+ * @param bool $stringMode
246
+ * @return string || Zend_Db_Select
247
+ */
248
+ function getSelectSql($stringMode = false)
249
+ {
250
+ if ($stringMode) {
251
+ return $this->_select->__toString();
252
+ }
253
+ return $this->_select;
254
+ }
255
+
256
+ /**
257
+ * Add select order
258
+ *
259
+ * @param string $field
260
+ * @param string $direction
261
+ * @return Varien_Data_Collection_Db
262
+ */
263
+ public function setOrder($field, $direction = self::SORT_ORDER_DESC)
264
+ {
265
+ return $this->_setOrder($field, $direction);
266
+ }
267
+
268
+ /**
269
+ * self::setOrder() alias
270
+ *
271
+ * @param string $field
272
+ * @param string $direction
273
+ * @return Varien_Data_Collection_Db
274
+ */
275
+ public function addOrder($field, $direction = self::SORT_ORDER_DESC)
276
+ {
277
+ return $this->_setOrder($field, $direction);
278
+ }
279
+
280
+ /**
281
+ * Add select order to the beginning
282
+ *
283
+ * @param string $field
284
+ * @param string $direction
285
+ * @return Varien_Data_Collection_Db
286
+ */
287
+ public function unshiftOrder($field, $direction = self::SORT_ORDER_DESC)
288
+ {
289
+ return $this->_setOrder($field, $direction, true);
290
+ }
291
+
292
+ /**
293
+ * Add ORDERBY to the end or to the beginning
294
+ *
295
+ * @param string $field
296
+ * @param string $direction
297
+ * @param bool $unshift
298
+ * @return Varien_Data_Collection_Db
299
+ */
300
+ private function _setOrder($field, $direction, $unshift = false)
301
+ {
302
+ $field = (string)$this->_getMappedField($field);
303
+ $direction = (strtoupper($direction) == self::SORT_ORDER_ASC) ? self::SORT_ORDER_ASC : self::SORT_ORDER_DESC;
304
+ // emulate associative unshift
305
+ if ($unshift) {
306
+ $orders = array($field => new Zend_Db_Expr($field . ' ' . $direction));
307
+ foreach ($this->_orders as $key => $expression) {
308
+ if (!isset($orders[$key])) {
309
+ $orders[$key] = $expression;
310
+ }
311
+ }
312
+ $this->_orders = $orders;
313
+ }
314
+ else {
315
+ $this->_orders[$field] = new Zend_Db_Expr($field . ' ' . $direction);
316
+ }
317
+ return $this;
318
+ }
319
+
320
+ /**
321
+ * Render sql select conditions
322
+ *
323
+ * @return Varien_Data_Collection_Db
324
+ */
325
+ protected function _renderFilters()
326
+ {
327
+ if ($this->_isFiltersRendered) {
328
+ return $this;
329
+ }
330
+
331
+ $this->_renderFiltersBefore();
332
+
333
+ foreach ($this->_filters as $filter) {
334
+ switch ($filter['type']) {
335
+ case 'or' :
336
+ $condition = $this->_conn->quoteInto($filter['field'].'=?', $filter['value']);
337
+ $this->_select->orWhere($condition);
338
+ break;
339
+ case 'string' :
340
+ $this->_select->where($filter['value']);
341
+ break;
342
+ case 'public':
343
+ $field = $this->_getMappedField($filter['field']);
344
+ $condition = $filter['value'];
345
+ $this->_select->where(
346
+ $this->_getConditionSql($field, $condition), null, Varien_Db_Select::TYPE_CONDITION
347
+ );
348
+ break;
349
+ default:
350
+ $condition = $this->_conn->quoteInto($filter['field'].'=?', $filter['value']);
351
+ $this->_select->where($condition);
352
+ }
353
+ }
354
+ $this->_isFiltersRendered = true;
355
+ return $this;
356
+ }
357
+
358
+ /**
359
+ * Hook for operations before rendering filters
360
+ */
361
+ protected function _renderFiltersBefore()
362
+ {
363
+ }
364
+
365
+ /**
366
+ * Add field filter to collection
367
+ *
368
+ * @see self::_getConditionSql for $condition
369
+ * @param string $field
370
+ * @param null|string|array $condition
371
+ * @return Mage_Eav_Model_Entity_Collection_Abstract
372
+ */
373
+ public function addFieldToFilter($field, $condition=null)
374
+ {
375
+ $field = $this->_getMappedField($field);
376
+ $this->_select->where($this->_getConditionSql($field, $condition), null, Varien_Db_Select::TYPE_CONDITION);
377
+ return $this;
378
+ }
379
+
380
+ /**
381
+ * Try to get mapped field name for filter to collection
382
+ *
383
+ * @param string
384
+ * @return string
385
+ */
386
+ protected function _getMappedField($field)
387
+ {
388
+ $mappedFiled = $field;
389
+
390
+ $mapper = $this->_getMapper();
391
+
392
+ if (isset($mapper['fields'][$field])) {
393
+ $mappedFiled = $mapper['fields'][$field];
394
+ }
395
+
396
+ return $mappedFiled;
397
+ }
398
+
399
+ protected function _getMapper()
400
+ {
401
+ if (isset($this->_map)) {
402
+ return $this->_map;
403
+ }
404
+ else {
405
+ return false;
406
+ }
407
+ }
408
+
409
+ /**
410
+ * Build SQL statement for condition
411
+ *
412
+ * If $condition integer or string - exact value will be filtered ('eq' condition)
413
+ *
414
+ * If $condition is array - one of the following structures is expected:
415
+ * - array("from" => $fromValue, "to" => $toValue)
416
+ * - array("eq" => $equalValue)
417
+ * - array("neq" => $notEqualValue)
418
+ * - array("like" => $likeValue)
419
+ * - array("in" => array($inValues))
420
+ * - array("nin" => array($notInValues))
421
+ * - array("notnull" => $valueIsNotNull)
422
+ * - array("null" => $valueIsNull)
423
+ * - array("moreq" => $moreOrEqualValue)
424
+ * - array("gt" => $greaterValue)
425
+ * - array("lt" => $lessValue)
426
+ * - array("gteq" => $greaterOrEqualValue)
427
+ * - array("lteq" => $lessOrEqualValue)
428
+ * - array("finset" => $valueInSet)
429
+ * - array("regexp" => $regularExpression)
430
+ * - array("seq" => $stringValue)
431
+ * - array("sneq" => $stringValue)
432
+ *
433
+ * If non matched - sequential array is expected and OR conditions
434
+ * will be built using above mentioned structure
435
+ *
436
+ * @param string $fieldName
437
+ * @param integer|string|array $condition
438
+ * @return string
439
+ */
440
+ protected function _getConditionSql($fieldName, $condition)
441
+ {
442
+ return $this->getConnection()->prepareSqlCondition($fieldName, $condition);
443
+ }
444
+
445
+ protected function _getConditionFieldName($fieldName)
446
+ {
447
+ return $fieldName;
448
+ }
449
+
450
+ /**
451
+ * Render sql select orders
452
+ *
453
+ * @return Varien_Data_Collection_Db
454
+ */
455
+ protected function _renderOrders()
456
+ {
457
+ $ordersInSelect = $this->_select->getPart(Zend_Db_Select::ORDER);
458
+
459
+ foreach ($this->_orders as $orderExpr) {
460
+ if (!in_array($orderExpr, $ordersInSelect)) {
461
+ $this->_select->order($orderExpr);
462
+ }
463
+ }
464
+
465
+ return $this;
466
+ }
467
+
468
+ /**
469
+ * Render sql select limit
470
+ *
471
+ * @return Varien_Data_Collection_Db
472
+ */
473
+ protected function _renderLimit()
474
+ {
475
+ if($this->_pageSize){
476
+ $this->_select->limitPage($this->getCurPage(), $this->_pageSize);
477
+ }
478
+
479
+ return $this;
480
+ }
481
+
482
+ /**
483
+ * Set select distinct
484
+ *
485
+ * @param bool $flag
486
+ */
487
+ public function distinct($flag)
488
+ {
489
+ $this->_select->distinct($flag);
490
+ return $this;
491
+ }
492
+
493
+ /**
494
+ * Before load action
495
+ *
496
+ * @return Varien_Data_Collection_Db
497
+ */
498
+ protected function _beforeLoad()
499
+ {
500
+ return $this;
501
+ }
502
+
503
+ /**
504
+ * Load data
505
+ *
506
+ * @return Varien_Data_Collection_Db
507
+ */
508
+ public function load($printQuery = false, $logQuery = false)
509
+ {
510
+ if ($this->isLoaded()) {
511
+ return $this;
512
+ }
513
+
514
+ $this->_beforeLoad();
515
+
516
+ $this->_renderFilters()
517
+ ->_renderOrders()
518
+ ->_renderLimit();
519
+
520
+ $this->printLogQuery($printQuery, $logQuery);
521
+ $data = $this->getData();
522
+ $this->resetData();
523
+
524
+ if (is_array($data)) {
525
+ foreach ($data as $row) {
526
+ $item = $this->getNewEmptyItem();
527
+ if ($this->getIdFieldName()) {
528
+ $item->setIdFieldName($this->getIdFieldName());
529
+ }
530
+ $item->addData($row);
531
+ $this->addItem($item);
532
+ }
533
+ }
534
+
535
+ $this->_setIsLoaded();
536
+ $this->_afterLoad();
537
+ return $this;
538
+ }
539
+
540
+ /**
541
+ * Returns a collection item that corresponds to the fetched row
542
+ * and moves the internal data pointer ahead
543
+ *
544
+ * return Varien_Object|bool
545
+ */
546
+ public function fetchItem()
547
+ {
548
+ if (null === $this->_fetchStmt) {
549
+ $this->_fetchStmt = $this->getConnection()
550
+ ->query($this->getSelect());
551
+ }
552
+ $data = $this->_fetchStmt->fetch();
553
+ if (!empty($data) && is_array($data)) {
554
+ $item = $this->getNewEmptyItem();
555
+ if ($this->getIdFieldName()) {
556
+ $item->setIdFieldName($this->getIdFieldName());
557
+ }
558
+ $item->setData($data);
559
+
560
+ return $item;
561
+ }
562
+ return false;
563
+ }
564
+
565
+ /**
566
+ * Convert items array to hash for select options
567
+ * unsing fetchItem method
568
+ *
569
+ * The difference between _toOptionHash() and this one is that this
570
+ * method fetch items one by one and does not load all collection items at once
571
+ * return items hash
572
+ * array($value => $label)
573
+ *
574
+ * @see fetchItem()
575
+ *
576
+ * @param string $valueField
577
+ * @param string $labelField
578
+ * @return array
579
+ */
580
+ protected function _toOptionHashOptimized($valueField='id', $labelField='name')
581
+ {
582
+ $result = array();
583
+ while ($item = $this->fetchItem()) {
584
+ $result[$item->getData($valueField)] = $item->getData($labelField);
585
+ }
586
+ return $result;
587
+ }
588
+
589
+ /**
590
+ * Get all data array for collection
591
+ *
592
+ * @return array
593
+ */
594
+ public function getData()
595
+ {
596
+ if ($this->_data === null) {
597
+ $this->_renderFilters()
598
+ ->_renderOrders()
599
+ ->_renderLimit();
600
+ $this->_data = $this->_fetchAll($this->_select);
601
+ $this->_afterLoadData();
602
+ }
603
+ return $this->_data;
604
+ }
605
+
606
+ /**
607
+ * Proces loaded collection data
608
+ *
609
+ * @return Varien_Data_Collection_Db
610
+ */
611
+ protected function _afterLoadData()
612
+ {
613
+ return $this;
614
+ }
615
+
616
+ /**
617
+ * Reset loaded for collection data array
618
+ *
619
+ * @return Varien_Data_Collection_Db
620
+ */
621
+ public function resetData()
622
+ {
623
+ $this->_data = null;
624
+ return $this;
625
+ }
626
+
627
+ protected function _afterLoad()
628
+ {
629
+ return $this;
630
+ }
631
+
632
+ public function loadData($printQuery = false, $logQuery = false)
633
+ {
634
+ return $this->load($printQuery, $logQuery);
635
+ }
636
+
637
+ /**
638
+ * Print and/or log query
639
+ *
640
+ * @param boolean $printQuery
641
+ * @param boolean $logQuery
642
+ * @return Varien_Data_Collection_Db
643
+ */
644
+ public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {
645
+ if ($printQuery) {
646
+ echo is_null($sql) ? $this->getSelect()->__toString() : $sql;
647
+ }
648
+
649
+ if ($logQuery){
650
+ Mage::log(is_null($sql) ? $this->getSelect()->__toString() : $sql);
651
+ }
652
+ return $this;
653
+ }
654
+
655
+ /**
656
+ * Reset collection
657
+ *
658
+ * @return Varien_Data_Collection_Db
659
+ */
660
+ protected function _reset()
661
+ {
662
+ $this->getSelect()->reset();
663
+ $this->_initSelect();
664
+ $this->_setIsLoaded(false);
665
+ $this->_items = array();
666
+ $this->_data = null;
667
+ return $this;
668
+ }
669
+
670
+ /**
671
+ * Fetch collection data
672
+ *
673
+ * @param Zend_Db_Select $select
674
+ * @return array
675
+ */
676
+ protected function _fetchAll($select)
677
+ {
678
+ if ($this->_canUseCache()) {
679
+ $data = $this->_loadCache($select);
680
+ if ($data) {
681
+ $data = unserialize($data);
682
+ } else {
683
+ $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
684
+ $this->_saveCache($data, $select);
685
+ }
686
+ } else {
687
+ $data = $this->getConnection()->fetchAll($select, $this->_bindParams);
688
+ }
689
+ return $data;
690
+ }
691
+
692
+ /**
693
+ * Load cached data for select
694
+ *
695
+ * @param Zend_Db_Select $select
696
+ * @return string | false
697
+ */
698
+ protected function _loadCache($select)
699
+ {
700
+ $data = false;
701
+ $object = $this->_getCacheInstance();
702
+ if ($object) {
703
+ $data = $object->load($this->_getSelectCacheId($select));
704
+ }
705
+ return $data;
706
+ }
707
+
708
+ /**
709
+ * Save collection data to cache
710
+ *
711
+ * @param array $data
712
+ * @param Zend_Db_Select $select
713
+ * @return unknown_type
714
+ */
715
+ protected function _saveCache($data, $select)
716
+ {
717
+ $object = $this->_getCacheInstance();
718
+ $object->save(serialize($data), $this->_getSelectCacheId($select), $this->_getCacheTags());
719
+ return $this;
720
+ }
721
+
722
+ /**
723
+ * Check if cache can be used for collection data
724
+ *
725
+ * @return bool
726
+ */
727
+ protected function _canUseCache()
728
+ {
729
+ return $this->_getCacheInstance();
730
+ }
731
+
732
+ /**
733
+ * Get cache identifier base on select
734
+ *
735
+ * @param Zend_Db_Select|string $select
736
+ * @return string
737
+ */
738
+ protected function _getSelectCacheId($select)
739
+ {
740
+ $id = md5((string)$select);
741
+ if (isset($this->_cacheConf['prefix'])) {
742
+ $id = $this->_cacheConf['prefix'].'_'.$id;
743
+ }
744
+ return $id;
745
+ }
746
+
747
+ /**
748
+ * Retrieve cache instance
749
+ *
750
+ * @return Zend_Cache_Core
751
+ */
752
+ protected function _getCacheInstance()
753
+ {
754
+ if (isset($this->_cacheConf['object'])) {
755
+ return $this->_cacheConf['object'];
756
+ }
757
+ return false;
758
+ }
759
+
760
+ /**
761
+ * Get cache tags list
762
+ *
763
+ * @return array
764
+ */
765
+ protected function _getCacheTags()
766
+ {
767
+ if (isset($this->_cacheConf['tags'])) {
768
+ return $this->_cacheConf['tags'];
769
+ }
770
+ return array();
771
+ }
772
+
773
+ /**
774
+ * Add filter to Map
775
+ *
776
+ * @param string $filter
777
+ * @param string $alias
778
+ * @param string $group default 'fields'
779
+ *
780
+ * @return Varien_Data_Collection_Db
781
+ */
782
+ public function addFilterToMap($filter, $alias, $group = 'fields')
783
+ {
784
+ if (is_null($this->_map)) {
785
+ $this->_map = array($group => array());
786
+ } else if(is_null($this->_map[$group])) {
787
+ $this->_map[$group] = array();
788
+ }
789
+ $this->_map[$group][$filter] = $alias;
790
+
791
+ return $this;
792
+ }
793
+ }
lib/Varien/Data/Collection/Filesystem.php ADDED
@@ -0,0 +1,701 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Filesystem items collection
29
+ *
30
+ * Can scan a folder for files and/or folders recursively.
31
+ * Creates Varien_Object instance for each item, with its filename and base name
32
+ *
33
+ * Supports regexp masks that are applied to files and folders base names.
34
+ * These masks apply before adding items to collection, during filesystem scanning
35
+ *
36
+ * Supports dirsFirst feature, that will make directories be before files, regardless of sorting column.
37
+ *
38
+ * Supports some fancy filters.
39
+ *
40
+ * At least one target directory must be set
41
+ */
42
+ class Varien_Data_Collection_Filesystem extends Varien_Data_Collection
43
+ {
44
+ /**
45
+ * Target directory
46
+ *
47
+ * @var string
48
+ */
49
+ protected $_targetDirs = array();
50
+
51
+ /**
52
+ * Whether to collect files
53
+ *
54
+ * @var bool
55
+ */
56
+ protected $_collectFiles = true;
57
+
58
+ /**
59
+ * Whether to collect directories before files
60
+ *
61
+ * @var bool
62
+ */
63
+ protected $_dirsFirst = true;
64
+
65
+ /**
66
+ * Whether to collect recursively
67
+ *
68
+ * @var bool
69
+ */
70
+ protected $_collectRecursively = true;
71
+
72
+ /**
73
+ * Whether to collect dirs
74
+ *
75
+ * @var bool
76
+ */
77
+ protected $_collectDirs = false;
78
+
79
+ /**
80
+ * Directory names regex pre-filter
81
+ *
82
+ * @var string
83
+ */
84
+ protected $_allowedDirsMask = '/^[a-z0-9\.\-\_]+$/i';
85
+
86
+ /**
87
+ * Filenames regex pre-filter
88
+ *
89
+ * @var string
90
+ */
91
+ protected $_allowedFilesMask = '/^[a-z0-9\.\-\_]+\.[a-z0-9]+$/i';
92
+
93
+ /**
94
+ * Disallowed filenames regex pre-filter match for better versatility
95
+ *
96
+ * @var string
97
+ */
98
+ protected $_disallowedFilesMask = '';
99
+
100
+ /**
101
+ * Filter rendering helper variables
102
+ *
103
+ * @see Varien_Data_Collection::$_filter
104
+ * @see Varien_Data_Collection::$_isFiltersRendered
105
+ */
106
+ private $_filterIncrement = 0;
107
+ private $_filterBrackets = array();
108
+ private $_filterEvalRendered = '';
109
+
110
+ /**
111
+ * Collecting items helper variables
112
+ *
113
+ * @var array
114
+ */
115
+ protected $_collectedDirs = array();
116
+ protected $_collectedFiles = array();
117
+
118
+ /**
119
+ * Allowed dirs mask setter
120
+ * Set empty to not filter
121
+ *
122
+ * @param string $regex
123
+ * @return Varien_Data_Collection_Files
124
+ */
125
+ public function setDirsFilter($regex)
126
+ {
127
+ $this->_allowedDirsMask = (string)$regex;
128
+ return $this;
129
+ }
130
+
131
+ /**
132
+ * Allowed files mask setter
133
+ * Set empty to not filter
134
+ *
135
+ * @param string $regex
136
+ * @return Varien_Data_Collection_Files
137
+ */
138
+ public function setFilesFilter($regex)
139
+ {
140
+ $this->_allowedFilesMask = (string)$regex;
141
+ return $this;
142
+ }
143
+
144
+ /**
145
+ * Disallowed files mask setter
146
+ * Set empty value to not use this filter
147
+ *
148
+ * @param string $regex
149
+ * @return Varien_Data_Collection_Files
150
+ */
151
+ public function setDisallowedFilesFilter($regex)
152
+ {
153
+ $this->_disallowedFilesMask = (string)$regex;
154
+ return $this;
155
+ }
156
+
157
+ /**
158
+ * Set whether to collect dirs
159
+ *
160
+ * @param bool $value
161
+ * @return Varien_Data_Collection_Filesystem
162
+ */
163
+ public function setCollectDirs($value)
164
+ {
165
+ $this->_collectDirs = (bool)$value;
166
+ return $this;
167
+ }
168
+
169
+ /**
170
+ * Set whether to collect files
171
+ *
172
+ * @param bool $value
173
+ * @return Varien_Data_Collection_Filesystem
174
+ */
175
+ public function setCollectFiles($value)
176
+ {
177
+ $this->_collectFiles = (bool)$value;
178
+ return $this;
179
+ }
180
+
181
+ /**
182
+ * Set whether to collect recursively
183
+ *
184
+ * @param bool $value
185
+ * @return Varien_Data_Collection_Filesystem
186
+ */
187
+ public function setCollectRecursively($value)
188
+ {
189
+ $this->_collectRecursively = (bool)$value;
190
+ return $this;
191
+ }
192
+
193
+ /**
194
+ * Target directory setter. Adds directory to be scanned
195
+ *
196
+ * @param string $value
197
+ * @return Varien_Data_Collection_Filesystem
198
+ */
199
+ public function addTargetDir($value)
200
+ {
201
+ $value = (string)$value;
202
+ if (!is_dir($value)) {
203
+ throw new Exception('Unable to set target directory.');
204
+ }
205
+ $this->_targetDirs[$value] = $value;
206
+ return $this;
207
+ }
208
+
209
+ /**
210
+ * Set whether to collect directories before files
211
+ * Works *before* sorting.
212
+ *
213
+ * @param bool $value
214
+ * @return Varien_Data_Collection_Filesystem
215
+ */
216
+ public function setDirsFirst($value)
217
+ {
218
+ $this->_dirsFirst = (bool)$value;
219
+ return $this;
220
+ }
221
+
222
+ /**
223
+ * Get files from specified directory recursively (if needed)
224
+ *
225
+ * @param string|array $dir
226
+ */
227
+ protected function _collectRecursive($dir)
228
+ {
229
+ $collectedResult = array();
230
+ if (!is_array($dir)) {
231
+ $dir = array($dir);
232
+ }
233
+ foreach ($dir as $folder) {
234
+ if ($nodes = glob($folder . DIRECTORY_SEPARATOR . '*')) {
235
+ foreach ($nodes as $node) {
236
+ $collectedResult[] = $node;
237
+ }
238
+ }
239
+ }
240
+ if (empty($collectedResult)) {
241
+ return;
242
+ }
243
+
244
+ foreach ($collectedResult as $item) {
245
+ if (is_dir($item) && (!$this->_allowedDirsMask || preg_match($this->_allowedDirsMask, basename($item)))) {
246
+ if ($this->_collectDirs) {
247
+ if ($this->_dirsFirst) {
248
+ $this->_collectedDirs[] = $item;
249
+ }
250
+ else {
251
+ $this->_collectedFiles[] = $item;
252
+ }
253
+ }
254
+ if ($this->_collectRecursively) {
255
+ $this->_collectRecursive($item);
256
+ }
257
+ }
258
+ elseif ($this->_collectFiles && is_file($item)
259
+ && (!$this->_allowedFilesMask || preg_match($this->_allowedFilesMask, basename($item)))
260
+ && (!$this->_disallowedFilesMask || !preg_match($this->_disallowedFilesMask, basename($item)))) {
261
+ $this->_collectedFiles[] = $item;
262
+ }
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Lauch data collecting
268
+ *
269
+ * @param bool $printQuery
270
+ * @param bool $logQuery
271
+ * @return Varien_Data_Collection_Filesystem
272
+ */
273
+ public function loadData($printQuery = false, $logQuery = false)
274
+ {
275
+ if ($this->isLoaded()) {
276
+ return $this;
277
+ }
278
+ if (empty($this->_targetDirs)) {
279
+ throw new Exception('Please specify at least one target directory.');
280
+ }
281
+
282
+ $this->_collectedFiles = array();
283
+ $this->_collectedDirs = array();
284
+ $this->_collectRecursive($this->_targetDirs);
285
+ $this->_generateAndFilterAndSort('_collectedFiles');
286
+ if ($this->_dirsFirst) {
287
+ $this->_generateAndFilterAndSort('_collectedDirs');
288
+ $this->_collectedFiles = array_merge($this->_collectedDirs, $this->_collectedFiles);
289
+ }
290
+
291
+ // calculate totals
292
+ $this->_totalRecords = count($this->_collectedFiles);
293
+ $this->_setIsLoaded();
294
+
295
+ // paginate and add items
296
+ $from = ($this->getCurPage() - 1) * $this->getPageSize();
297
+ $to = $from + $this->getPageSize() - 1;
298
+ $isPaginated = $this->getPageSize() > 0;
299
+
300
+ $cnt = 0;
301
+ foreach ($this->_collectedFiles as $row) {
302
+ $cnt++;
303
+ if ($isPaginated && ($cnt < $from || $cnt > $to)) {
304
+ continue;
305
+ }
306
+ $item = new $this->_itemObjectClass();
307
+ $this->addItem($item->addData($row));
308
+ if (!$item->hasId()) {
309
+ $item->setId($cnt);
310
+ }
311
+ }
312
+
313
+ return $this;
314
+ }
315
+
316
+ /**
317
+ * With specified collected items:
318
+ * - generate data
319
+ * - apply filters
320
+ * - sort
321
+ *
322
+ * @param string $attributeName '_collectedFiles' | '_collectedDirs'
323
+ */
324
+ private function _generateAndFilterAndSort($attributeName)
325
+ {
326
+ // generate custom data (as rows with columns) basing on the filenames
327
+ foreach ($this->$attributeName as $key => $filename) {
328
+ $this->{$attributeName}[$key] = $this->_generateRow($filename);
329
+ }
330
+
331
+ // apply filters on generated data
332
+ if (!empty($this->_filters)) {
333
+ foreach ($this->$attributeName as $key => $row) {
334
+ if (!$this->_filterRow($row)) {
335
+ unset($this->{$attributeName}[$key]);
336
+ }
337
+ }
338
+ }
339
+
340
+ // sort (keys are lost!)
341
+ if (!empty($this->_orders)) {
342
+ usort($this->$attributeName, array($this, '_usort'));
343
+ }
344
+ }
345
+
346
+ /**
347
+ * Callback for sorting items
348
+ * Currently supports only sorting by one column
349
+ *
350
+ * @param array $a
351
+ * @param array $b
352
+ * @return int
353
+ */
354
+ protected function _usort($a, $b)
355
+ {
356
+ foreach ($this->_orders as $key => $direction) {
357
+ $result = $a[$key] > $b[$key] ? 1 : ($a[$key] < $b[$key] ? -1 : 0);
358
+ return (self::SORT_ORDER_ASC === strtoupper($direction) ? $result : -$result);
359
+ break;
360
+ }
361
+ }
362
+
363
+ /**
364
+ * Set select order
365
+ * Currently supports only sorting by one column
366
+ *
367
+ * @param string $field
368
+ * @param string $direction
369
+ * @return Varien_Data_Collection
370
+ */
371
+ public function setOrder($field, $direction = self::SORT_ORDER_DESC)
372
+ {
373
+ $this->_orders = array($field => $direction);
374
+ return $this;
375
+ }
376
+
377
+ /**
378
+ * Generate item row basing on the filename
379
+ *
380
+ * @param string $filename
381
+ * @return array
382
+ */
383
+ protected function _generateRow($filename)
384
+ {
385
+ return array(
386
+ 'filename' => $filename,
387
+ 'basename' => basename($filename),
388
+ );
389
+ }
390
+
391
+ /**
392
+ * Set a custom filter with callback
393
+ * The callback must take 3 params:
394
+ * string $field - field key,
395
+ * mixed $filterValue - value to filter by,
396
+ * array $row - a generated row (before generaring varien objects)
397
+ *
398
+ * @param string $field
399
+ * @param mixed $value
400
+ * @param string $type 'and'|'or'
401
+ * @param callback $callback
402
+ * @param bool $isInverted
403
+ * @return Varien_Data_Collection_Filesystem
404
+ */
405
+ public function addCallbackFilter($field, $value, $type, $callback, $isInverted = false)
406
+ {
407
+ $this->_filters[$this->_filterIncrement] = array(
408
+ 'field' => $field,
409
+ 'value' => $value,
410
+ 'is_and' => 'and' === $type,
411
+ 'callback' => $callback,
412
+ 'is_inverted' => $isInverted
413
+ );
414
+ $this->_filterIncrement++;
415
+ return $this;
416
+ }
417
+
418
+ /**
419
+ * The filters renderer and caller
420
+ * Aplies to each row, renders once.
421
+ *
422
+ * @param array $row
423
+ * @return bool
424
+ */
425
+ protected function _filterRow($row)
426
+ {
427
+ // render filters once
428
+ if (!$this->_isFiltersRendered) {
429
+ $eval = '';
430
+ for ($i = 0; $i < $this->_filterIncrement; $i++) {
431
+ if (isset($this->_filterBrackets[$i])) {
432
+ $eval .= $this->_renderConditionBeforeFilterElement($i, $this->_filterBrackets[$i]['is_and'])
433
+ . $this->_filterBrackets[$i]['value'];
434
+ }
435
+ else {
436
+ $f = '$this->_filters[' . $i . ']';
437
+ $eval .= $this->_renderConditionBeforeFilterElement($i, $this->_filters[$i]['is_and'])
438
+ . ($this->_filters[$i]['is_inverted'] ? '!' : '')
439
+ . '$this->_invokeFilter(' . "{$f}['callback'], array({$f}['field'], {$f}['value'], " . '$row))';
440
+ }
441
+ }
442
+ $this->_filterEvalRendered = $eval;
443
+ $this->_isFiltersRendered = true;
444
+ }
445
+ $result = false;
446
+ if ($this->_filterEvalRendered) {
447
+ eval('$result = ' . $this->_filterEvalRendered . ';');
448
+ }
449
+ return $result;
450
+ }
451
+
452
+ /**
453
+ * Invokes specified callback
454
+ * Skips, if there is no filtered key in the row
455
+ *
456
+ * @param callback $callback
457
+ * @param array $callbackParams
458
+ * @return bool
459
+ */
460
+ protected function _invokeFilter($callback, $callbackParams)
461
+ {
462
+ list($field, $value, $row) = $callbackParams;
463
+ if (!array_key_exists($field, $row)) {
464
+ return false;
465
+ }
466
+ return call_user_func_array($callback, $callbackParams);
467
+ }
468
+
469
+ /**
470
+ * Fancy field filter
471
+ *
472
+ * @param string $field
473
+ * @param mixed $cond
474
+ * @param string $type 'and' | 'or'
475
+ * @see Varien_Data_Collection_Db::addFieldToFilter()
476
+ * @return Varien_Data_Collection_Filesystem
477
+ */
478
+ public function addFieldToFilter($field, $cond, $type = 'and')
479
+ {
480
+ $inverted = true;
481
+
482
+ // simply check whether equals
483
+ if (!is_array($cond)) {
484
+ return $this->addCallbackFilter($field, $cond, $type, array($this, 'filterCallbackEq'));
485
+ }
486
+
487
+ // versatile filters
488
+ if (isset($cond['from']) || isset($cond['to'])) {
489
+ $this->_addFilterBracket('(', 'and' === $type);
490
+ if (isset($cond['from'])) {
491
+ $this->addCallbackFilter($field, $cond['from'], 'and', array($this, 'filterCallbackIsLessThan'), $inverted);
492
+ }
493
+ if (isset($cond['to'])) {
494
+ $this->addCallbackFilter($field, $cond['to'], 'and', array($this, 'filterCallbackIsMoreThan'), $inverted);
495
+ }
496
+ return $this->_addFilterBracket(')');
497
+ }
498
+ if (isset($cond['eq'])) {
499
+ return $this->addCallbackFilter($field, $cond['eq'], $type, array($this, 'filterCallbackEq'));
500
+ }
501
+ if (isset($cond['neq'])) {
502
+ return $this->addCallbackFilter($field, $cond['neq'], $type, array($this, 'filterCallbackEq'), $inverted);
503
+ }
504
+ if (isset($cond['like'])) {
505
+ return $this->addCallbackFilter($field, $cond['like'], $type, array($this, 'filterCallbackLike'));
506
+ }
507
+ if (isset($cond['nlike'])) {
508
+ return $this->addCallbackFilter($field, $cond['nlike'], $type, array($this, 'filterCallbackLike'), $inverted);
509
+ }
510
+ if (isset($cond['in'])) {
511
+ return $this->addCallbackFilter($field, $cond['in'], $type, array($this, 'filterCallbackInArray'));
512
+ }
513
+ if (isset($cond['nin'])) {
514
+ return $this->addCallbackFilter($field, $cond['nin'], $type, array($this, 'filterCallbackIn'), $inverted);
515
+ }
516
+ if (isset($cond['notnull'])) {
517
+ return $this->addCallbackFilter($field, $cond['notnull'], $type, array($this, 'filterCallbackIsNull'), $inverted);
518
+ }
519
+ if (isset($cond['null'])) {
520
+ return $this->addCallbackFilter($field, $cond['null'], $type, array($this, 'filterCallbackIsNull'));
521
+ }
522
+ if (isset($cond['moreq'])) {
523
+ return $this->addCallbackFilter($field, $cond['moreq'], $type, array($this, 'filterCallbackIsLessThan'), $inverted);
524
+ }
525
+ if (isset($cond['gt'])) {
526
+ return $this->addCallbackFilter($field, $cond['gt'], $type, array($this, 'filterCallbackIsMoreThan'));
527
+ }
528
+ if (isset($cond['lt'])) {
529
+ return $this->addCallbackFilter($field, $cond['lt'], $type, array($this, 'filterCallbackIsLessThan'));
530
+ }
531
+ if (isset($cond['gteq'])) {
532
+ return $this->addCallbackFilter($field, $cond['gteq'], $type, array($this, 'filterCallbackIsLessThan'), $inverted);
533
+ }
534
+ if (isset($cond['lteq'])) {
535
+ return $this->addCallbackFilter($field, $cond['lteq'], $type, array($this, 'filterCallbackIsMoreThan'), $inverted);
536
+ }
537
+ if (isset($cond['finset'])) {
538
+ $filterValue = ($cond['finset'] ? explode(',', $cond['finset']) : array());
539
+ return $this->addCallbackFilter($field, $filterValue, $type, array($this, 'filterCallbackInArray'));
540
+ }
541
+
542
+ // add OR recursively
543
+ foreach ($cond as $orCond) {
544
+ $this->_addFilterBracket('(', 'and' === $type);
545
+ $this->addFieldToFilter($field, $orCond, 'or');
546
+ $this->_addFilterBracket(')');
547
+ }
548
+ return $this;
549
+ }
550
+
551
+ /**
552
+ * Prepare a bracket into filters
553
+ *
554
+ * @param string $bracket
555
+ * @param bool $isAnd
556
+ * @return Varien_Data_Collection_Filesystem
557
+ */
558
+ protected function _addFilterBracket($bracket = '(', $isAnd = true)
559
+ {
560
+ $this->_filterBrackets[$this->_filterIncrement] = array(
561
+ 'value' => $bracket === ')' ? ')' : '(',
562
+ 'is_and' => $isAnd,
563
+ );
564
+ $this->_filterIncrement++;
565
+ return $this;
566
+ }
567
+
568
+ /**
569
+ * Render condition sign before element, if required
570
+ *
571
+ * @param int $increment
572
+ * @param bool $isAnd
573
+ * @return string
574
+ */
575
+ protected function _renderConditionBeforeFilterElement($increment, $isAnd)
576
+ {
577
+ if (isset($this->_filterBrackets[$increment]) && ')' === $this->_filterBrackets[$increment]['value']) {
578
+ return '';
579
+ }
580
+ $prevIncrement = $increment - 1;
581
+ $prevBracket = false;
582
+ if (isset($this->_filterBrackets[$prevIncrement])) {
583
+ $prevBracket = $this->_filterBrackets[$prevIncrement]['value'];
584
+ }
585
+ if ($prevIncrement < 0 || $prevBracket === '(') {
586
+ return '';
587
+ }
588
+ return ($isAnd ? ' && ' : ' || ');
589
+ }
590
+
591
+ /**
592
+ * Does nothing. Intentionally disabled parent method
593
+ *
594
+ * @return Varien_Data_Collection_Filesystem
595
+ */
596
+ public function addFilter($field, $value, $type = 'and')
597
+ {
598
+ return $this;
599
+ }
600
+
601
+ /**
602
+ * Get all ids of collected items
603
+ *
604
+ * @return array
605
+ */
606
+ public function getAllIds()
607
+ {
608
+ return array_keys($this->_items);
609
+ }
610
+
611
+ /**
612
+ * Callback method for 'like' fancy filter
613
+ *
614
+ * @param string $field
615
+ * @param mixed $filterValue
616
+ * @param array $row
617
+ * @return bool
618
+ * @see addFieldToFilter()
619
+ * @see addCallbackFilter()
620
+ */
621
+ public function filterCallbackLike($field, $filterValue, $row)
622
+ {
623
+ $filterValueRegex = str_replace('%', '(.*?)', preg_quote($filterValue, '/'));
624
+ return (bool)preg_match("/^{$filterValueRegex}$/i", $row[$field]);
625
+ }
626
+
627
+ /**
628
+ * Callback method for 'eq' fancy filter
629
+ *
630
+ * @param string $field
631
+ * @param mixed $filterValue
632
+ * @param array $row
633
+ * @return bool
634
+ * @see addFieldToFilter()
635
+ * @see addCallbackFilter()
636
+ */
637
+ public function filterCallbackEq($field, $filterValue, $row)
638
+ {
639
+ return $filterValue == $row[$field];
640
+ }
641
+
642
+ /**
643
+ * Callback method for 'in' fancy filter
644
+ *
645
+ * @param string $field
646
+ * @param mixed $filterValue
647
+ * @param array $row
648
+ * @return bool
649
+ * @see addFieldToFilter()
650
+ * @see addCallbackFilter()
651
+ */
652
+ public function filterCallbackInArray($field, $filterValue, $row)
653
+ {
654
+ return in_array($row[$field], $filterValue);
655
+ }
656
+
657
+ /**
658
+ * Callback method for 'isnull' fancy filter
659
+ *
660
+ * @param string $field
661
+ * @param mixed $filterValue
662
+ * @param array $row
663
+ * @return bool
664
+ * @see addFieldToFilter()
665
+ * @see addCallbackFilter()
666
+ */
667
+ public function filterCallbackIsNull($field, $filterValue, $row)
668
+ {
669
+ return null === $row[$field];
670
+ }
671
+
672
+ /**
673
+ * Callback method for 'moreq' fancy filter
674
+ *
675
+ * @param string $field
676
+ * @param mixed $filterValue
677
+ * @param array $row
678
+ * @return bool
679
+ * @see addFieldToFilter()
680
+ * @see addCallbackFilter()
681
+ */
682
+ public function filterCallbackIsMoreThan($field, $filterValue, $row)
683
+ {
684
+ return $row[$field] > $filterValue;
685
+ }
686
+
687
+ /**
688
+ * Callback method for 'lteq' fancy filter
689
+ *
690
+ * @param string $field
691
+ * @param mixed $filterValue
692
+ * @param array $row
693
+ * @return bool
694
+ * @see addFieldToFilter()
695
+ * @see addCallbackFilter()
696
+ */
697
+ public function filterCallbackIsLessThan($field, $filterValue, $row)
698
+ {
699
+ return $row[$field] < $filterValue;
700
+ }
701
+ }
lib/Varien/Data/Form.php ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Data form
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form extends Varien_Data_Form_Abstract
35
+ {
36
+ /**
37
+ * All form elements collection
38
+ *
39
+ * @var Varien_Data_Form_Element_Collection
40
+ */
41
+ protected $_allElements;
42
+
43
+ /**
44
+ * form elements index
45
+ *
46
+ * @var array
47
+ */
48
+ protected $_elementsIndex;
49
+
50
+ static protected $_defaultElementRenderer;
51
+ static protected $_defaultFieldsetRenderer;
52
+ static protected $_defaultFieldsetElementRenderer;
53
+
54
+ public function __construct($attributes = array())
55
+ {
56
+ parent::__construct($attributes);
57
+ $this->_allElements = new Varien_Data_Form_Element_Collection($this);
58
+ }
59
+
60
+ public static function setElementRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
61
+ {
62
+ self::$_defaultElementRenderer = $renderer;
63
+ }
64
+
65
+ public static function setFieldsetRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
66
+ {
67
+ self::$_defaultFieldsetRenderer = $renderer;
68
+ }
69
+
70
+ public static function setFieldsetElementRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
71
+ {
72
+ self::$_defaultFieldsetElementRenderer = $renderer;
73
+ }
74
+
75
+ public static function getElementRenderer()
76
+ {
77
+ return self::$_defaultElementRenderer;
78
+ }
79
+
80
+ public static function getFieldsetRenderer()
81
+ {
82
+ return self::$_defaultFieldsetRenderer;
83
+ }
84
+
85
+ public static function getFieldsetElementRenderer()
86
+ {
87
+ return self::$_defaultFieldsetElementRenderer;
88
+ }
89
+
90
+ /**
91
+ * Return allowed HTML form attributes
92
+ * @return array
93
+ */
94
+ public function getHtmlAttributes()
95
+ {
96
+ return array('id', 'name', 'method', 'action', 'enctype', 'class', 'onsubmit');
97
+ }
98
+
99
+ /**
100
+ * Add form element
101
+ *
102
+ * @param Varien_Data_Form_Element_Abstract $element
103
+ * @return Varien_Data_Form
104
+ */
105
+ public function addElement(Varien_Data_Form_Element_Abstract $element, $after=false)
106
+ {
107
+ $this->checkElementId($element->getId());
108
+ parent::addElement($element, $after);
109
+ $this->addElementToCollection($element);
110
+ return $this;
111
+ }
112
+
113
+ /**
114
+ * Check existing element
115
+ *
116
+ * @param string $elementId
117
+ * @return bool
118
+ */
119
+ protected function _elementIdExists($elementId)
120
+ {
121
+ return isset($this->_elementsIndex[$elementId]);
122
+ }
123
+
124
+ public function addElementToCollection($element)
125
+ {
126
+ $this->_elementsIndex[$element->getId()] = $element;
127
+ $this->_allElements->add($element);
128
+ return $this;
129
+ }
130
+
131
+ public function checkElementId($elementId)
132
+ {
133
+ if ($this->_elementIdExists($elementId)) {
134
+ throw new Exception('Element with id "'.$elementId.'" already exists');
135
+ }
136
+ return true;
137
+ }
138
+
139
+ public function getForm()
140
+ {
141
+ return $this;
142
+ }
143
+
144
+ public function getElement($elementId)
145
+ {
146
+ if ($this->_elementIdExists($elementId)) {
147
+ return $this->_elementsIndex[$elementId];
148
+ }
149
+ return null;
150
+ }
151
+
152
+ public function setValues($values)
153
+ {
154
+ foreach ($this->_allElements as $element) {
155
+ if (isset($values[$element->getId()])) {
156
+ $element->setValue($values[$element->getId()]);
157
+ }
158
+ else {
159
+ $element->setValue(null);
160
+ }
161
+ }
162
+ return $this;
163
+ }
164
+
165
+ public function addValues($values)
166
+ {
167
+ if (!is_array($values)) {
168
+ return $this;
169
+ }
170
+ foreach ($values as $elementId=>$value) {
171
+ if ($element = $this->getElement($elementId)) {
172
+ $element->setValue($value);
173
+ }
174
+ }
175
+ return $this;
176
+ }
177
+
178
+ /**
179
+ * Add suffix to name of all elements
180
+ *
181
+ * @param string $suffix
182
+ * @return Varien_Data_Form
183
+ */
184
+ public function addFieldNameSuffix($suffix)
185
+ {
186
+ foreach ($this->_allElements as $element) {
187
+ $name = $element->getName();
188
+ if ($name) {
189
+ $element->setName($this->addSuffixToName($name, $suffix));
190
+ }
191
+ }
192
+ return $this;
193
+ }
194
+
195
+ public function addSuffixToName($name, $suffix)
196
+ {
197
+ if (!$name) {
198
+ return $suffix;
199
+ }
200
+ $vars = explode('[', $name);
201
+ $newName = $suffix;
202
+ foreach ($vars as $index=>$value) {
203
+ $newName.= '['.$value;
204
+ if ($index==0) {
205
+ $newName.= ']';
206
+ }
207
+ }
208
+ return $newName;
209
+ }
210
+
211
+ public function removeField($elementId)
212
+ {
213
+ if ($this->_elementIdExists($elementId)) {
214
+ unset($this->_elementsIndex[$elementId]);
215
+ }
216
+ return $this;
217
+ }
218
+
219
+ public function setFieldContainerIdPrefix($prefix)
220
+ {
221
+ $this->setData('field_container_id_prefix', $prefix);
222
+ return $this;
223
+ }
224
+
225
+ public function getFieldContainerIdPrefix()
226
+ {
227
+ return $this->getData('field_container_id_prefix');
228
+ }
229
+
230
+ public function toHtml()
231
+ {
232
+ Varien_Profiler::start('form/toHtml');
233
+ $html = '';
234
+ if ($useContainer = $this->getUseContainer()) {
235
+ $html .= '<form '.$this->serialize($this->getHtmlAttributes()).'>';
236
+ $html .= '<div>';
237
+ if (strtolower($this->getData('method')) == 'post') {
238
+ $html .= '<input name="form_key" type="hidden" value="'.Mage::getSingleton('core/session')->getFormKey().'" />';
239
+ }
240
+ $html .= '</div>';
241
+ }
242
+
243
+ foreach ($this->getElements() as $element) {
244
+ $html.= $element->toHtml();
245
+ }
246
+
247
+ if ($useContainer) {
248
+ $html.= '</form>';
249
+ }
250
+ Varien_Profiler::stop('form/toHtml');
251
+ return $html;
252
+ }
253
+
254
+ public function getHtml()
255
+ {
256
+ return $this->toHtml();
257
+ }
258
+ }
lib/Varien/Data/Form/Abstract.php ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Abstract class for form, coumn and fieldset
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Abstract extends Varien_Object
36
+ {
37
+
38
+ /**
39
+ * Form level elements collection
40
+ *
41
+ * @var Varien_Data_Form_Element_Collection
42
+ */
43
+ protected $_elements;
44
+
45
+ /**
46
+ * Element type classes
47
+ *
48
+ * @var unknown_type
49
+ */
50
+ protected $_types = array();
51
+
52
+ /**
53
+ * Enter description here...
54
+ *
55
+ * @param array $attributes
56
+ */
57
+ public function __construct($attributes = array())
58
+ {
59
+ parent::__construct($attributes);
60
+ }
61
+
62
+ /**
63
+ * Enter description here...
64
+ *
65
+ * @param string $type
66
+ * @param string $className
67
+ * @return Varien_Data_Form_Abstract
68
+ */
69
+ public function addType($type, $className)
70
+ {
71
+ $this->_types[$type] = $className;
72
+ return $this;
73
+ }
74
+
75
+ /**
76
+ * Enter description here...
77
+ *
78
+ * @return Varien_Data_Form_Element_Collection
79
+ */
80
+ public function getElements()
81
+ {
82
+ if (empty($this->_elements)) {
83
+ $this->_elements = new Varien_Data_Form_Element_Collection($this);
84
+ }
85
+ return $this->_elements;
86
+ }
87
+
88
+ /**
89
+ * Disable elements
90
+ *
91
+ * @param boolean $readonly
92
+ * @param boolean $useDisabled
93
+ * @return Varien_Data_Form_Abstract
94
+ */
95
+ public function setReadonly($readonly, $useDisabled = false)
96
+ {
97
+ if ($useDisabled) {
98
+ $this->setDisabled($readonly);
99
+ $this->setData('readonly_disabled', $readonly);
100
+ } else {
101
+ $this->setData('readonly', $readonly);
102
+ }
103
+ foreach ($this->getElements() as $element) {
104
+ $element->setReadonly($readonly, $useDisabled);
105
+ }
106
+
107
+ return $this;
108
+ }
109
+
110
+ /**
111
+ * Add form element
112
+ *
113
+ * @param Varien_Data_Form_Element_Abstract $element
114
+ * @return Varien_Data_Form
115
+ */
116
+ public function addElement(Varien_Data_Form_Element_Abstract $element, $after=null)
117
+ {
118
+ $element->setForm($this);
119
+ $this->getElements()->add($element, $after);
120
+ return $this;
121
+ }
122
+
123
+ /**
124
+ * Add child element
125
+ *
126
+ * if $after parameter is false - then element adds to end of collection
127
+ * if $after parameter is null - then element adds to befin of collection
128
+ * if $after parameter is string - then element adds after of the element with some id
129
+ *
130
+ * @param string $elementId
131
+ * @param string $type
132
+ * @param array $config
133
+ * @param mixed $after
134
+ * @return Varien_Data_Form_Element_Abstract
135
+ */
136
+ public function addField($elementId, $type, $config, $after=false)
137
+ {
138
+ if (isset($this->_types[$type])) {
139
+ $className = $this->_types[$type];
140
+ }
141
+ else {
142
+ $className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
143
+ }
144
+ $element = new $className($config);
145
+ $element->setId($elementId);
146
+ $this->addElement($element, $after);
147
+ return $element;
148
+ }
149
+
150
+ /**
151
+ * Enter description here...
152
+ *
153
+ * @param string $elementId
154
+ * @return Varien_Data_Form_Abstract
155
+ */
156
+ public function removeField($elementId)
157
+ {
158
+ $this->getElements()->remove($elementId);
159
+ return $this;
160
+ }
161
+
162
+ /**
163
+ * Enter description here...
164
+ *
165
+ * @param string $elementId
166
+ * @param unknown_type $config
167
+ * @param unknown_type $after
168
+ * @return Varien_Data_Form_Element_Fieldset
169
+ */
170
+ public function addFieldset($elementId, $config, $after=false)
171
+ {
172
+ $element = new Varien_Data_Form_Element_Fieldset($config);
173
+ $element->setId($elementId);
174
+ $this->addElement($element, $after);
175
+ return $element;
176
+ }
177
+
178
+ /**
179
+ * Enter description here...
180
+ *
181
+ * @param string $elementId
182
+ * @param array $config
183
+ * @return Varien_Data_Form_Element_Column
184
+ */
185
+ public function addColumn($elementId, $config)
186
+ {
187
+ $element = new Varien_Data_Form_Element_Column($config);
188
+ $element->setForm($this)
189
+ ->setId($elementId);
190
+ $this->addElement($element);
191
+ return $element;
192
+ }
193
+
194
+ /**
195
+ * Enter description here...
196
+ *
197
+ * @param array $arrAttributes
198
+ * @return array
199
+ */
200
+ public function __toArray(array $arrAttributes = array())
201
+ {
202
+ $res = array();
203
+ $res['config'] = $this->getData();
204
+ $res['formElements']= array();
205
+ foreach ($this->getElements() as $element) {
206
+ $res['formElements'][] = $element->toArray();
207
+ }
208
+ return $res;
209
+ }
210
+
211
+ }
lib/Varien/Data/Form/Element/Abstract.php ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Data form abstract class
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Data_Form_Element_Abstract extends Varien_Data_Form_Abstract
36
+ {
37
+ protected $_id;
38
+ protected $_type;
39
+ protected $_form;
40
+ protected $_elements;
41
+ protected $_renderer;
42
+
43
+ public function __construct($attributes = array())
44
+ {
45
+ parent::__construct($attributes);
46
+ $this->_renderer = Varien_Data_Form::getElementRenderer();
47
+ }
48
+
49
+ /**
50
+ * Add form element
51
+ *
52
+ * @param Varien_Data_Form_Element_Abstract $element
53
+ * @return Varien_Data_Form
54
+ */
55
+ public function addElement(Varien_Data_Form_Element_Abstract $element, $after=false)
56
+ {
57
+ if ($this->getForm()) {
58
+ $this->getForm()->checkElementId($element->getId());
59
+ $this->getForm()->addElementToCollection($element);
60
+ }
61
+
62
+ parent::addElement($element, $after);
63
+ return $this;
64
+ }
65
+
66
+ public function getId()
67
+ {
68
+ return $this->_id;
69
+ }
70
+
71
+ public function getType()
72
+ {
73
+ return $this->_type;
74
+ }
75
+
76
+ public function getForm()
77
+ {
78
+ return $this->_form;
79
+ }
80
+
81
+ public function setId($id)
82
+ {
83
+ $this->_id = $id;
84
+ $this->setData('html_id', $id);
85
+ return $this;
86
+ }
87
+
88
+ public function getHtmlId()
89
+ {
90
+ return $this->getForm()->getHtmlIdPrefix() . $this->getData('html_id') . $this->getForm()->getHtmlIdSuffix();
91
+ }
92
+
93
+ public function getName()
94
+ {
95
+ $name = $this->getData('name');
96
+ if ($suffix = $this->getForm()->getFieldNameSuffix()) {
97
+ $name = $this->getForm()->addSuffixToName($name, $suffix);
98
+ }
99
+ return $name;
100
+ }
101
+
102
+ public function setType($type)
103
+ {
104
+ $this->_type = $type;
105
+ $this->setData('type', $type);
106
+ return $this;
107
+ }
108
+
109
+ public function setForm($form)
110
+ {
111
+ $this->_form = $form;
112
+ return $this;
113
+ }
114
+
115
+ public function removeField($elementId)
116
+ {
117
+ $this->getForm()->removeField($elementId);
118
+ return parent::removeField($elementId);
119
+ }
120
+
121
+ public function getHtmlAttributes()
122
+ {
123
+ return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex');
124
+ }
125
+
126
+ public function addClass($class)
127
+ {
128
+ $oldClass = $this->getClass();
129
+ $this->setClass($oldClass.' '.$class);
130
+ return $this;
131
+ }
132
+
133
+ /**
134
+ * Remove CSS class
135
+ *
136
+ * @param string $class
137
+ * @return Varien_Data_Form_Element_Abstract
138
+ */
139
+ public function removeClass($class)
140
+ {
141
+ $classes = array_unique(explode(' ', $this->getClass()));
142
+ if (false !== ($key = array_search($class, $classes))) {
143
+ unset($classes[$key]);
144
+ }
145
+ $this->setClass(implode(' ', $classes));
146
+ return $this;
147
+ }
148
+
149
+ protected function _escape($string)
150
+ {
151
+ return htmlspecialchars($string, ENT_COMPAT);
152
+ }
153
+
154
+ public function getEscapedValue($index=null)
155
+ {
156
+ $value = $this->getValue($index);
157
+
158
+ if ($filter = $this->getValueFilter()) {
159
+ $value = $filter->filter($value);
160
+ }
161
+ return $this->_escape($value);
162
+ }
163
+
164
+ public function setRenderer(Varien_Data_Form_Element_Renderer_Interface $renderer)
165
+ {
166
+ $this->_renderer = $renderer;
167
+ return $this;
168
+ }
169
+
170
+ public function getRenderer()
171
+ {
172
+ return $this->_renderer;
173
+ }
174
+
175
+ public function getElementHtml()
176
+ {
177
+ $html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
178
+ .'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
179
+ $html.= $this->getAfterElementHtml();
180
+ return $html;
181
+ }
182
+
183
+ public function getAfterElementHtml()
184
+ {
185
+ return $this->getData('after_element_html');
186
+ }
187
+
188
+ public function getLabelHtml($idSuffix = '')
189
+ {
190
+ if (!is_null($this->getLabel())) {
191
+ $html = '<label for="'.$this->getHtmlId() . $idSuffix . '">'.$this->getLabel()
192
+ . ( $this->getRequired() ? ' <span class="required">*</span>' : '' ).'</label>'."\n";
193
+ }
194
+ else {
195
+ $html = '';
196
+ }
197
+ return $html;
198
+ }
199
+
200
+ public function getDefaultHtml()
201
+ {
202
+ $html = $this->getData('default_html');
203
+ if (is_null($html)) {
204
+ $html = ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
205
+ $html.= $this->getLabelHtml();
206
+ $html.= $this->getElementHtml();
207
+ $html.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
208
+ }
209
+ return $html;
210
+ }
211
+
212
+ public function getHtml()
213
+ {
214
+ if ($this->getRequired()) {
215
+ $this->addClass('required-entry');
216
+ }
217
+ if ($this->_renderer) {
218
+ $html = $this->_renderer->render($this);
219
+ }
220
+ else {
221
+ $html = $this->getDefaultHtml();
222
+ }
223
+ return $html;
224
+ }
225
+
226
+ public function toHtml()
227
+ {
228
+ return $this->getHtml();
229
+ }
230
+
231
+ public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
232
+ {
233
+ if (in_array('disabled', $attributes) && !empty($this->_data['disabled'])) {
234
+ $this->_data['disabled'] = 'disabled';
235
+ }
236
+ else {
237
+ unset($this->_data['disabled']);
238
+ }
239
+ if (in_array('checked', $attributes) && !empty($this->_data['checked'])) {
240
+ $this->_data['checked'] = 'checked';
241
+ }
242
+ else {
243
+ unset($this->_data['checked']);
244
+ }
245
+ return parent::serialize($attributes, $valueSeparator, $fieldSeparator, $quote);
246
+ }
247
+
248
+ public function getReadonly()
249
+ {
250
+ if ($this->hasData('readonly_disabled')) {
251
+ return $this->_getData('readonly_disabled');
252
+ }
253
+
254
+ return $this->_getData('readonly');
255
+ }
256
+
257
+ public function getHtmlContainerId()
258
+ {
259
+ if ($this->hasData('container_id')) {
260
+ return $this->getData('container_id');
261
+ } elseif ($idPrefix = $this->getForm()->getFieldContainerIdPrefix()) {
262
+ return $idPrefix . $this->getId();
263
+ }
264
+ return '';
265
+ }
266
+
267
+ /**
268
+ * Add specified values to element values
269
+ *
270
+ * @param string|int|array $values
271
+ * @param bool $overwrite
272
+ * @return Varien_Data_Form_Element_Abstract
273
+ */
274
+ public function addElementValues($values, $overwrite = false)
275
+ {
276
+ if (empty($values) || (is_string($values) && trim($values) == '')) {
277
+ return $this;
278
+ }
279
+ if (!is_array($values)) {
280
+ $values = Mage::helper('core')->escapeHtml(trim($values));
281
+ $values = array($values => $values);
282
+ }
283
+ $elementValues = $this->getValues();
284
+ if (!empty($elementValues)) {
285
+ foreach ($values as $key => $value) {
286
+ if ((isset($elementValues[$key]) && $overwrite) || !isset($elementValues[$key])) {
287
+ $elementValues[$key] = Mage::helper('core')->escapeHtml($value);
288
+ }
289
+ }
290
+ $values = $elementValues;
291
+ }
292
+ $this->setValues($values);
293
+
294
+ return $this;
295
+ }
296
+ }
lib/Varien/Data/Form/Element/Button.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form button element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Button extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('button');
40
+ $this->setExtType('textfield');
41
+ }
42
+ }
lib/Varien/Data/Form/Element/Checkbox.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form checkbox element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Checkbox extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('checkbox');
40
+ $this->setExtType('checkbox');
41
+ }
42
+
43
+ public function getHtmlAttributes()
44
+ {
45
+ return array('type', 'title', 'class', 'style', 'checked', 'onclick', 'onchange', 'disabled', 'tabindex');
46
+ }
47
+
48
+ public function getElementHtml()
49
+ {
50
+ if ($checked = $this->getChecked()) {
51
+ $this->setData('checked', true);
52
+ }
53
+ else {
54
+ $this->unsetData('checked');
55
+ }
56
+ return parent::getElementHtml();
57
+ }
58
+
59
+ /**
60
+ * Set check status of checkbox
61
+ *
62
+ * @param boolean $value
63
+ * @return Varien_Data_Form_Element_Checkbox
64
+ */
65
+ public function setIsChecked($value=false)
66
+ {
67
+ $this->setData('checked', $value);
68
+ return $this;
69
+ }
70
+
71
+ /**
72
+ * Return check status of checkbox
73
+ *
74
+ * @return boolean
75
+ */
76
+ public function getIsChecked() {
77
+ return $this->getData('checked');
78
+ }
79
+ }
lib/Varien/Data/Form/Element/Checkboxes.php ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form select element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Checkboxes extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ /**
37
+ * Init Element
38
+ *
39
+ * @param array $attributes
40
+ */
41
+ public function __construct($attributes=array())
42
+ {
43
+ parent::__construct($attributes);
44
+ $this->setType('checkbox');
45
+ $this->setExtType('checkbox');
46
+ }
47
+
48
+ /**
49
+ * Retrieve allow attributes
50
+ *
51
+ * @return array
52
+ */
53
+ public function getHtmlAttributes()
54
+ {
55
+ return array('type', 'name', 'class', 'style', 'checked', 'onclick', 'onchange', 'disabled');
56
+ }
57
+
58
+ /**
59
+ * Prepare value list
60
+ *
61
+ * @return array
62
+ */
63
+ protected function _prepareValues() {
64
+ $options = array();
65
+ $values = array();
66
+
67
+ if ($this->getValues()) {
68
+ if (!is_array($this->getValues())) {
69
+ $options = array($this->getValues());
70
+ }
71
+ else {
72
+ $options = $this->getValues();
73
+ }
74
+ }
75
+ elseif ($this->getOptions() && is_array($this->getOptions())) {
76
+ $options = $this->getOptions();
77
+ }
78
+ foreach ($options as $k => $v) {
79
+ if (is_string($v)) {
80
+ $values[] = array(
81
+ 'label' => $v,
82
+ 'value' => $k
83
+ );
84
+ }
85
+ elseif (isset($v['value'])) {
86
+ if (!isset($v['label'])) {
87
+ $v['label'] = $v['value'];
88
+ }
89
+ $values[] = array(
90
+ 'label' => $v['label'],
91
+ 'value' => $v['value']
92
+ );
93
+ }
94
+ }
95
+
96
+ return $values;
97
+ }
98
+
99
+ /**
100
+ * Retrieve HTML
101
+ *
102
+ * @return string
103
+ */
104
+ public function getElementHtml()
105
+ {
106
+ $values = $this->_prepareValues();
107
+
108
+ if (!$values) {
109
+ return '';
110
+ }
111
+
112
+ $html = '<ul class="checkboxes">';
113
+ foreach ($values as $value) {
114
+ $html.= $this->_optionToHtml($value);
115
+ }
116
+ $html .= '</ul>'
117
+ . $this->getAfterElementHtml();
118
+
119
+ return $html;
120
+ }
121
+
122
+ public function getChecked($value)
123
+ {
124
+ if ($checked = $this->getValue()) {
125
+ }
126
+ elseif ($checked = $this->getData('checked')) {
127
+ }
128
+ else {
129
+ return ;
130
+ }
131
+ if (!is_array($checked)) {
132
+ $checked = array(strval($checked));
133
+ }
134
+ else {
135
+ foreach ($checked as $k => $v) {
136
+ $checked[$k] = strval($v);
137
+ }
138
+ }
139
+ if (in_array(strval($value), $checked)) {
140
+ return 'checked';
141
+ }
142
+ return ;
143
+ }
144
+
145
+ public function getDisabled($value)
146
+ {
147
+ if ($disabled = $this->getData('disabled')) {
148
+ if (!is_array($disabled)) {
149
+ $disabled = array(strval($disabled));
150
+ }
151
+ else {
152
+ foreach ($disabled as $k => $v) {
153
+ $disabled[$k] = strval($v);
154
+ }
155
+ }
156
+ if (in_array(strval($value), $disabled)) {
157
+ return 'disabled';
158
+ }
159
+ }
160
+ return ;
161
+ }
162
+
163
+ public function getOnclick($value)
164
+ {
165
+ if ($onclick = $this->getData('onclick')) {
166
+ return str_replace('$value', $value, $onclick);
167
+ }
168
+ return ;
169
+ }
170
+
171
+ public function getOnchange($value)
172
+ {
173
+ if ($onchange = $this->getData('onchange')) {
174
+ return str_replace('$value', $value, $onchange);
175
+ }
176
+ return ;
177
+ }
178
+
179
+ // public function getName($value)
180
+ // {
181
+ // if ($name = $this->getData('name')) {
182
+ // return str_replace('$value', $value, $name);
183
+ // }
184
+ // return ;
185
+ // }
186
+
187
+ protected function _optionToHtml($option)
188
+ {
189
+ $id = $this->getHtmlId().'_'.$this->_escape($option['value']);
190
+
191
+ $html = '<li><input id="'.$id.'"';
192
+ foreach ($this->getHtmlAttributes() as $attribute) {
193
+ if ($value = $this->getDataUsingMethod($attribute, $option['value'])) {
194
+ $html .= ' '.$attribute.'="'.$value.'"';
195
+ }
196
+ }
197
+ $html .= ' value="'.$option['value'].'" />'
198
+ . ' <label for="'.$id.'">' . $option['label'] . '</label></li>'
199
+ . "\n";
200
+ return $html;
201
+ }
202
+ }
lib/Varien/Data/Form/Element/Collection.php ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form element collection
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Collection implements ArrayAccess, IteratorAggregate
35
+ {
36
+
37
+ /**
38
+ * Elements storage
39
+ *
40
+ * @var array
41
+ */
42
+ private $_elements;
43
+
44
+ /**
45
+ * Elements container
46
+ *
47
+ * @var Varien_Data_Form_Abstract
48
+ */
49
+ private $_container;
50
+
51
+ /**
52
+ * Class constructor
53
+ *
54
+ * @param Varien_Data_Form_Abstract $container
55
+ */
56
+ public function __construct($container)
57
+ {
58
+ $this->_elements = array();
59
+ $this->_container = $container;
60
+ }
61
+
62
+ /**
63
+ * Implementation of IteratorAggregate::getIterator()
64
+ *
65
+ * @return ArrayIterator
66
+ */
67
+ public function getIterator()
68
+ {
69
+ return new ArrayIterator($this->_elements);
70
+ }
71
+
72
+ /**
73
+ * Implementation of ArrayAccess:offsetSet()
74
+ *
75
+ * @param mixed $key
76
+ * @param mixed $value
77
+ */
78
+ public function offsetSet($key, $value)
79
+ {
80
+ $this->_elements[$key] = $value;
81
+ }
82
+
83
+ /**
84
+ * Implementation of ArrayAccess:offsetGet()
85
+ *
86
+ * @param mixed $key
87
+ */
88
+ public function offsetGet($key)
89
+ {
90
+ return $this->_elements[$key];
91
+ }
92
+
93
+ /**
94
+ * Implementation of ArrayAccess:offsetUnset()
95
+ *
96
+ * @param mixed $key
97
+ */
98
+ public function offsetUnset($key)
99
+ {
100
+ unset($this->_elements[$key]);
101
+ }
102
+
103
+ /**
104
+ * Implementation of ArrayAccess:offsetExists()
105
+ *
106
+ * @param mixed $key
107
+ * @return boolean
108
+ */
109
+ public function offsetExists($key)
110
+ {
111
+ return isset($this->_elements[$key]);
112
+ }
113
+
114
+ /**
115
+ * Add element to collection
116
+ *
117
+ * @todo get it straight with $after
118
+ * @param Varien_Data_Form_Element_Abstract $element
119
+ * @param boolean|'^'|string $after
120
+ * @return Varien_Data_Form_Element_Abstract
121
+ */
122
+ public function add(Varien_Data_Form_Element_Abstract $element, $after=false)
123
+ {
124
+ // Set the Form for the node
125
+ if ($this->_container->getForm() instanceof Varien_Data_Form) {
126
+ $element->setContainer($this->_container);
127
+ $element->setForm($this->_container->getForm());
128
+ }
129
+
130
+ if ($after === false) {
131
+ $this->_elements[] = $element;
132
+ }
133
+ elseif ($after === '^') {
134
+ array_unshift($this->_elements, $element);
135
+ }
136
+ elseif (is_string($after)) {
137
+ $newOrderElements = array();
138
+ foreach ($this->_elements as $index => $currElement) {
139
+ if ($currElement->getId() == $after) {
140
+ $newOrderElements[] = $currElement;
141
+ $newOrderElements[] = $element;
142
+ $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, $index + 1));
143
+ return $element;
144
+ }
145
+ $newOrderElements[] = $currElement;
146
+ }
147
+ $this->_elements[] = $element;
148
+ }
149
+
150
+ return $element;
151
+ }
152
+
153
+ /**
154
+ * Sort elements by values using a user-defined comparison function
155
+ *
156
+ * @param mixed $callback
157
+ * @return Varien_Data_Form_Element_Collection
158
+ */
159
+ public function usort($callback)
160
+ {
161
+ usort($this->_elements, $callback);
162
+ return $this;
163
+ }
164
+
165
+ /**
166
+ * Remove element from collection
167
+ *
168
+ * @param mixed $elementId
169
+ * @return Varien_Data_Form_Element_Collection
170
+ */
171
+ public function remove($elementId)
172
+ {
173
+ foreach ($this->_elements as $index => $element) {
174
+ if ($elementId == $element->getId()) {
175
+ unset($this->_elements[$index]);
176
+ }
177
+ }
178
+ // Renumber elements for further correct adding and removing other elements
179
+ $this->_elements = array_merge($this->_elements, array());
180
+ return $this;
181
+ }
182
+
183
+ /**
184
+ * Count elements in collection
185
+ *
186
+ * @return int
187
+ */
188
+ public function count()
189
+ {
190
+ return count($this->_elements);
191
+ }
192
+
193
+ /**
194
+ * Find element by ID
195
+ *
196
+ * @param mixed $elementId
197
+ * @return Varien_Data_Form_Element_Abstract|null
198
+ */
199
+ public function searchById($elementId)
200
+ {
201
+ foreach ($this->_elements as $element) {
202
+ if ($element->getId() == $elementId) {
203
+ return $element;
204
+ }
205
+ }
206
+ return null;
207
+ }
208
+
209
+ }
lib/Varien/Data/Form/Element/Column.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form column
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Column extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes = array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('column');
40
+ }
41
+ }
lib/Varien/Data/Form/Element/Date.php ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien data selector form element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Date extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ /**
37
+ * @var Zend_Date
38
+ */
39
+ protected $_value;
40
+
41
+ public function __construct($attributes=array())
42
+ {
43
+ parent::__construct($attributes);
44
+ $this->setType('text');
45
+ $this->setExtType('textfield');
46
+ if (isset($attributes['value'])) {
47
+ $this->setValue($attributes['value']);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * If script executes on x64 system, converts large
53
+ * numeric values to timestamp limit
54
+ */
55
+ protected function _toTimestamp($value)
56
+ {
57
+
58
+ $value = (int)$value;
59
+ if ($value > 3155760000) {
60
+ $value = 0;
61
+ }
62
+
63
+ return $value;
64
+ }
65
+
66
+
67
+ /**
68
+ * Set date value
69
+ * If Zend_Date instance is provided instead of value, other params will be ignored.
70
+ * Format and locale must be compatible with Zend_Date
71
+ *
72
+ * @param mixed $value
73
+ * @param string $format
74
+ * @param string $locale
75
+ * @return Varien_Data_Form_Element_Date
76
+ */
77
+ public function setValue($value, $format = null, $locale = null)
78
+ {
79
+ if (empty($value)) {
80
+ $this->_value = '';
81
+ return $this;
82
+ }
83
+ if ($value instanceof Zend_Date) {
84
+ $this->_value = $value;
85
+ return $this;
86
+ }
87
+ if (preg_match('/^[0-9]+$/', $value)) {
88
+ $this->_value = new Zend_Date($this->_toTimestamp($value));
89
+ //$this->_value = new Zend_Date((int)value);
90
+ return $this;
91
+ }
92
+ // last check, if input format was set
93
+ if (null === $format) {
94
+ $format = Varien_Date::DATETIME_INTERNAL_FORMAT;
95
+ if ($this->getInputFormat()) {
96
+ $format = $this->getInputFormat();
97
+ }
98
+ }
99
+ // last check, if locale was set
100
+ if (null === $locale) {
101
+ if (!$locale = $this->getLocale()) {
102
+ $locale = null;
103
+ }
104
+ }
105
+ try {
106
+ $this->_value = new Zend_Date($value, $format, $locale);
107
+ }
108
+ catch (Exception $e) {
109
+ $this->_value = '';
110
+ }
111
+ return $this;
112
+ }
113
+
114
+ /**
115
+ * Get date value as string.
116
+ * Format can be specified, or it will be taken from $this->getFormat()
117
+ *
118
+ * @param string $format (compatible with Zend_Date)
119
+ * @return string
120
+ */
121
+ public function getValue($format = null)
122
+ {
123
+ if (empty($this->_value)) {
124
+ return '';
125
+ }
126
+ if (null === $format) {
127
+ $format = $this->getFormat();
128
+ }
129
+ return $this->_value->toString($format);
130
+ }
131
+
132
+ /**
133
+ * Get value instance, if any
134
+ *
135
+ * @return Zend_Date
136
+ */
137
+ public function getValueInstance()
138
+ {
139
+ if (empty($this->_value)) {
140
+ return null;
141
+ }
142
+ return $this->_value;
143
+ }
144
+
145
+ /**
146
+ * Output the input field and assign calendar instance to it.
147
+ * In order to output the date:
148
+ * - the value must be instantiated (Zend_Date)
149
+ * - output format must be set (compatible with Zend_Date)
150
+ *
151
+ * @return string
152
+ */
153
+ public function getElementHtml()
154
+ {
155
+ $this->addClass('input-text');
156
+
157
+ $html = sprintf(
158
+ '<input name="%s" id="%s" value="%s" %s style="width:110px !important;" />'
159
+ .' <img src="%s" alt="" class="v-middle" id="%s_trig" title="%s" style="%s" />',
160
+ $this->getName(), $this->getHtmlId(), $this->_escape($this->getValue()), $this->serialize($this->getHtmlAttributes()),
161
+ $this->getImage(), $this->getHtmlId(), 'Select Date', ($this->getDisabled() ? 'display:none;' : '')
162
+ );
163
+ $outputFormat = $this->getFormat();
164
+ if (empty($outputFormat)) {
165
+ throw new Exception('Output format is not specified. Please, specify "format" key in constructor, or set it using setFormat().');
166
+ }
167
+ $displayFormat = Varien_Date::convertZendToStrFtime($outputFormat, true, (bool)$this->getTime());
168
+
169
+ $html .= sprintf('
170
+ <script type="text/javascript">
171
+ //<![CDATA[
172
+ Calendar.setup({
173
+ inputField: "%s",
174
+ ifFormat: "%s",
175
+ showsTime: %s,
176
+ button: "%s_trig",
177
+ align: "Bl",
178
+ singleClick : true
179
+ });
180
+ //]]>
181
+ </script>',
182
+ $this->getHtmlId(), $displayFormat,
183
+ $this->getTime() ? 'true' : 'false', $this->getHtmlId()
184
+ );
185
+
186
+ $html .= $this->getAfterElementHtml();
187
+
188
+ return $html;
189
+ }
190
+ }
lib/Varien/Data/Form/Element/Editor.php ADDED
@@ -0,0 +1,399 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form editor element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Editor extends Varien_Data_Form_Element_Textarea
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+
40
+ if($this->isEnabled()) {
41
+ $this->setType('wysiwyg');
42
+ $this->setExtType('wysiwyg');
43
+ } else {
44
+ $this->setType('textarea');
45
+ $this->setExtType('textarea');
46
+ }
47
+ }
48
+
49
+ public function getElementHtml()
50
+ {
51
+ $js = '
52
+ <script type="text/javascript">
53
+ //<![CDATA[
54
+ openEditorPopup = function(url, name, specs, parent) {
55
+ if ((typeof popups == "undefined") || popups[name] == undefined || popups[name].closed) {
56
+ if (typeof popups == "undefined") {
57
+ popups = new Array();
58
+ }
59
+ var opener = (parent != undefined ? parent : window);
60
+ popups[name] = opener.open(url, name, specs);
61
+ } else {
62
+ popups[name].focus();
63
+ }
64
+ return popups[name];
65
+ }
66
+
67
+ closeEditorPopup = function(name) {
68
+ if ((typeof popups != "undefined") && popups[name] != undefined && !popups[name].closed) {
69
+ popups[name].close();
70
+ }
71
+ }
72
+ //]]>
73
+ </script>';
74
+
75
+ if($this->isEnabled())
76
+ {
77
+ // add Firebug notice translations
78
+ $warn = 'Firebug is known to make the WYSIWYG editor slow unless it is turned off or configured properly.';
79
+ $this->getConfig()->addData(array(
80
+ 'firebug_warning_title' => $this->translate('Warning'),
81
+ 'firebug_warning_text' => $this->translate($warn),
82
+ 'firebug_warning_anchor' => $this->translate('Hide'),
83
+ ));
84
+
85
+ $translatedString = array(
86
+ 'Insert Image...' => $this->translate('Insert Image...'),
87
+ 'Insert Media...' => $this->translate('Insert Media...'),
88
+ 'Insert File...' => $this->translate('Insert File...')
89
+ );
90
+
91
+ $jsSetupObject = 'wysiwyg' . $this->getHtmlId();
92
+
93
+ $forceLoad = '';
94
+ if (!$this->isHidden()) {
95
+ if ($this->getForceLoad()) {
96
+ $forceLoad = $jsSetupObject . '.setup("exact");';
97
+ } else {
98
+ $forceLoad = 'Event.observe(window, "load", '
99
+ . $jsSetupObject . '.setup.bind(' . $jsSetupObject . ', "exact"));';
100
+ }
101
+ }
102
+
103
+ $html = $this->_getButtonsHtml()
104
+ . '<textarea name="' . $this->getName() . '" title="' . $this->getTitle()
105
+ . '" id="' . $this->getHtmlId() . '"'
106
+ . ' class="textarea ' . $this->getClass() . '" '
107
+ . $this->serialize($this->getHtmlAttributes()) . ' >' . $this->getEscapedValue() . '</textarea>'
108
+ . $js . '
109
+ <script type="text/javascript">
110
+ //<![CDATA[
111
+ if ("undefined" != typeof(Translator)) {
112
+ Translator.add(' . Zend_Json::encode($translatedString) . ');
113
+ }'
114
+ . $jsSetupObject . ' = new tinyMceWysiwygSetup("' . $this->getHtmlId() . '", '
115
+ . Zend_Json::encode($this->getConfig()).');'
116
+ . $forceLoad.'
117
+ editorFormValidationHandler = ' . $jsSetupObject . '.onFormValidation.bind(' . $jsSetupObject . ');
118
+ Event.observe("toggle' . $this->getHtmlId() . '", "click", '
119
+ . $jsSetupObject . '.toggle.bind('.$jsSetupObject.'));
120
+ varienGlobalEvents.attachEventHandler("formSubmit", editorFormValidationHandler);
121
+ varienGlobalEvents.attachEventHandler("tinymceBeforeSetContent", '
122
+ . $jsSetupObject . '.beforeSetContent.bind(' . $jsSetupObject . '));
123
+ varienGlobalEvents.attachEventHandler("tinymceSaveContent", '
124
+ . $jsSetupObject . '.saveContent.bind(' . $jsSetupObject . '));
125
+ varienGlobalEvents.clearEventHandlers("open_browser_callback");
126
+ varienGlobalEvents.attachEventHandler("open_browser_callback", '
127
+ . $jsSetupObject . '.openFileBrowser.bind(' . $jsSetupObject . '));
128
+ //]]>
129
+ </script>';
130
+
131
+ $html = $this->_wrapIntoContainer($html);
132
+ $html .= $this->getAfterElementHtml();
133
+ return $html;
134
+ } else {
135
+ // Display only buttons to additional features
136
+ if ($this->getConfig('widget_window_url')) {
137
+ $html = $this->_getButtonsHtml() . $js . parent::getElementHtml();
138
+ $html = $this->_wrapIntoContainer($html);
139
+ return $html;
140
+ }
141
+ return parent::getElementHtml();
142
+ }
143
+ }
144
+
145
+ public function getTheme()
146
+ {
147
+ if(!$this->hasData('theme')) {
148
+ return 'simple';
149
+ }
150
+
151
+ return $this->_getData('theme');
152
+ }
153
+
154
+ /**
155
+ * Return Editor top Buttons HTML
156
+ *
157
+ * @return string
158
+ */
159
+ protected function _getButtonsHtml()
160
+ {
161
+ $buttonsHtml = '<div id="buttons'.$this->getHtmlId().'" class="buttons-set">';
162
+ if ($this->isEnabled()) {
163
+ $buttonsHtml .= $this->_getToggleButtonHtml() . $this->_getPluginButtonsHtml($this->isHidden());
164
+ } else {
165
+ $buttonsHtml .= $this->_getPluginButtonsHtml(true);
166
+ }
167
+ $buttonsHtml .= '</div>';
168
+
169
+ return $buttonsHtml;
170
+ }
171
+
172
+ /**
173
+ * Return HTML button to toggling WYSIWYG
174
+ *
175
+ * @return string
176
+ */
177
+ protected function _getToggleButtonHtml($visible = true)
178
+ {
179
+ $html = $this->_getButtonHtml(array(
180
+ 'title' => $this->translate('Show / Hide Editor'),
181
+ 'class' => 'show-hide',
182
+ 'style' => $visible ? '' : 'display:none',
183
+ 'id' => 'toggle'.$this->getHtmlId(),
184
+ ));
185
+ return $html;
186
+ }
187
+
188
+ /**
189
+ * Prepare Html buttons for additional WYSIWYG features
190
+ *
191
+ * @param bool $visible Display button or not
192
+ * @return void
193
+ */
194
+ protected function _getPluginButtonsHtml($visible = true)
195
+ {
196
+ $buttonsHtml = '';
197
+
198
+ // Button to widget insertion window
199
+ if ($this->getConfig('add_widgets')) {
200
+ $buttonsHtml .= $this->_getButtonHtml(array(
201
+ 'title' => $this->translate('Insert Widget...'),
202
+ 'onclick' => "widgetTools.openDialog('" . $this->getConfig('widget_window_url') . "widget_target_id/"
203
+ . $this->getHtmlId() . "')",
204
+ 'class' => 'add-widget plugin',
205
+ 'style' => $visible ? '' : 'display:none',
206
+ ));
207
+ }
208
+
209
+ // Button to media images insertion window
210
+ if ($this->getConfig('add_images')) {
211
+ $buttonsHtml .= $this->_getButtonHtml(array(
212
+ 'title' => $this->translate('Insert Image...'),
213
+ 'onclick' => "MediabrowserUtility.openDialog('" .
214
+ $this->getConfig('files_browser_window_url') .
215
+ "target_element_id/" . $this->getHtmlId() . "/" .
216
+ ((null !== $this->getConfig('store_id'))
217
+ ? ('store/' . $this->getConfig('store_id') . '/')
218
+ : '')
219
+ . "')",
220
+ 'class' => 'add-image plugin',
221
+ 'style' => $visible ? '' : 'display:none',
222
+ ));
223
+ }
224
+
225
+ foreach ($this->getConfig('plugins') as $plugin) {
226
+ if (isset($plugin['options']) && $this->_checkPluginButtonOptions($plugin['options'])) {
227
+ $buttonOptions = $this->_prepareButtonOptions($plugin['options']);
228
+ if (!$visible) {
229
+ $configStyle = '';
230
+ if (isset($buttonOptions['style'])) {
231
+ $configStyle = $buttonOptions['style'];
232
+ }
233
+ $buttonOptions = array_merge($buttonOptions, array('style' => 'display:none;' . $configStyle));
234
+ }
235
+ $buttonsHtml .= $this->_getButtonHtml($buttonOptions);
236
+ }
237
+ }
238
+
239
+ return $buttonsHtml;
240
+ }
241
+
242
+ /**
243
+ * Prepare button options array to create button html
244
+ *
245
+ * @param array $options
246
+ * @return array
247
+ */
248
+ protected function _prepareButtonOptions($options)
249
+ {
250
+ $buttonOptions = array();
251
+ $buttonOptions['class'] = 'plugin';
252
+ foreach ($options as $name => $value) {
253
+ $buttonOptions[$name] = $value;
254
+ }
255
+ $buttonOptions = $this->_prepareOptions($buttonOptions);
256
+ return $buttonOptions;
257
+ }
258
+
259
+ /**
260
+ * Check if plugin button options have required values
261
+ *
262
+ * @param array $pluginOptions
263
+ * @return boolean
264
+ */
265
+ protected function _checkPluginButtonOptions($pluginOptions)
266
+ {
267
+ if (!isset($pluginOptions['title'])) {
268
+ return false;
269
+ }
270
+ return true;
271
+ }
272
+
273
+ /**
274
+ * Convert options by replacing template constructions ( like {{var_name}} )
275
+ * with data from this element object
276
+ *
277
+ * @param array $options
278
+ * @return array
279
+ */
280
+ protected function _prepareOptions($options)
281
+ {
282
+ $preparedOptions = array();
283
+ foreach ($options as $name => $value) {
284
+ if (is_array($value) && isset($value['search']) && isset($value['subject'])) {
285
+ $subject = $value['subject'];
286
+ foreach ($value['search'] as $part) {
287
+ $subject = str_replace('{{'.$part.'}}', $this->getDataUsingMethod($part), $subject);
288
+ }
289
+ $preparedOptions[$name] = $subject;
290
+ } else {
291
+ $preparedOptions[$name] = $value;
292
+ }
293
+ }
294
+ return $preparedOptions;
295
+ }
296
+
297
+ /**
298
+ * Return custom button HTML
299
+ *
300
+ * @param array $data Button params
301
+ * @return string
302
+ */
303
+ protected function _getButtonHtml($data)
304
+ {
305
+ $html = '<button type="button"';
306
+ $html.= ' class="scalable '.(isset($data['class']) ? $data['class'] : '').'"';
307
+ $html.= isset($data['onclick']) ? ' onclick="'.$data['onclick'].'"' : '';
308
+ $html.= isset($data['style']) ? ' style="'.$data['style'].'"' : '';
309
+ $html.= isset($data['id']) ? ' id="'.$data['id'].'"' : '';
310
+ $html.= '>';
311
+ $html.= isset($data['title']) ? '<span>'.$data['title'].'</span>' : '';
312
+ $html.= '</button>';
313
+
314
+ return $html;
315
+ }
316
+
317
+ /**
318
+ * Wraps Editor HTML into div if 'use_container' config option is set to true
319
+ * If 'no_display' config option is set to true, the div will be invisible
320
+ *
321
+ * @param string $html HTML code to wrap
322
+ * @return string
323
+ */
324
+ protected function _wrapIntoContainer($html)
325
+ {
326
+ if (!$this->getConfig('use_container')) {
327
+ return $html;
328
+ }
329
+
330
+ $html = '<div id="editor'.$this->getHtmlId().'"'
331
+ . ($this->getConfig('no_display') ? ' style="display:none;"' : '')
332
+ . ($this->getConfig('container_class') ? ' class="' . $this->getConfig('container_class') . '"' : '')
333
+ . '>'
334
+ . $html
335
+ . '</div>';
336
+
337
+ return $html;
338
+ }
339
+
340
+ /**
341
+ * Editor config retriever
342
+ *
343
+ * @param string $key Config var key
344
+ * @return mixed
345
+ */
346
+ public function getConfig($key = null)
347
+ {
348
+ if ( !($this->_getData('config') instanceof Varien_Object) ) {
349
+ $config = new Varien_Object();
350
+ $this->setConfig($config);
351
+ }
352
+ if ($key !== null) {
353
+ return $this->_getData('config')->getData($key);
354
+ }
355
+ return $this->_getData('config');
356
+ }
357
+
358
+ /**
359
+ * Translate string using defined helper
360
+ *
361
+ * @param string $string String to be translated
362
+ * @return string
363
+ */
364
+ public function translate($string)
365
+ {
366
+ $translator = $this->getConfig('translator');
367
+ if (is_object($translator) && is_callable(array($translator, '__'))) {
368
+ $result = $translator->__($string);
369
+ if (is_string($result)) {
370
+ return $result;
371
+ }
372
+ }
373
+
374
+ return $string;
375
+ }
376
+
377
+ /**
378
+ * Check whether Wysiwyg is enabled or not
379
+ *
380
+ * @return bool
381
+ */
382
+ public function isEnabled()
383
+ {
384
+ if ($this->hasData('wysiwyg')) {
385
+ return $this->getWysiwyg();
386
+ }
387
+ return $this->getConfig('enabled');
388
+ }
389
+
390
+ /**
391
+ * Check whether Wysiwyg is loaded on demand or not
392
+ *
393
+ * @return bool
394
+ */
395
+ public function isHidden()
396
+ {
397
+ return $this->getConfig('hidden');
398
+ }
399
+ }
lib/Varien/Data/Form/Element/Fieldset.php ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Form fieldset
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Element_Fieldset extends Varien_Data_Form_Element_Abstract
36
+ {
37
+ /**
38
+ * Sort child elements by specified data key
39
+ *
40
+ * @var string
41
+ */
42
+ protected $_sortChildrenByKey = '';
43
+
44
+ /**
45
+ * Children sort direction
46
+ *
47
+ * @var int
48
+ */
49
+ protected $_sortChildrenDirection = SORT_ASC;
50
+
51
+ /**
52
+ * Enter description here...
53
+ *
54
+ * @param array $attributes
55
+ */
56
+ public function __construct($attributes=array())
57
+ {
58
+ parent::__construct($attributes);
59
+ $this->_renderer = Varien_Data_Form::getFieldsetRenderer();
60
+ $this->setType('fieldset');
61
+ }
62
+
63
+ /**
64
+ * Enter description here...
65
+ *
66
+ * @return string
67
+ */
68
+ public function getElementHtml()
69
+ {
70
+ $html = '<fieldset id="'.$this->getHtmlId().'"'.$this->serialize(array('class')).'>'."\n";
71
+ if ($this->getLegend()) {
72
+ $html.= '<legend>'.$this->getLegend().'</legend>'."\n";
73
+ }
74
+ $html.= $this->getChildrenHtml();
75
+ $html.= '</fieldset></div>'."\n";
76
+ $html.= $this->getAfterElementHtml();
77
+ return $html;
78
+ }
79
+
80
+ /**
81
+ * Enter description here...
82
+ *
83
+ * @return string
84
+ */
85
+ public function getChildrenHtml()
86
+ {
87
+ $html = '';
88
+ foreach ($this->getSortedElements() as $element) {
89
+ if ($element->getType() != 'fieldset') {
90
+ $html.= $element->toHtml();
91
+ }
92
+ }
93
+ return $html;
94
+ }
95
+
96
+ /**
97
+ * Enter description here...
98
+ *
99
+ * @return string
100
+ */
101
+ public function getSubFieldsetHtml()
102
+ {
103
+ $html = '';
104
+ foreach ($this->getSortedElements() as $element) {
105
+ if ($element->getType() == 'fieldset') {
106
+ $html.= $element->toHtml();
107
+ }
108
+ }
109
+ return $html;
110
+ }
111
+
112
+ /**
113
+ * Enter description here...
114
+ *
115
+ * @return string
116
+ */
117
+ public function getDefaultHtml()
118
+ {
119
+ $html = '<div><h4 class="icon-head head-edit-form fieldset-legend">'.$this->getLegend().'</h4>'."\n";
120
+ $html.= $this->getElementHtml();
121
+ return $html;
122
+ }
123
+
124
+ /**
125
+ * Enter description here...
126
+ *
127
+ * @param string $elementId
128
+ * @param string $type
129
+ * @param array $config
130
+ * @param boolean $after
131
+ * @return Varien_Data_Form_Element_Abstract
132
+ */
133
+ public function addField($elementId, $type, $config, $after=false)
134
+ {
135
+ $element = parent::addField($elementId, $type, $config, $after);
136
+ if ($renderer = Varien_Data_Form::getFieldsetElementRenderer()) {
137
+ $element->setRenderer($renderer);
138
+ }
139
+ return $element;
140
+ }
141
+
142
+ /**
143
+ * Commence sorting elements by values by specified data key
144
+ *
145
+ * @param string $key
146
+ * @param int $direction
147
+ * @return Varien_Data_Form_Element_Fieldset
148
+ */
149
+ public function setSortElementsByAttribute($key, $direction = SORT_ASC)
150
+ {
151
+ $this->_sortChildrenByKey = $key;
152
+ $this->_sortDirection = $direction;
153
+ return $this;
154
+ }
155
+
156
+ /**
157
+ * Get sorted elements as array
158
+ *
159
+ * @return array
160
+ */
161
+ public function getSortedElements()
162
+ {
163
+ $elements = array();
164
+ // sort children by value by specified key
165
+ if ($this->_sortChildrenByKey) {
166
+ $sortKey = $this->_sortChildrenByKey;
167
+ $uniqueIncrement = 0; // in case if there are elements with same values
168
+ foreach ($this->getElements() as $e) {
169
+ $key = '_' . $uniqueIncrement;
170
+ if ($e->hasData($sortKey)) {
171
+ $key = $e->getDataUsingMethod($sortKey) . $key;
172
+ }
173
+ $elements[$key] = $e;
174
+ $uniqueIncrement++;
175
+ }
176
+ ksort($elements, $this->_sortChildrenDirection);
177
+ $elements = array_values($elements);
178
+ }
179
+ else {
180
+ foreach ($this->getElements() as $element) {
181
+ $elements[] = $element;
182
+ }
183
+ }
184
+ return $elements;
185
+ }
186
+ }
lib/Varien/Data/Form/Element/File.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form file element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_File extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('file');
40
+ $this->setExtType('file');
41
+ }
42
+ }
lib/Varien/Data/Form/Element/Gallery.php ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Category form input image element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Gallery extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($data)
37
+ {
38
+ parent::__construct($data);
39
+ $this->setType('file');
40
+ }
41
+
42
+ public function getElementHtml()
43
+ {
44
+ $gallery = $this->getValue();
45
+
46
+ $html = '<table id="gallery" class="gallery" border="0" cellspacing="3" cellpadding="0">';
47
+ $html .= '<thead id="gallery_thead" class="gallery"><tr class="gallery"><td class="gallery" valign="middle" align="center">Big Image</td><td class="gallery" valign="middle" align="center">Thumbnail</td><td class="gallery" valign="middle" align="center">Small Thumb</td><td class="gallery" valign="middle" align="center">Sort Order</td><td class="gallery" valign="middle" align="center">Delete</td></tr></thead>';
48
+ $widgetButton = $this->getForm()->getParent()->getLayout();
49
+ $buttonHtml = $widgetButton->createBlock('adminhtml/widget_button')
50
+ ->setData(
51
+ array(
52
+ 'label' => 'Add New Image',
53
+ 'onclick' => 'addNewImg()',
54
+ 'class' => 'add'))
55
+ ->toHtml();
56
+
57
+ $html .= '<tfoot class="gallery">';
58
+ $html .= '<tr class="gallery">';
59
+ $html .= '<td class="gallery" valign="middle" align="left" colspan="5">'.$buttonHtml.'</td>';
60
+ $html .= '</tr>';
61
+ $html .= '</tfoot>';
62
+
63
+ $html .= '<tbody class="gallery">';
64
+
65
+ $i = 0;
66
+ if (!is_null($this->getValue())) {
67
+ foreach ($this->getValue() as $image) {
68
+ $i++;
69
+ $html .= '<tr class="gallery">';
70
+ foreach ($this->getValue()->getAttributeBackend()->getImageTypes() as $type) {
71
+ $url = $image->setType($type)->getSourceUrl();
72
+ $html .= '<td class="gallery" align="center" style="vertical-align:bottom;">';
73
+ $html .= '<a href="'.$url.'" target="_blank" onclick="imagePreview(\''.$this->getHtmlId().'_image_'.$type.'_'.$image->getValueId().'\');return false;">
74
+ <img id="'.$this->getHtmlId().'_image_'.$type.'_'.$image->getValueId().'" src="'.$url.'" alt="'.$image->getValue().'" height="25" align="absmiddle" class="small-image-preview"></a><br/>';
75
+ $html .= '<input type="file" name="'.$this->getName().'_'.$type.'['.$image->getValueId().']" size="1"></td>';
76
+ }
77
+ $html .= '<td class="gallery" align="center" style="vertical-align:bottom;"><input type="input" name="'.parent::getName().'[position]['.$image->getValueId().']" value="'.$image->getPosition().'" id="'.$this->getHtmlId().'_position_'.$image->getValueId().'" size="3"/></td>';
78
+ $html .= '<td class="gallery" align="center" style="vertical-align:bottom;"><input type="checkbox" name="'.parent::getName().'[delete]['.$image->getValueId().']" value="'.$image->getValueId().'" id="'.$this->getHtmlId().'_delete_'.$image->getValueId().'"/></td>';
79
+ $html .= '</tr>';
80
+ }
81
+ }
82
+ if ($i==0) {
83
+ $html .= '<script type="text/javascript">document.getElementById("gallery_thead").style.visibility="hidden";</script>';
84
+ }
85
+
86
+ $html .= '</tbody></table>';
87
+
88
+ /*
89
+ $html .= '<script language="javascript">
90
+ var multi_selector = new MultiSelector( document.getElementById( "gallery" ),
91
+ "'.$this->getName().'",
92
+ -1,
93
+ \'<a href="file:///%file%" target="_blank" onclick="imagePreview(\\\''.$this->getHtmlId().'_image_new_%id%\\\');return false;"><img src="file:///%file%" width="50" align="absmiddle" class="small-image-preview" style="padding-bottom:3px; width:"></a> <div id="'.$this->getHtmlId().'_image_new_%id%" style="display:none" class="image-preview"><img src="file:///%file%"></div>\',
94
+ "",
95
+ \'<input type="file" name="'.parent::getName().'[new_image][%id%][%j%]" size="1" />\'
96
+ );
97
+ multi_selector.addElement( document.getElementById( "'.$this->getHtmlId().'" ) );
98
+ </script>';
99
+ */
100
+
101
+ $name = $this->getName();
102
+ $parentName = parent::getName();
103
+
104
+ $html .= <<<EndSCRIPT
105
+
106
+ <script language="javascript">
107
+ id = 0;
108
+
109
+ function addNewImg(){
110
+
111
+ document.getElementById("gallery_thead").style.visibility="visible";
112
+
113
+ id--;
114
+ new_file_input = '<input type="file" name="{$name}_%j%[%id%]" size="1" />';
115
+
116
+ // Sort order input
117
+ var new_row_input = document.createElement( 'input' );
118
+ new_row_input.type = 'text';
119
+ new_row_input.name = '{$parentName}[position]['+id+']';
120
+ new_row_input.size = '3';
121
+ new_row_input.value = '0';
122
+
123
+ // Delete button
124
+ var new_row_button = document.createElement( 'input' );
125
+ new_row_button.type = 'checkbox';
126
+ new_row_button.value = 'Delete';
127
+
128
+ table = document.getElementById( "gallery" );
129
+
130
+ // no of rows in the table:
131
+ noOfRows = table.rows.length;
132
+
133
+ // no of columns in the pre-last row:
134
+ noOfCols = table.rows[noOfRows-2].cells.length;
135
+
136
+ // insert row at pre-last:
137
+ var x=table.insertRow(noOfRows-1);
138
+
139
+ // insert cells in row.
140
+ for (var j = 0; j < noOfCols; j++) {
141
+
142
+ newCell = x.insertCell(j);
143
+ newCell.align = "center";
144
+ newCell.valign = "middle";
145
+
146
+ if (j==3) {
147
+ newCell.appendChild( new_row_input );
148
+ }
149
+ else if (j==4) {
150
+ newCell.appendChild( new_row_button );
151
+ }
152
+ else {
153
+ newCell.innerHTML = new_file_input.replace(/%j%/g, j).replace(/%id%/g, id);
154
+ }
155
+
156
+ }
157
+
158
+ // Delete function
159
+ new_row_button.onclick= function(){
160
+
161
+ this.parentNode.parentNode.parentNode.removeChild( this.parentNode.parentNode );
162
+
163
+ // Appease Safari
164
+ // without it Safari wants to reload the browser window
165
+ // which nixes your already queued uploads
166
+ return false;
167
+ };
168
+
169
+ }
170
+ </script>
171
+
172
+ EndSCRIPT;
173
+ $html.= $this->getAfterElementHtml();
174
+ return $html;
175
+ }
176
+
177
+ public function getName()
178
+ {
179
+ return $this->getData('name');
180
+ }
181
+
182
+ public function getParentName()
183
+ {
184
+ return parent::getName();
185
+ }
186
+ }
lib/Varien/Data/Form/Element/Hidden.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form hidden element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Hidden extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('hidden');
40
+ $this->setExtType('hiddenfield');
41
+ }
42
+
43
+ public function getDefaultHtml()
44
+ {
45
+ $html = $this->getData('default_html');
46
+ if (is_null($html)) {
47
+ $html = $this->getElementHtml();
48
+ }
49
+ return $html;
50
+ }
51
+ }
lib/Varien/Data/Form/Element/Image.php ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Category form input image element
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Element_Image extends Varien_Data_Form_Element_Abstract
36
+ {
37
+
38
+ /**
39
+ * Enter description here...
40
+ *
41
+ * @param array $data
42
+ */
43
+ public function __construct($data)
44
+ {
45
+ parent::__construct($data);
46
+ $this->setType('file');
47
+ }
48
+
49
+ /**
50
+ * Enter description here...
51
+ *
52
+ * @return string
53
+ */
54
+ public function getElementHtml()
55
+ {
56
+ $html = '';
57
+
58
+ if ($this->getValue()) {
59
+ $url = $this->_getUrl();
60
+
61
+ if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
62
+ $url = Mage::getBaseUrl('media') . $url;
63
+ }
64
+
65
+ $html = '<a href="'.$url.'" onclick="imagePreview(\''.$this->getHtmlId().'_image\'); return false;"><img src="'.$url.'" id="'.$this->getHtmlId().'_image" title="'.$this->getValue().'" alt="'.$this->getValue().'" height="22" width="22" class="small-image-preview v-middle" /></a> ';
66
+ }
67
+ $this->setClass('input-file');
68
+ $html.= parent::getElementHtml();
69
+ $html.= $this->_getDeleteCheckbox();
70
+
71
+ return $html;
72
+ }
73
+
74
+ /**
75
+ * Enter description here...
76
+ *
77
+ * @return string
78
+ */
79
+ protected function _getDeleteCheckbox()
80
+ {
81
+ $html = '';
82
+ if ($this->getValue()) {
83
+ $label = Mage::helper('core')->__('Delete Image');
84
+ $html .= '<span class="delete-image">';
85
+ $html .= '<input type="checkbox" name="'.parent::getName().'[delete]" value="1" class="checkbox" id="'.$this->getHtmlId().'_delete"'.($this->getDisabled() ? ' disabled="disabled"': '').'/>';
86
+ $html .= '<label for="'.$this->getHtmlId().'_delete"'.($this->getDisabled() ? ' class="disabled"' : '').'> '.$label.'</label>';
87
+ $html .= $this->_getHiddenInput();
88
+ $html .= '</span>';
89
+ }
90
+
91
+ return $html;
92
+ }
93
+
94
+ /**
95
+ * Enter description here...
96
+ *
97
+ * @return string
98
+ */
99
+ protected function _getHiddenInput()
100
+ {
101
+ return '<input type="hidden" name="'.parent::getName().'[value]" value="'.$this->getValue().'" />';
102
+ }
103
+
104
+ /**
105
+ * Get image preview url
106
+ *
107
+ * @return string
108
+ */
109
+ protected function _getUrl()
110
+ {
111
+ return $this->getValue();
112
+ }
113
+
114
+ /**
115
+ * Enter description here...
116
+ *
117
+ * @return string
118
+ */
119
+ public function getName()
120
+ {
121
+ return $this->getData('name');
122
+ }
123
+
124
+ }
lib/Varien/Data/Form/Element/Imagefile.php ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form image file element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Imagefile extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('file');
40
+ $this->setExtType('imagefile');
41
+ $this->setAutosubmit(false);
42
+ $this->setData('autoSubmit', false);
43
+ //$this->setExtType('file');
44
+ }
45
+ }
lib/Varien/Data/Form/Element/Label.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Data form abstract class
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Label extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('label');
40
+ }
41
+
42
+ public function getElementHtml()
43
+ {
44
+ $html = $this->getBold() ? '<strong>' : '';
45
+ $html.= $this->getEscapedValue();
46
+ $html.= $this->getBold() ? '</strong>' : '';
47
+ $html.= $this->getAfterElementHtml();
48
+ return $html;
49
+ }
50
+
51
+ }
lib/Varien/Data/Form/Element/Link.php ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien Form element renderer to display link element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Link extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('link');
40
+ }
41
+
42
+ /**
43
+ * Generates element html
44
+ *
45
+ * @return string
46
+ */
47
+ public function getElementHtml()
48
+ {
49
+ $html = $this->getBeforeElementHtml();
50
+ $html .= '<a id="'.$this->getHtmlId().'" '.$this->serialize($this->getHtmlAttributes()).'>'. $this->getEscapedValue() . "</a>\n";
51
+ $html .= $this->getAfterElementHtml();
52
+ return $html;
53
+ }
54
+
55
+ /**
56
+ * Prepare array of anchor attributes
57
+ *
58
+ * @return array
59
+ */
60
+ public function getHtmlAttributes()
61
+ {
62
+ return array('charset', 'coords', 'href', 'hreflang', 'rel', 'rev', 'name',
63
+ 'shape', 'target', 'accesskey', 'class', 'dir', 'lang', 'style',
64
+ 'tabindex', 'title', 'xml:lang', 'onblur', 'onclick', 'ondblclick',
65
+ 'onfocus', 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover',
66
+ 'onmouseup', 'onkeydown', 'onkeypress', 'onkeyup');
67
+ }
68
+ }
lib/Varien/Data/Form/Element/Multiline.php ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form multiline text elements
29
+ *
30
+ * @author Magento Core Team <core@magentocommerce.com>
31
+ */
32
+ class Varien_Data_Form_Element_Multiline extends Varien_Data_Form_Element_Abstract
33
+ {
34
+ public function __construct($attributes=array())
35
+ {
36
+ parent::__construct($attributes);
37
+ $this->setType('text');
38
+ $this->setLineCount(2);
39
+ }
40
+
41
+ public function getHtmlAttributes()
42
+ {
43
+ return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'maxlength');
44
+ }
45
+
46
+ public function getLabelHtml($suffix = 0)
47
+ {
48
+ return parent::getLabelHtml($suffix);
49
+ }
50
+
51
+ /**
52
+ * Get element HTML
53
+ *
54
+ * @return string
55
+ */
56
+ public function getElementHtml()
57
+ {
58
+ $html = '';
59
+ $lineCount = $this->getLineCount();
60
+
61
+ for ($i = 0; $i < $lineCount; $i++) {
62
+ if ($i == 0 && $this->getRequired()) {
63
+ $this->setClass('input-text required-entry');
64
+ } else {
65
+ $this->setClass('input-text');
66
+ }
67
+ $html .= '<div class="multi-input"><input id="' . $this->getHtmlId() . $i . '" name="' . $this->getName()
68
+ . '[' . $i . ']' . '" value="' . $this->getEscapedValue($i) . '" '
69
+ . $this->serialize($this->getHtmlAttributes()) . ' />' . "\n";
70
+ if ($i==0) {
71
+ $html .= $this->getAfterElementHtml();
72
+ }
73
+ $html .= '</div>';
74
+ }
75
+ return $html;
76
+ }
77
+
78
+ public function getDefaultHtml()
79
+ {
80
+ $html = '';
81
+ $lineCount = $this->getLineCount();
82
+
83
+ for ($i=0; $i<$lineCount; $i++){
84
+ $html.= ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
85
+ if ($i==0) {
86
+ $html.= '<label for="'.$this->getHtmlId().$i.'">'.$this->getLabel()
87
+ .( $this->getRequired() ? ' <span class="required">*</span>' : '' ).'</label>'."\n";
88
+ if($this->getRequired()){
89
+ $this->setClass('input-text required-entry');
90
+ }
91
+ }
92
+ else {
93
+ $this->setClass('input-text');
94
+ $html.= '<label>&nbsp;</label>'."\n";
95
+ }
96
+ $html.= '<input id="'.$this->getHtmlId().$i.'" name="'.$this->getName().'['.$i.']'
97
+ .'" value="'.$this->getEscapedValue($i).'"'.$this->serialize($this->getHtmlAttributes()).' />'."\n";
98
+ if ($i==0) {
99
+ $html.= $this->getAfterElementHtml();
100
+ }
101
+ $html.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
102
+ }
103
+ return $html;
104
+ }
105
+ }
lib/Varien/Data/Form/Element/Multiselect.php ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form select element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Multiselect extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('select');
40
+ $this->setExtType('multiple');
41
+ $this->setSize(10);
42
+ }
43
+
44
+ public function getName()
45
+ {
46
+ $name = parent::getName();
47
+ if (strpos($name, '[]') === false) {
48
+ $name.= '[]';
49
+ }
50
+ return $name;
51
+ }
52
+
53
+ public function getElementHtml()
54
+ {
55
+ $this->addClass('select multiselect');
56
+ $html = '';
57
+ if ($this->getCanBeEmpty() && empty($this->_data['disabled'])) {
58
+ $html .= '<input type="hidden" name="' . parent::getName() . '" value="" />';
59
+ }
60
+ $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' .
61
+ $this->serialize($this->getHtmlAttributes()) . ' multiple="multiple">' . "\n";
62
+
63
+ $value = $this->getValue();
64
+ if (!is_array($value)) {
65
+ $value = explode(',', $value);
66
+ }
67
+
68
+ if ($values = $this->getValues()) {
69
+ foreach ($values as $option) {
70
+ if (is_array($option['value'])) {
71
+ $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
72
+ foreach ($option['value'] as $groupItem) {
73
+ $html .= $this->_optionToHtml($groupItem, $value);
74
+ }
75
+ $html .= '</optgroup>' . "\n";
76
+ }
77
+ else {
78
+ $html .= $this->_optionToHtml($option, $value);
79
+ }
80
+ }
81
+ }
82
+
83
+ $html .= '</select>' . "\n";
84
+ $html .= $this->getAfterElementHtml();
85
+
86
+ return $html;
87
+ }
88
+
89
+ public function getHtmlAttributes()
90
+ {
91
+ return array('title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'size', 'tabindex');
92
+ }
93
+
94
+ public function getDefaultHtml()
95
+ {
96
+ $result = ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
97
+ $result.= $this->getLabelHtml();
98
+ $result.= $this->getElementHtml();
99
+
100
+
101
+ if($this->getSelectAll() && $this->getDeselectAll()) {
102
+ $result .= '<a href="#" onclick="return ' . $this->getJsObjectName() . '.selectAll()">' .
103
+ $this->getSelectAll() . '</a> <span class="separator">&nbsp;|&nbsp;</span>';
104
+ $result .= '<a href="#" onclick="return ' . $this->getJsObjectName() . '.deselectAll()">' .
105
+ $this->getDeselectAll() . '</a>';
106
+ }
107
+
108
+ $result.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
109
+
110
+
111
+ $result.= '<script type="text/javascript">' . "\n";
112
+ $result.= ' var ' . $this->getJsObjectName() . ' = {' . "\n";
113
+ $result.= ' selectAll: function() { ' . "\n";
114
+ $result.= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
115
+ $result.= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
116
+ $result.= ' sel.options[i].selected = true; ' . "\n";
117
+ $result.= ' } ' . "\n";
118
+ $result.= ' return false; ' . "\n";
119
+ $result.= ' },' . "\n";
120
+ $result.= ' deselectAll: function() {' . "\n";
121
+ $result.= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
122
+ $result.= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
123
+ $result.= ' sel.options[i].selected = false; ' . "\n";
124
+ $result.= ' } ' . "\n";
125
+ $result.= ' return false; ' . "\n";
126
+ $result.= ' }' . "\n";
127
+ $result.= ' }' . "\n";
128
+ $result.= "\n" . '</script>';
129
+
130
+ return $result;
131
+ }
132
+
133
+ public function getJsObjectName() {
134
+ return $this->getHtmlId() . 'ElementControl';
135
+ }
136
+
137
+ protected function _optionToHtml($option, $selected)
138
+ {
139
+ $html = '<option value="'.$this->_escape($option['value']).'"';
140
+ $html.= isset($option['title']) ? 'title="'.$this->_escape($option['title']).'"' : '';
141
+ $html.= isset($option['style']) ? 'style="'.$option['style'].'"' : '';
142
+ if (in_array((string)$option['value'], $selected)) {
143
+ $html.= ' selected="selected"';
144
+ }
145
+ $html.= '>'.$this->_escape($option['label']). '</option>'."\n";
146
+ return $html;
147
+ }
148
+ }
lib/Varien/Data/Form/Element/Note.php ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form note element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Note extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('note');
40
+ //$this->setExtType('textfield');
41
+ }
42
+
43
+ public function getElementHtml()
44
+ {
45
+ $html = '<span id="' . $this->getHtmlId() . '">' . $this->getText() . '</span>';
46
+ $html.= $this->getAfterElementHtml();
47
+ return $html;
48
+ }
49
+ }
lib/Varien/Data/Form/Element/Obscure.php ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form text element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Obscure extends Varien_Data_Form_Element_Password
35
+ {
36
+ /**
37
+ * @var string
38
+ */
39
+ protected $_obscuredValue = '******';
40
+
41
+ /**
42
+ * Hide value to make sure it will not show in HTML
43
+ *
44
+ * @param string $index
45
+ * @return string
46
+ */
47
+ public function getEscapedValue($index = null)
48
+ {
49
+ $value = parent::getEscapedValue($index);
50
+ if (!empty($value)) {
51
+ return $this->_obscuredValue;
52
+ }
53
+ return $value;
54
+ }
55
+
56
+ /**
57
+ * Returns list of html attributes possible to output in HTML
58
+ *
59
+ * @return array()
60
+ */
61
+ public function getHtmlAttributes()
62
+ {
63
+ return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'onkeyup', 'disabled', 'readonly', 'maxlength', 'tabindex');
64
+ }
65
+ }
lib/Varien/Data/Form/Element/Password.php ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form password element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Password extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('password');
40
+ $this->setExtType('textfield');
41
+ }
42
+
43
+ public function getHtml()
44
+ {
45
+ $this->addClass('input-text');
46
+ return parent::getHtml();
47
+ }
48
+ }
lib/Varien/Data/Form/Element/Radio.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form radio element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Radio extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('radio');
40
+ $this->setExtType('radio');
41
+ }
42
+ }
lib/Varien/Data/Form/Element/Radios.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Radio buttons collection
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Radios extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('radios');
40
+ }
41
+
42
+ public function getSeparator()
43
+ {
44
+ $separator = $this->getData('separator');
45
+ if (is_null($separator)) {
46
+ $separator = '&nbsp;';
47
+ }
48
+ return $separator;
49
+ }
50
+
51
+ public function getElementHtml()
52
+ {
53
+ $html = '';
54
+ $value = $this->getValue();
55
+ if ($values = $this->getValues()) {
56
+ foreach ($values as $option) {
57
+ $html.= $this->_optionToHtml($option, $value);
58
+ }
59
+ }
60
+ $html.= $this->getAfterElementHtml();
61
+ return $html;
62
+ }
63
+
64
+ protected function _optionToHtml($option, $selected)
65
+ {
66
+ $html = '<input type="radio"'.$this->serialize(array('name', 'class', 'style'));
67
+ if (is_array($option)) {
68
+ $html.= 'value="'.$this->_escape($option['value']).'" id="'.$this->getHtmlId().$option['value'].'"';
69
+ if ($option['value'] == $selected) {
70
+ $html.= ' checked="checked"';
71
+ }
72
+ $html.= ' />';
73
+ $html.= '<label class="inline" for="'.$this->getHtmlId().$option['value'].'">'.$option['label'].'</label>';
74
+ }
75
+ elseif ($option instanceof Varien_Object) {
76
+ $html.= 'id="'.$this->getHtmlId().$option->getValue().'"'.$option->serialize(array('label', 'title', 'value', 'class', 'style'));
77
+ if (in_array($option->getValue(), $selected)) {
78
+ $html.= ' checked="checked"';
79
+ }
80
+ $html.= ' />';
81
+ $html.= '<label class="inline" for="'.$this->getHtmlId().$option->getValue().'">'.$option->getLabel().'</label>';
82
+ }
83
+ $html.= $this->getSeparator() . "\n";
84
+ return $html;
85
+ }
86
+ }
lib/Varien/Data/Form/Element/Renderer/Interface.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form field renderer
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ interface Varien_Data_Form_Element_Renderer_Interface
35
+ {
36
+ public function render(Varien_Data_Form_Element_Abstract $element);
37
+ }
lib/Varien/Data/Form/Element/Reset.php ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form relset element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Reset extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('text');
40
+ $this->setExtType('textfield');
41
+ }
42
+ }
lib/Varien/Data/Form/Element/Select.php ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form select element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Select extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('select');
40
+ $this->setExtType('combobox');
41
+ $this->_prepareOptions();
42
+ }
43
+
44
+ public function getElementHtml()
45
+ {
46
+ $this->addClass('select');
47
+ $html = '<select id="'.$this->getHtmlId().'" name="'.$this->getName().'" '.$this->serialize($this->getHtmlAttributes()).'>'."\n";
48
+
49
+ $value = $this->getValue();
50
+ if (!is_array($value)) {
51
+ $value = array($value);
52
+ }
53
+
54
+ if ($values = $this->getValues()) {
55
+ foreach ($values as $key => $option) {
56
+ if (!is_array($option)) {
57
+ $html.= $this->_optionToHtml(array(
58
+ 'value' => $key,
59
+ 'label' => $option),
60
+ $value
61
+ );
62
+ }
63
+ elseif (is_array($option['value'])) {
64
+ $html.='<optgroup label="'.$option['label'].'">'."\n";
65
+ foreach ($option['value'] as $groupItem) {
66
+ $html.= $this->_optionToHtml($groupItem, $value);
67
+ }
68
+ $html.='</optgroup>'."\n";
69
+ }
70
+ else {
71
+ $html.= $this->_optionToHtml($option, $value);
72
+ }
73
+ }
74
+ }
75
+
76
+ $html.= '</select>'."\n";
77
+ $html.= $this->getAfterElementHtml();
78
+ return $html;
79
+ }
80
+
81
+ protected function _optionToHtml($option, $selected)
82
+ {
83
+ if (is_array($option['value'])) {
84
+ $html ='<optgroup label="'.$option['label'].'">'."\n";
85
+ foreach ($option['value'] as $groupItem) {
86
+ $html .= $this->_optionToHtml($groupItem, $selected);
87
+ }
88
+ $html .='</optgroup>'."\n";
89
+ }
90
+ else {
91
+ $html = '<option value="'.$this->_escape($option['value']).'"';
92
+ $html.= isset($option['title']) ? 'title="'.$this->_escape($option['title']).'"' : '';
93
+ $html.= isset($option['style']) ? 'style="'.$option['style'].'"' : '';
94
+ if (in_array($option['value'], $selected)) {
95
+ $html.= ' selected="selected"';
96
+ }
97
+ $html.= '>'.$this->_escape($option['label']). '</option>'."\n";
98
+ }
99
+ return $html;
100
+ }
101
+
102
+ protected function _prepareOptions()
103
+ {
104
+ $values = $this->getValues();
105
+ if (empty($values)) {
106
+ $options = $this->getOptions();
107
+ if (is_array($options)) {
108
+ $values = array();
109
+ foreach ($options as $value => $label) {
110
+ $values[] = array('value' => $value, 'label' => $label);
111
+ }
112
+ } elseif (is_string($options)) {
113
+ $values = array( array('value' => $options, 'label' => $options) );
114
+ }
115
+ $this->setValues($values);
116
+ }
117
+ }
118
+
119
+ public function getHtmlAttributes()
120
+ {
121
+ return array('title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly', 'tabindex');
122
+ }
123
+ }
lib/Varien/Data/Form/Element/Submit.php ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form submit element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Submit extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setExtType('submit');
40
+ $this->setType('submit');
41
+ }
42
+
43
+ public function getHtml()
44
+ {
45
+ $this->addClass('submit');
46
+ return parent::getHtml();
47
+ }
48
+ }
lib/Varien/Data/Form/Element/Text.php ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form text element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Text extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('text');
40
+ $this->setExtType('textfield');
41
+ }
42
+
43
+ public function getHtml()
44
+ {
45
+ $this->addClass('input-text');
46
+ return parent::getHtml();
47
+ }
48
+
49
+ public function getHtmlAttributes()
50
+ {
51
+ return array('type', 'title', 'class', 'style', 'onclick', 'onchange', 'onkeyup', 'disabled', 'readonly', 'maxlength', 'tabindex');
52
+ }
53
+ }
lib/Varien/Data/Form/Element/Textarea.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form textarea element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Textarea extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('textarea');
40
+ $this->setExtType('textarea');
41
+ $this->setRows(2);
42
+ $this->setCols(15);
43
+ }
44
+
45
+ public function getHtmlAttributes()
46
+ {
47
+ return array('title', 'class', 'style', 'onclick', 'onchange', 'rows', 'cols', 'readonly', 'disabled', 'onkeyup', 'tabindex');
48
+ }
49
+
50
+ public function getElementHtml()
51
+ {
52
+ $this->addClass('textarea');
53
+ $html = '<textarea id="'.$this->getHtmlId().'" name="'.$this->getName().'" '.$this->serialize($this->getHtmlAttributes()).' >';
54
+ $html .= $this->getEscapedValue();
55
+ $html .= "</textarea>";
56
+ $html .= $this->getAfterElementHtml();
57
+ return $html;
58
+ }
59
+ }
lib/Varien/Data/Form/Element/Time.php ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Form time element
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Form_Element_Time extends Varien_Data_Form_Element_Abstract
35
+ {
36
+ public function __construct($attributes=array())
37
+ {
38
+ parent::__construct($attributes);
39
+ $this->setType('time');
40
+ }
41
+
42
+ public function getName()
43
+ {
44
+ $name = parent::getName();
45
+ if (strpos($name, '[]') === false) {
46
+ $name.= '[]';
47
+ }
48
+ return $name;
49
+ }
50
+
51
+ public function getElementHtml()
52
+ {
53
+ $this->addClass('select');
54
+
55
+ $value_hrs = 0;
56
+ $value_min = 0;
57
+ $value_sec = 0;
58
+
59
+ if( $value = $this->getValue() ) {
60
+ $values = explode(',', $value);
61
+ if( is_array($values) && count($values) == 3 ) {
62
+ $value_hrs = $values[0];
63
+ $value_min = $values[1];
64
+ $value_sec = $values[2];
65
+ }
66
+ }
67
+
68
+ $html = '<input type="hidden" id="' . $this->getHtmlId() . '" />';
69
+ $html .= '<select name="'. $this->getName() . '" '.$this->serialize($this->getHtmlAttributes()).' style="width:40px">'."\n";
70
+ for( $i=0;$i<24;$i++ ) {
71
+ $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
72
+ $html.= '<option value="'.$hour.'" '. ( ($value_hrs == $i) ? 'selected="selected"' : '' ) .'>' . $hour . '</option>';
73
+ }
74
+ $html.= '</select>'."\n";
75
+
76
+ $html.= '&nbsp;:&nbsp;<select name="'. $this->getName() . '" '.$this->serialize($this->getHtmlAttributes()).' style="width:40px">'."\n";
77
+ for( $i=0;$i<60;$i++ ) {
78
+ $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
79
+ $html.= '<option value="'.$hour.'" '. ( ($value_min == $i) ? 'selected="selected"' : '' ) .'>' . $hour . '</option>';
80
+ }
81
+ $html.= '</select>'."\n";
82
+
83
+ $html.= '&nbsp;:&nbsp;<select name="'. $this->getName() . '" '.$this->serialize($this->getHtmlAttributes()).' style="width:40px">'."\n";
84
+ for( $i=0;$i<60;$i++ ) {
85
+ $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
86
+ $html.= '<option value="'.$hour.'" '. ( ($value_sec == $i) ? 'selected="selected"' : '' ) .'>' . $hour . '</option>';
87
+ }
88
+ $html.= '</select>'."\n";
89
+ $html.= $this->getAfterElementHtml();
90
+ return $html;
91
+ }
92
+ }
lib/Varien/Data/Form/Filter/Date.php ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Form Input/Output Strip HTML tags Filter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Filter_Date implements Varien_Data_Form_Filter_Interface
36
+ {
37
+ /**
38
+ * Date format
39
+ *
40
+ * @var string
41
+ */
42
+ protected $_dateFormat;
43
+
44
+ /**
45
+ * Local
46
+ *
47
+ * @var Zend_Locale
48
+ */
49
+ protected $_locale;
50
+
51
+ /**
52
+ * Initialize filter
53
+ *
54
+ * @param string $format Zend_Date input/output format
55
+ * @param Zend_Locale $locale
56
+ */
57
+ public function __construct($format = null, $locale = null)
58
+ {
59
+ if (is_null($format)) {
60
+ $format = Varien_Date::DATE_INTERNAL_FORMAT;
61
+ }
62
+ $this->_dateFormat = $format;
63
+ $this->_locale = $locale;
64
+ }
65
+
66
+ /**
67
+ * Returns the result of filtering $value
68
+ *
69
+ * @param string $value
70
+ * @return string
71
+ */
72
+ public function inputFilter($value)
73
+ {
74
+ $filterInput = new Zend_Filter_LocalizedToNormalized(array(
75
+ 'date_format' => $this->_dateFormat,
76
+ 'locale' => $this->_locale
77
+ ));
78
+ $filterInternal = new Zend_Filter_NormalizedToLocalized(array(
79
+ 'date_format' => Varien_Date::DATE_INTERNAL_FORMAT,
80
+ 'locale' => $this->_locale
81
+ ));
82
+
83
+ $value = $filterInput->filter($value);
84
+ $value = $filterInternal->filter($value);
85
+ return $value;
86
+ }
87
+
88
+ /**
89
+ * Returns the result of filtering $value
90
+ *
91
+ * @param string $value
92
+ * @return string
93
+ */
94
+ public function outputFilter($value)
95
+ {
96
+ $filterInput = new Zend_Filter_LocalizedToNormalized(array(
97
+ 'date_format' => Varien_Date::DATE_INTERNAL_FORMAT,
98
+ 'locale' => $this->_locale
99
+ ));
100
+ $filterInternal = new Zend_Filter_NormalizedToLocalized(array(
101
+ 'date_format' => $this->_dateFormat,
102
+ 'locale' => $this->_locale
103
+ ));
104
+
105
+ $value = $filterInput->filter($value);
106
+ $value = $filterInternal->filter($value);
107
+ return $value;
108
+ }
109
+ }
lib/Varien/Data/Form/Filter/Escapehtml.php ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Form Input/Output Escape HTML entities Filter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Filter_Escapehtml implements Varien_Data_Form_Filter_Interface
36
+ {
37
+ /**
38
+ * Returns the result of filtering $value
39
+ *
40
+ * @param string $value
41
+ * @return string
42
+ */
43
+ public function inputFilter($value)
44
+ {
45
+ return $value;
46
+ }
47
+
48
+ /**
49
+ * Returns the result of filtering $value
50
+ *
51
+ * @param string $value
52
+ * @return string
53
+ */
54
+ public function outputFilter($value)
55
+ {
56
+ return htmlspecialchars($value);
57
+ }
58
+ }
lib/Varien/Data/Form/Filter/Interface.php ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Form Input/Output Filter Interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Data_Form_Filter_Interface
36
+ {
37
+ /**
38
+ * Returns the result of filtering $value
39
+ *
40
+ * @param string $value
41
+ * @return string
42
+ */
43
+ public function inputFilter($value);
44
+
45
+ /**
46
+ * Returns the result of filtering $value
47
+ *
48
+ * @param string $value
49
+ * @return string
50
+ */
51
+ public function outputFilter($value);
52
+ }
lib/Varien/Data/Form/Filter/Striptags.php ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Form Input/Output Strip HTML tags Filter
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Form_Filter_Striptags implements Varien_Data_Form_Filter_Interface
36
+ {
37
+ /**
38
+ * Returns the result of filtering $value
39
+ *
40
+ * @param string $value
41
+ * @return string
42
+ */
43
+ public function inputFilter($value)
44
+ {
45
+ return strip_tags($value);
46
+ }
47
+
48
+ /**
49
+ * Returns the result of filtering $value
50
+ *
51
+ * @param string $value
52
+ * @return string
53
+ */
54
+ public function outputFilter($value)
55
+ {
56
+ return $value;
57
+ }
58
+ }
lib/Varien/Data/Tree.php ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Data tree
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Data
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Data_Tree
36
+ {
37
+
38
+ /**
39
+ * Nodes collection
40
+ *
41
+ * @var Varien_Data_Tree_Node_Collection
42
+ */
43
+ protected $_nodes;
44
+
45
+ /**
46
+ * Enter description here...
47
+ *
48
+ */
49
+ public function __construct()
50
+ {
51
+ $this->_nodes = new Varien_Data_Tree_Node_Collection($this);
52
+ }
53
+
54
+ /**
55
+ * Enter description here...
56
+ *
57
+ * @return Varien_Data_Tree
58
+ */
59
+ public function getTree()
60
+ {
61
+ return $this;
62
+ }
63
+
64
+ /**
65
+ * Enter description here...
66
+ *
67
+ * @param Varien_Data_Tree_Node $parentNode
68
+ */
69
+ public function load($parentNode=null)
70
+ {
71
+ }
72
+
73
+ /**
74
+ * Enter description here...
75
+ *
76
+ * @param unknown_type $nodeId
77
+ */
78
+ public function loadNode($nodeId)
79
+ {
80
+ }
81
+
82
+ /**
83
+ * Enter description here...
84
+ *
85
+ * @param array|Varien_Data_Tree_Node $data
86
+ * @param Varien_Data_Tree_Node $parentNode
87
+ * @param Varien_Data_Tree_Node $prevNode
88
+ * @return Varien_Data_Tree_Node
89
+ */
90
+ public function appendChild($data=array(), $parentNode, $prevNode=null)
91
+ {
92
+ if (is_array($data)) {
93
+ $node = $this->addNode(
94
+ new Varien_Data_Tree_Node($data, $parentNode->getIdField(), $this),
95
+ $parentNode
96
+ );
97
+ } elseif ($data instanceof Varien_Data_Tree_Node) {
98
+ $node = $this->addNode($data, $parentNode);
99
+ }
100
+ return $node;
101
+ }
102
+
103
+ /**
104
+ * Enter description here...
105
+ *
106
+ * @param Varien_Data_Tree_Node $node
107
+ * @param Varien_Data_Tree_Node $parent
108
+ * @return Varien_Data_Tree_Node
109
+ */
110
+ public function addNode($node, $parent=null)
111
+ {
112
+ $this->_nodes->add($node);
113
+ $node->setParent($parent);
114
+ if (!is_null($parent) && ($parent instanceof Varien_Data_Tree_Node) ) {
115
+ $parent->addChild($node);
116
+ }
117
+ return $node;
118
+ }
119
+
120
+ /**
121
+ * Enter description here...
122
+ *
123
+ * @param Varien_Data_Tree_Node $node
124
+ * @param Varien_Data_Tree_Node $parentNode
125
+ * @param Varien_Data_Tree_Node $prevNode
126
+ */
127
+ public function moveNodeTo($node, $parentNode, $prevNode=null)
128
+ {
129
+ }
130
+
131
+ /**
132
+ * Enter description here...
133
+ *
134
+ * @param Varien_Data_Tree_Node $node
135
+ * @param Varien_Data_Tree_Node $parentNode
136
+ * @param Varien_Data_Tree_Node $prevNode
137
+ */
138
+ public function copyNodeTo($node, $parentNode, $prevNode=null)
139
+ {
140
+ }
141
+
142
+ /**
143
+ * Enter description here...
144
+ *
145
+ * @param Varien_Data_Tree_Node $node
146
+ * @return Varien_Data_Tree
147
+ */
148
+ public function removeNode($node)
149
+ {
150
+ $this->_nodes->delete($node);
151
+ if ($node->getParent()) {
152
+ $node->getParent()->removeChild($node);
153
+ }
154
+ unset($node);
155
+ return $this;
156
+ }
157
+
158
+ /**
159
+ * Enter description here...
160
+ *
161
+ * @param Varien_Data_Tree_Node $parentNode
162
+ * @param Varien_Data_Tree_Node $prevNode
163
+ */
164
+ public function createNode($parentNode, $prevNode=null)
165
+ {
166
+ }
167
+
168
+ /**
169
+ * Enter description here...
170
+ *
171
+ * @param Varien_Data_Tree_Node $node
172
+ */
173
+ public function getChild($node)
174
+ {
175
+ }
176
+
177
+ /**
178
+ * Enter description here...
179
+ *
180
+ * @param Varien_Data_Tree_Node $node
181
+ */
182
+ public function getChildren($node)
183
+ {
184
+ }
185
+
186
+ /**
187
+ * Enter description here...
188
+ *
189
+ * @return Varien_Data_Tree_Node_Collection
190
+ */
191
+ public function getNodes()
192
+ {
193
+ return $this->_nodes;
194
+ }
195
+
196
+ /**
197
+ * Enter description here...
198
+ *
199
+ * @param unknown_type $nodeId
200
+ * @return Varien_Data_Tree_Node
201
+ */
202
+ public function getNodeById($nodeId)
203
+ {
204
+ return $this->_nodes->searchById($nodeId);
205
+ }
206
+
207
+ /**
208
+ * Enter description here...
209
+ *
210
+ * @param Varien_Data_Tree_Node $node
211
+ * @return array
212
+ */
213
+ public function getPath($node)
214
+ {
215
+ if ($node instanceof Varien_Data_Tree_Node ) {
216
+
217
+ } elseif (is_numeric($node)){
218
+ if ($_node = $this->getNodeById($node)) {
219
+ return $_node->getPath();
220
+ }
221
+ }
222
+ return array();
223
+ }
224
+
225
+ }
lib/Varien/Data/Tree/Db.php ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Data DB tree
29
+ *
30
+ * Data model:
31
+ * id | pid | level | order
32
+ *
33
+ * @category Varien
34
+ * @package Varien_Data
35
+ * @author Magento Core Team <core@magentocommerce.com>
36
+ */
37
+ class Varien_Data_Tree_Db extends Varien_Data_Tree
38
+ {
39
+ const ID_FIELD = 'id';
40
+ const PARENT_FIELD = 'parent';
41
+ const LEVEL_FIELD = 'level';
42
+ const ORDER_FIELD = 'order';
43
+
44
+ /**
45
+ * DB connection
46
+ *
47
+ * @var Zend_Db_Adapter_Abstract
48
+ */
49
+ protected $_conn;
50
+
51
+ /**
52
+ * Data table name
53
+ *
54
+ * @var string
55
+ */
56
+ protected $_table;
57
+
58
+ /**
59
+ * SQL select object
60
+ *
61
+ * @var Zend_Db_Select
62
+ */
63
+ protected $_select;
64
+
65
+ /**
66
+ * Tree ctructure field names
67
+ *
68
+ * @var string
69
+ */
70
+ protected $_idField;
71
+ protected $_parentField;
72
+ protected $_levelField;
73
+ protected $_orderField;
74
+
75
+ /**
76
+ * Db tree constructor
77
+ *
78
+ * $fields = array(
79
+ * Varien_Data_Tree_Db::ID_FIELD => string,
80
+ * Varien_Data_Tree_Db::PARENT_FIELD => string,
81
+ * Varien_Data_Tree_Db::LEVEL_FIELD => string
82
+ * Varien_Data_Tree_Db::ORDER_FIELD => string
83
+ * )
84
+ *
85
+ * @param Zend_Db_Adapter_Abstract $connection
86
+ * @param string $table
87
+ * @param array $fields
88
+ */
89
+ public function __construct($connection, $table, $fields)
90
+ {
91
+ parent::__construct();
92
+
93
+ if (!$connection) {
94
+ throw new Exception('Wrong "$connection" parametr');
95
+ }
96
+
97
+ $this->_conn = $connection;
98
+ $this->_table = $table;
99
+
100
+ if (!isset($fields[self::ID_FIELD]) ||
101
+ !isset($fields[self::PARENT_FIELD]) ||
102
+ !isset($fields[self::LEVEL_FIELD]) ||
103
+ !isset($fields[self::ORDER_FIELD])) {
104
+
105
+ throw new Exception('"$fields" tree configuratin array');
106
+ }
107
+
108
+ $this->_idField = $fields[self::ID_FIELD];
109
+ $this->_parentField = $fields[self::PARENT_FIELD];
110
+ $this->_levelField = $fields[self::LEVEL_FIELD];
111
+ $this->_orderField = $fields[self::ORDER_FIELD];
112
+
113
+ $this->_select = $this->_conn->select();
114
+ $this->_select->from($this->_table, array_values($fields));
115
+ }
116
+
117
+ public function getDbSelect()
118
+ {
119
+ return $this->_select;
120
+ }
121
+
122
+ public function setDbSelect($select)
123
+ {
124
+ $this->_select = $select;
125
+ }
126
+
127
+ /**
128
+ * Load tree
129
+ *
130
+ * @param int || Varien_Data_Tree_Node $parentNode
131
+ * @param int $recursionLevel recursion level
132
+ * @return this
133
+ */
134
+ public function load($parentNode=null, $recursionLevel=100)
135
+ {
136
+ if (is_null($parentNode)) {
137
+ $this->_loadFullTree();
138
+ return $this;
139
+ }
140
+ elseif ($parentNode instanceof Varien_Data_Tree_Node) {
141
+ $parentId = $parentNode->getId();
142
+ }
143
+ elseif (is_numeric($parentNode)) {
144
+ $parentId = $parentNode;
145
+ $parentNode = null;
146
+ }
147
+ else {
148
+ throw new Exception('root node id is not defined');
149
+ }
150
+
151
+ $select = clone $this->_select;
152
+ $select->order($this->_table.'.'.$this->_orderField . ' ASC');
153
+ $condition = $this->_conn->quoteInto("$this->_table.$this->_parentField=?", $parentId);
154
+ $select->where($condition);
155
+ $arrNodes = $this->_conn->fetchAll($select);
156
+ foreach ($arrNodes as $nodeInfo) {
157
+ $node = new Varien_Data_Tree_Node($nodeInfo, $this->_idField, $this, $parentNode);
158
+ $this->addNode($node, $parentNode);
159
+
160
+ if ($recursionLevel) {
161
+ $node->loadChildren($recursionLevel-1);
162
+ }
163
+ }
164
+ return $this;
165
+ }
166
+
167
+ public function loadNode($nodeId)
168
+ {
169
+ $select = clone $this->_select;
170
+ $condition = $this->_conn->quoteInto("$this->_table.$this->_idField=?", $nodeId);
171
+ $select->where($condition);
172
+ $node = new Varien_Data_Tree_Node($this->_conn->fetchRow($select), $this->_idField, $this);
173
+ $this->addNode($node);
174
+ return $node;
175
+ }
176
+
177
+ public function appendChild($data=array(), $parentNode, $prevNode=null)
178
+ {
179
+ $orderSelect = $this->_conn->select();
180
+ $orderSelect->from($this->_table, new Zend_Db_Expr('MAX('.$this->_conn->quoteIdentifier($this->_orderField).')'))
181
+ ->where($this->_conn->quoteIdentifier($this->_parentField).'='.$parentNode->getId());
182
+
183
+ $order = $this->_conn->fetchOne($orderSelect);
184
+ $data[$this->_parentField] = $parentNode->getId();
185
+ $data[$this->_levelField] = $parentNode->getData($this->_levelField)+1;
186
+ $data[$this->_orderField] = $order+1;
187
+
188
+ $this->_conn->insert($this->_table, $data);
189
+ $data[$this->_idField] = $this->_conn->lastInsertId();
190
+
191
+ return parent::appendChild($data, $parentNode, $prevNode);
192
+ }
193
+
194
+ /**
195
+ * Move tree node
196
+ *
197
+ * @param Varien_Data_Tree_Node $node
198
+ * @param Varien_Data_Tree_Node $parentNode
199
+ * @param Varien_Data_Tree_Node $prevNode
200
+ */
201
+ public function moveNodeTo($node, $parentNode, $prevNode=null)
202
+ {
203
+ $data = array();
204
+ $data[$this->_parentField] = $parentNode->getId();
205
+ $data[$this->_levelField] = $parentNode->getData($this->_levelField)+1;
206
+ // New node order
207
+ if (is_null($prevNode) || is_null($prevNode->getData($this->_orderField))) {
208
+ $data[$this->_orderField] = 1;
209
+ }
210
+ else {
211
+ $data[$this->_orderField] = $prevNode->getData($this->_orderField)+1;
212
+ }
213
+ $condition = $this->_conn->quoteInto("$this->_idField=?", $node->getId());
214
+
215
+ // For reorder new node branch
216
+ $dataReorderNew = array(
217
+ $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'+1')
218
+ );
219
+ $conditionReorderNew = $this->_conn->quoteIdentifier($this->_parentField).'='.$parentNode->getId().
220
+ ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>='. $data[$this->_orderField];
221
+
222
+ // For reorder old node branch
223
+ $dataReorderOld = array(
224
+ $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'-1')
225
+ );
226
+ $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField).'='.$node->getData($this->_parentField).
227
+ ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>'.$node->getData($this->_orderField);
228
+
229
+ $this->_conn->beginTransaction();
230
+ try {
231
+ // Prepare new node branch
232
+ $this->_conn->update($this->_table, $dataReorderNew, $conditionReorderNew);
233
+ // Move node
234
+ $this->_conn->update($this->_table, $data, $condition);
235
+ // Update old node branch
236
+ $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
237
+ $this->_updateChildLevels($node->getId(), $data[$this->_levelField]);
238
+ $this->_conn->commit();
239
+ }
240
+ catch (Exception $e){
241
+ $this->_conn->rollBack();
242
+ throw new Exception('Can\'t move tree node');
243
+ }
244
+ }
245
+
246
+ protected function _updateChildLevels($parentId, $parentLevel)
247
+ {
248
+ $select = $this->_conn->select()
249
+ ->from($this->_table, $this->_idField)
250
+ ->where($this->_parentField.'=?', $parentId);
251
+ $ids = $this->_conn->fetchCol($select);
252
+
253
+ if (!empty($ids)) {
254
+ $this->_conn->update($this->_table,
255
+ array($this->_levelField=>$parentLevel+1),
256
+ $this->_conn->quoteInto($this->_idField.' IN (?)', $ids));
257
+ foreach ($ids as $id) {
258
+ $this->_updateChildLevels($id, $parentLevel+1);
259
+ }
260
+ }
261
+ return $this;
262
+ }
263
+
264
+ protected function _loadFullTree()
265
+ {
266
+ $select = clone $this->_select;
267
+ $select->order($this->_table . '.' . $this->_levelField)
268
+ ->order($this->_table.'.'.$this->_orderField);
269
+
270
+ $arrNodes = $this->_conn->fetchAll($select);
271
+
272
+ foreach ($arrNodes as $nodeInfo) {
273
+ $node = new Varien_Data_Tree_Node($nodeInfo, $this->_idField, $this);
274
+ $parentNode = $this->getNodeById($nodeInfo[$this->_parentField]);
275
+ $this->addNode($node, $parentNode);
276
+ }
277
+
278
+ return $this;
279
+ }
280
+
281
+ public function removeNode($node)
282
+ {
283
+ // For reorder old node branch
284
+ $dataReorderOld = array(
285
+ $this->_orderField => new Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField).'-1')
286
+ );
287
+ $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField).'='.$node->getData($this->_parentField).
288
+ ' AND '.$this->_conn->quoteIdentifier($this->_orderField).'>'.$node->getData($this->_orderField);
289
+
290
+ $this->_conn->beginTransaction();
291
+ try {
292
+ $condition = $this->_conn->quoteInto("$this->_idField=?", $node->getId());
293
+ $this->_conn->delete($this->_table, $condition);
294
+ // Update old node branch
295
+ $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld);
296
+ $this->_conn->commit();
297
+ }
298
+ catch (Exception $e){
299
+ $this->_conn->rollBack();
300
+ throw new Exception('Can\'t remove tree node');
301
+ }
302
+ parent::removeNode($node);
303
+ return $this;
304
+ }
305
+ }
lib/Varien/Data/Tree/Dbp.php ADDED
@@ -0,0 +1,394 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Data DB tree
30
+ *
31
+ * Data model:
32
+ * id | path | order
33
+ *
34
+ * @category Varien
35
+ * @package Varien_Data
36
+ * @author Magento Core Team <core@magentocommerce.com>
37
+ */
38
+ class Varien_Data_Tree_Dbp extends Varien_Data_Tree
39
+ {
40
+
41
+ const ID_FIELD = 'id';
42
+ const PATH_FIELD = 'path';
43
+ const ORDER_FIELD = 'order';
44
+ const LEVEL_FIELD = 'level';
45
+
46
+ /**
47
+ * DB connection
48
+ *
49
+ * @var Zend_Db_Adapter_Abstract
50
+ */
51
+ protected $_conn;
52
+
53
+ /**
54
+ * Data table name
55
+ *
56
+ * @var string
57
+ */
58
+ protected $_table;
59
+
60
+ protected $_loaded = false;
61
+
62
+ /**
63
+ * SQL select object
64
+ *
65
+ * @var Zend_Db_Select
66
+ */
67
+ protected $_select;
68
+
69
+ /**
70
+ * Tree ctructure field names
71
+ *
72
+ * @var string
73
+ */
74
+ protected $_idField;
75
+ protected $_pathField;
76
+ protected $_orderField;
77
+ protected $_levelField;
78
+
79
+ /**
80
+ * Db tree constructor
81
+ *
82
+ * $fields = array(
83
+ * Varien_Data_Tree_Dbp::ID_FIELD => string,
84
+ * Varien_Data_Tree_Dbp::PATH_FIELD => string,
85
+ * Varien_Data_Tree_Dbp::ORDER_FIELD => string
86
+ * Varien_Data_Tree_Dbp::LEVEL_FIELD => string
87
+ * )
88
+ *
89
+ * @param Zend_Db_Adapter_Abstract $connection
90
+ * @param string $table
91
+ * @param array $fields
92
+ */
93
+ public function __construct($connection, $table, $fields)
94
+ {
95
+ parent::__construct();
96
+
97
+ if (!$connection) {
98
+ throw new Exception('Wrong "$connection" parametr');
99
+ }
100
+
101
+ $this->_conn = $connection;
102
+ $this->_table = $table;
103
+
104
+ if (!isset($fields[self::ID_FIELD]) ||
105
+ !isset($fields[self::PATH_FIELD]) ||
106
+ !isset($fields[self::LEVEL_FIELD]) ||
107
+ !isset($fields[self::ORDER_FIELD])) {
108
+
109
+ throw new Exception('"$fields" tree configuratin array');
110
+ }
111
+
112
+ $this->_idField = $fields[self::ID_FIELD];
113
+ $this->_pathField = $fields[self::PATH_FIELD];
114
+ $this->_orderField = $fields[self::ORDER_FIELD];
115
+ $this->_levelField = $fields[self::LEVEL_FIELD];
116
+
117
+ $this->_select = $this->_conn->select();
118
+ $this->_select->from($this->_table);
119
+ }
120
+
121
+ /**
122
+ * Retrieve current select object
123
+ *
124
+ * @return Varien_Db_Select
125
+ */
126
+ public function getDbSelect()
127
+ {
128
+ return $this->_select;
129
+ }
130
+
131
+ /**
132
+ * Set Select object
133
+ *
134
+ * @param Varien_Db_Select $select
135
+ */
136
+ public function setDbSelect($select)
137
+ {
138
+ $this->_select = $select;
139
+ }
140
+
141
+ /**
142
+ * Load tree
143
+ *
144
+ * @param int|Varien_Data_Tree_Node $parentNode
145
+ * @return Varien_Data_Tree_Dbp
146
+ */
147
+ public function load($parentNode=null, $recursionLevel = 0)
148
+ {
149
+ if (!$this->_loaded) {
150
+ $startLevel = 1;
151
+ $parentPath = '';
152
+
153
+ if ($parentNode instanceof Varien_Data_Tree_Node) {
154
+ $parentPath = $parentNode->getData($this->_pathField);
155
+ $startLevel = $parentNode->getData($this->_levelField);
156
+ } else if (is_numeric($parentNode)) {
157
+ $select = $this->_conn->select()
158
+ ->from($this->_table, array($this->_pathField, $this->_levelField))
159
+ ->where("{$this->_idField} = ?", $parentNode);
160
+ $parent = $this->_conn->fetchRow($select);
161
+
162
+ $startLevel = $parent[$this->_levelField];
163
+ $parentPath = $parent[$this->_pathField];
164
+ $parentNode = null;
165
+ } else if (is_string($parentNode)) {
166
+ $parentPath = $parentNode;
167
+ $startLevel = count(explode($parentPath))-1;
168
+ $parentNode = null;
169
+ }
170
+
171
+ $select = clone $this->_select;
172
+
173
+ $select->order($this->_table . '.' . $this->_orderField . ' ASC');
174
+ if ($parentPath) {
175
+ $pathField = $this->_conn->quoteIdentifier(array($this->_table, $this->_pathField));
176
+ $select->where("{$pathField} LIKE ?", "{$parentPath}/%");
177
+ }
178
+ if ($recursionLevel != 0) {
179
+ $levelField = $this->_conn->quoteIdentifier(array($this->_table, $this->_levelField));
180
+ $select->where("{$levelField} <= ?", $startLevel + $recursionLevel);
181
+ }
182
+
183
+ $arrNodes = $this->_conn->fetchAll($select);
184
+
185
+ $childrenItems = array();
186
+
187
+ foreach ($arrNodes as $nodeInfo) {
188
+ $pathToParent = explode('/', $nodeInfo[$this->_pathField]);
189
+ array_pop($pathToParent);
190
+ $pathToParent = implode('/', $pathToParent);
191
+ $childrenItems[$pathToParent][] = $nodeInfo;
192
+ }
193
+
194
+ $this->addChildNodes($childrenItems, $parentPath, $parentNode);
195
+
196
+ $this->_loaded = true;
197
+ }
198
+
199
+ return $this;
200
+ }
201
+
202
+ public function addChildNodes($children, $path, $parentNode, $level = 0)
203
+ {
204
+ if (isset($children[$path])) {
205
+ foreach ($children[$path] as $child) {
206
+ $nodeId = isset($child[$this->_idField])?$child[$this->_idField]:false;
207
+ if ($parentNode && $nodeId && $node = $parentNode->getChildren()->searchById($nodeId)) {
208
+ $node->addData($child);
209
+ } else {
210
+ $node = new Varien_Data_Tree_Node($child, $this->_idField, $this, $parentNode);
211
+ }
212
+
213
+ //$node->setLevel(count(explode('/', $node->getData($this->_pathField)))-1);
214
+ $node->setLevel($node->getData($this->_levelField));
215
+ $node->setPathId($node->getData($this->_pathField));
216
+ $this->addNode($node, $parentNode);
217
+
218
+
219
+ if ($path) {
220
+ $childrenPath = explode('/', $path);
221
+ } else {
222
+ $childrenPath = array();
223
+ }
224
+ $childrenPath[] = $node->getId();
225
+ $childrenPath = implode('/', $childrenPath);
226
+
227
+ $this->addChildNodes($children, $childrenPath, $node, $level+1);
228
+ }
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Enter description here...
234
+ *
235
+ * @param int|string $nodeId
236
+ * @return Varien_Data_Tree_Node
237
+ */
238
+ public function loadNode($nodeId)
239
+ {
240
+ $select = clone $this->_select;
241
+ if (is_numeric($nodeId)) {
242
+ $condField = $this->_conn->quoteIdentifier(array($this->_table, $this->_idField));
243
+ } else {
244
+ $condField = $this->_conn->quoteIdentifier(array($this->_table, $this->_pathField));
245
+ }
246
+
247
+ $select->where("{$condField} = ?", $nodeId);
248
+
249
+ $node = new Varien_Data_Tree_Node($this->_conn->fetchRow($select), $this->_idField, $this);
250
+ $this->addNode($node);
251
+ return $node;
252
+ }
253
+
254
+ public function getChildren($node, $recursive = true, $result = array()) {
255
+ if (is_numeric($node)) {
256
+ $node = $this->getNodeById($node);
257
+ }
258
+ if (!$node) {
259
+ return $result;
260
+ }
261
+
262
+ foreach ($node->getChildren() as $child) {
263
+ if ($recursive) {
264
+ if ($child->getChildren()) {
265
+ $result = $this->getChildren($child, $recursive, $result);
266
+ }
267
+ }
268
+ $result[] = $child->getId();
269
+ }
270
+ return $result;
271
+ }
272
+
273
+ /**
274
+ * Move tree node
275
+ *
276
+ * @todo Use adapter for generate conditions
277
+ * @param Varien_Data_Tree_Node $node
278
+ * @param Varien_Data_Tree_Node $newParent
279
+ * @param Varien_Data_Tree_Node $prevNode
280
+ */
281
+ public function move($node, $newParent, $prevNode = null)
282
+ {
283
+ $position = 1;
284
+
285
+ $oldPath = $node->getData($this->_pathField);
286
+ $newPath = $newParent->getData($this->_pathField);
287
+
288
+ $newPath = $newPath . '/' . $node->getId();
289
+ $oldPathLength = strlen($oldPath);
290
+
291
+ $newLevel = $newParent->getLevel()+1;
292
+ $levelDisposition = $newLevel-$node->getLevel();
293
+
294
+ $data = array(
295
+ $this->_levelField => new Zend_Db_Expr("{$this->_levelField} + '{$levelDisposition}'"),
296
+ $this->_pathField => new Zend_Db_Expr("CONCAT('$newPath', RIGHT($this->_pathField, LENGTH($this->_pathField) - {$oldPathLength}))")
297
+ );
298
+ $condition = $this->_conn->quoteInto("$this->_pathField REGEXP ?", "^$oldPath(/|$)");
299
+
300
+ $this->_conn->beginTransaction();
301
+
302
+ $reorderData = array($this->_orderField => new Zend_Db_Expr("$this->_orderField + 1"));
303
+ try {
304
+ if ($prevNode && $prevNode->getId()) {
305
+ $reorderCondition = "{$this->_orderField} > {$prevNode->getData($this->_orderField)}";
306
+ $position = $prevNode->getData($this->_orderField) + 1;
307
+ } else {
308
+ $reorderCondition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$newParent->getData($this->_pathField)}/[0-9]+$");
309
+ $select = $this->_conn->select()
310
+ ->from($this->_table, new Zend_Db_Expr("MIN({$this->_orderField})"))
311
+ ->where($reorderCondition);
312
+
313
+ $position = (int) $this->_conn->fetchOne($select);
314
+ }
315
+ $this->_conn->update($this->_table, $reorderData, $reorderCondition);
316
+ $this->_conn->update($this->_table, $data, $condition);
317
+ $this->_conn->update($this->_table, array($this->_orderField => $position, $this->_levelField=>$newLevel),
318
+ $this->_conn->quoteInto("{$this->_idField} = ?", $node->getId())
319
+ );
320
+
321
+ $this->_conn->commit();
322
+ } catch (Exception $e){
323
+ $this->_conn->rollBack();
324
+ throw new Exception("Can't move tree node due to error: " . $e->getMessage());
325
+ }
326
+ }
327
+
328
+ public function loadEnsuredNodes($category, $rootNode)
329
+ {
330
+ $pathIds = $category->getPathIds();
331
+ $rootNodeId = $rootNode->getId();
332
+ $rootNodePath = $rootNode->getData($this->_pathField);
333
+
334
+ $select = clone $this->_select;
335
+ $select->order($this->_table.'.'.$this->_orderField . ' ASC');
336
+
337
+ if ($pathIds) {
338
+ $condition = $this->_conn->quoteInto("$this->_table.$this->_idField in (?)", $pathIds);
339
+ $select->where($condition);
340
+ }
341
+
342
+ $arrNodes = $this->_conn->fetchAll($select);
343
+
344
+ if ($arrNodes) {
345
+ $childrenItems = array();
346
+ foreach ($arrNodes as $nodeInfo) {
347
+ $nodeId = $nodeInfo[$this->_idField];
348
+ if ($nodeId<=$rootNodeId) {
349
+ continue;
350
+ }
351
+
352
+ $pathToParent = explode('/', $nodeInfo[$this->_pathField]);
353
+ array_pop($pathToParent);
354
+ $pathToParent = implode('/', $pathToParent);
355
+ $childrenItems[$pathToParent][] = $nodeInfo;
356
+ }
357
+
358
+ $this->_addChildNodes($childrenItems, $rootNodePath, $rootNode, true);
359
+ }
360
+ }
361
+
362
+ protected function _addChildNodes($children, $path, $parentNode, $withChildren=false, $level = 0)
363
+ {
364
+ if (isset($children[$path])) {
365
+ foreach ($children[$path] as $child) {
366
+ $nodeId = isset($child[$this->_idField])?$child[$this->_idField]:false;
367
+ if ($parentNode && $nodeId && $node = $parentNode->getChildren()->searchById($nodeId)) {
368
+ $node->addData($child);
369
+ } else {
370
+ $node = new Varien_Data_Tree_Node($child, $this->_idField, $this, $parentNode);
371
+ $node->setLevel($node->getData($this->_levelField));
372
+ $node->setPathId($node->getData($this->_pathField));
373
+ $this->addNode($node, $parentNode);
374
+ }
375
+
376
+ if ($withChildren) {
377
+ $this->_loaded = false;
378
+ $node->loadChildren(1);
379
+ $this->_loaded = false;
380
+ }
381
+
382
+ if ($path) {
383
+ $childrenPath = explode('/', $path);
384
+ } else {
385
+ $childrenPath = array();
386
+ }
387
+ $childrenPath[] = $node->getId();
388
+ $childrenPath = implode('/', $childrenPath);
389
+
390
+ $this->_addChildNodes($children, $childrenPath, $node, $withChildren, $level+1);
391
+ }
392
+ }
393
+ }
394
+ }
lib/Varien/Data/Tree/Node.php ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Data tree node
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Tree_Node extends Varien_Object
35
+ {
36
+ /**
37
+ * Parent node
38
+ *
39
+ * @var Varien_Data_Tree_Node
40
+ */
41
+ protected $_parent;
42
+
43
+ /**
44
+ * Main tree object
45
+ *
46
+ * @var Varien_Data_Tree
47
+ */
48
+ protected $_tree;
49
+
50
+ /**
51
+ * Child nodes
52
+ *
53
+ * @var Varien_Data_Tree_Node_Collection
54
+ */
55
+ protected $_childNodes;
56
+
57
+ /**
58
+ * Node ID field name
59
+ *
60
+ * @var string
61
+ */
62
+ protected $_idField;
63
+
64
+ /**
65
+ * Data tree node constructor
66
+ *
67
+ * @param array $data
68
+ * @param string $idFeild
69
+ * @param Varien_Data_Tree $tree
70
+ * @param Varien_Data_Tree_Node $parent
71
+ */
72
+ public function __construct($data, $idFeild, $tree, $parent = null)
73
+ {
74
+ $this->setTree($tree);
75
+ $this->setParent($parent);
76
+ $this->setIdField($idFeild);
77
+ $this->setData($data);
78
+ $this->_childNodes = new Varien_Data_Tree_Node_Collection($this);
79
+ }
80
+
81
+ /**
82
+ * Retrieve node id
83
+ *
84
+ * @return mixed
85
+ */
86
+ public function getId()
87
+ {
88
+ return $this->getData($this->getIdField());
89
+ }
90
+
91
+ /**
92
+ * Set node id field name
93
+ *
94
+ * @param string $idField
95
+ * @return this
96
+ */
97
+ public function setIdField($idField)
98
+ {
99
+ $this->_idField = $idField;
100
+ return $this;
101
+ }
102
+
103
+ /**
104
+ * Retrieve node id field name
105
+ *
106
+ * @return string
107
+ */
108
+ public function getIdField()
109
+ {
110
+ return $this->_idField;
111
+ }
112
+
113
+ /**
114
+ * Set node tree object
115
+ *
116
+ * @param Varien_Data_Tree $tree
117
+ * @return this
118
+ */
119
+ public function setTree(Varien_Data_Tree $tree)
120
+ {
121
+ $this->_tree = $tree;
122
+ return $this;
123
+ }
124
+
125
+ /**
126
+ * Retrieve node tree object
127
+ *
128
+ * @return Varien_Data_Tree
129
+ */
130
+ public function getTree()
131
+ {
132
+ return $this->_tree;
133
+ }
134
+
135
+ /**
136
+ * Set node parent
137
+ *
138
+ * @param Varien_Data_Tree_Node $parent
139
+ * @return Varien_Data_Tree_Node
140
+ */
141
+ public function setParent($parent)
142
+ {
143
+ $this->_parent = $parent;
144
+ return $this;
145
+ }
146
+
147
+ /**
148
+ * Retrieve node parent
149
+ *
150
+ * @return Varien_Data_Tree
151
+ */
152
+ public function getParent()
153
+ {
154
+ return $this->_parent;
155
+ }
156
+
157
+ /**
158
+ * Check node children
159
+ *
160
+ * @return bool
161
+ */
162
+ public function hasChildren()
163
+ {
164
+ return $this->_childNodes->count() > 0;
165
+ }
166
+
167
+ public function setLevel($level)
168
+ {
169
+ $this->setData('level', $level);
170
+ return $this;
171
+ }
172
+
173
+ public function setPathId($path)
174
+ {
175
+ $this->setData('path_id', $path);
176
+ return $this;
177
+ }
178
+
179
+ public function isChildOf($node)
180
+ {
181
+
182
+ }
183
+
184
+ /**
185
+ * Load node children
186
+ *
187
+ * @param int $recursionLevel
188
+ * @return Varien_Data_Tree_Node
189
+ */
190
+ public function loadChildren($recursionLevel=0)
191
+ {
192
+ $this->_tree->load($this, $recursionLevel);
193
+ return $this;
194
+ }
195
+
196
+ /**
197
+ * Retrieve node children collection
198
+ *
199
+ * @return Varien_Data_Tree_Node_Collection
200
+ */
201
+ public function getChildren()
202
+ {
203
+ return $this->_childNodes;
204
+ }
205
+
206
+ public function getAllChildNodes(&$nodes = array())
207
+ {
208
+ foreach ($this->_childNodes as $node) {
209
+ $nodes[$node->getId()] = $node;
210
+ $node->getAllChildNodes($nodes);
211
+ }
212
+ return $nodes;
213
+ }
214
+
215
+ public function getLastChild()
216
+ {
217
+ return $this->_childNodes->lastNode();
218
+ }
219
+
220
+ /**
221
+ * Add child node
222
+ *
223
+ * @param Varien_Data_Tree_Node $node
224
+ * @return Varien_Data_Tree_Node
225
+ */
226
+ public function addChild($node)
227
+ {
228
+ $this->_childNodes->add($node);
229
+ return $this;
230
+ }
231
+
232
+ public function appendChild($prevNode=null)
233
+ {
234
+ $this->_tree->appendChild($this, $prevNode);
235
+ return $this;
236
+ }
237
+
238
+ public function moveTo($parentNode, $prevNode=null)
239
+ {
240
+ $this->_tree->moveNodeTo($this, $parentNode, $prevNode);
241
+ return $this;
242
+ }
243
+
244
+ public function copyTo($parentNode, $prevNode=null)
245
+ {
246
+ $this->_tree->copyNodeTo($this, $parentNode, $prevNode);
247
+ return $this;
248
+ }
249
+
250
+ public function removeChild($childNode)
251
+ {
252
+ $this->_childNodes->delete($childNode);
253
+ return $this;
254
+ }
255
+
256
+ public function getPath(&$prevNodes = array())
257
+ {
258
+ if ($this->_parent) {
259
+ array_push($prevNodes, $this);
260
+ $this->_parent->getPath($prevNodes);
261
+ }
262
+ return $prevNodes;
263
+ }
264
+
265
+ public function getIsActive()
266
+ {
267
+ return $this->_getData('is_active');
268
+ }
269
+
270
+ public function getName()
271
+ {
272
+ return $this->_getData('name');
273
+ }
274
+
275
+ }
lib/Varien/Data/Tree/Node/Collection.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Data
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Tree node collection
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Data
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Data_Tree_Node_Collection implements ArrayAccess, IteratorAggregate
35
+ {
36
+ private $_nodes;
37
+ private $_container;
38
+
39
+ public function __construct($container)
40
+ {
41
+ $this->_nodes = array();
42
+ $this->_container = $container;
43
+ }
44
+
45
+ public function getNodes()
46
+ {
47
+ return $this->_nodes;
48
+ }
49
+
50
+ /**
51
+ * Implementation of IteratorAggregate::getIterator()
52
+ */
53
+ public function getIterator()
54
+ {
55
+ return new ArrayIterator($this->_nodes);
56
+ }
57
+
58
+ /**
59
+ * Implementation of ArrayAccess:offsetSet()
60
+ */
61
+ public function offsetSet($key, $value)
62
+ {
63
+ $this->_nodes[$key] = $value;
64
+ }
65
+
66
+ /**
67
+ * Implementation of ArrayAccess:offsetGet()
68
+ */
69
+ public function offsetGet($key)
70
+ {
71
+ return $this->_nodes[$key];
72
+ }
73
+
74
+ /**
75
+ * Implementation of ArrayAccess:offsetUnset()
76
+ */
77
+ public function offsetUnset($key)
78
+ {
79
+ unset($this->_nodes[$key]);
80
+ }
81
+
82
+ /**
83
+ * Implementation of ArrayAccess:offsetExists()
84
+ */
85
+ public function offsetExists($key)
86
+ {
87
+ return isset($this->_nodes[$key]);
88
+ }
89
+
90
+ /**
91
+ * Adds a node to this node
92
+ */
93
+ public function add(Varien_Data_Tree_Node $node)
94
+ {
95
+ $node->setParent($this->_container);
96
+
97
+ // Set the Tree for the node
98
+ if ($this->_container->getTree() instanceof Varien_Data_Tree) {
99
+ $node->setTree($this->_container->getTree());
100
+ }
101
+
102
+ $this->_nodes[$node->getId()] = $node;
103
+
104
+ return $node;
105
+ }
106
+
107
+ public function delete($node)
108
+ {
109
+ if (isset($this->_nodes[$node->getId()])) {
110
+ unset($this->_nodes[$node->getId()]);
111
+ }
112
+ return $this;
113
+ }
114
+
115
+ public function count()
116
+ {
117
+ return count($this->_nodes);
118
+ }
119
+
120
+ public function lastNode()
121
+ {
122
+ return !empty($this->_nodes) ? $this->_nodes[count($this->_nodes) - 1] : null;
123
+ }
124
+
125
+ public function searchById($nodeId)
126
+ {
127
+ if (isset($this->_nodes[$nodeId])) {
128
+ return $this->_nodes[$nodeId];
129
+ }
130
+ return null;
131
+ }
132
+ }
lib/Varien/Date.php ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Date
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Converter of date formats
29
+ * Internal dates
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Date
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Date
36
+ {
37
+ /**
38
+ * Date format, used as default. Compatible with Zend_Date
39
+ *
40
+ */
41
+ const DATETIME_INTERNAL_FORMAT = 'yyyy-MM-dd HH:mm:ss';
42
+ const DATE_INTERNAL_FORMAT = 'yyyy-MM-dd';
43
+
44
+ const DATETIME_PHP_FORMAT = 'Y-m-d H:i:s';
45
+ const DATE_PHP_FORMAT = 'Y-m-d';
46
+
47
+ /**
48
+ * Zend Date To local date according Map array
49
+ *
50
+ * @var array
51
+ */
52
+ private static $_convertZendToStrftimeDate = array(
53
+ 'yyyy-MM-ddTHH:mm:ssZZZZ' => '%c',
54
+ 'EEEE' => '%A',
55
+ 'EEE' => '%a',
56
+ 'D' => '%j',
57
+ 'MMMM' => '%B',
58
+ 'MMM' => '%b',
59
+ 'MM' => '%m',
60
+ 'M' => '%m',
61
+ 'dd' => '%d',
62
+ 'd' => '%e',
63
+ 'yyyy' => '%Y',
64
+ 'yy' => '%y',
65
+ 'y' => '%Y'
66
+ );
67
+ /**
68
+ * Zend Date To local time according Map array
69
+ *
70
+ * @var array
71
+ */
72
+ private static $_convertZendToStrftimeTime = array(
73
+ 'a' => '%p',
74
+ 'hh' => '%I',
75
+ 'h' => '%I',
76
+ 'HH' => '%H',
77
+ 'H' => '%H',
78
+ 'mm' => '%M',
79
+ 'ss' => '%S',
80
+ 'z' => '%Z',
81
+ 'v' => '%Z'
82
+ );
83
+
84
+ /**
85
+ * Convert Zend Date format to local time/date according format
86
+ *
87
+ * @param string $value
88
+ * @param boolean $convertDate
89
+ * @param boolean $convertTime
90
+ * @return string
91
+ */
92
+ public static function convertZendToStrftime($value, $convertDate = true, $convertTime = true)
93
+ {
94
+ if ($convertTime) {
95
+ $value = self::_convert($value, self::$_convertZendToStrftimeTime);
96
+ }
97
+ if ($convertDate) {
98
+ $value = self::_convert($value, self::$_convertZendToStrftimeDate);
99
+ }
100
+ return $value;
101
+ }
102
+
103
+ /**
104
+ * Convert value by dictionary
105
+ *
106
+ * @param string $value
107
+ * @param array $dictionary
108
+ * @return string
109
+ */
110
+ protected static function _convert($value, $dictionary)
111
+ {
112
+ foreach ($dictionary as $search => $replace) {
113
+ $value = preg_replace('/(^|[^%])' . $search . '/', '$1' . $replace, $value);
114
+ }
115
+ return $value;
116
+ }
117
+ /**
118
+ * Convert date to UNIX timestamp
119
+ * Returns current UNIX timestamp if date is true
120
+ *
121
+ * @param Zend_Date|string|true $date
122
+ * @return int
123
+ */
124
+ public static function toTimestamp($date)
125
+ {
126
+ if ($date instanceof Zend_Date) {
127
+ return $date->getUnixTimestamp();
128
+ }
129
+
130
+ if ($date === true) {
131
+ return time();
132
+ }
133
+
134
+ return strtotime($date);
135
+ }
136
+
137
+ /**
138
+ * Retrieve current date in internal format
139
+ *
140
+ * @param boolean $withoutTime day only flag
141
+ * @return string
142
+ */
143
+ public static function now($withoutTime = false)
144
+ {
145
+ $format = $withoutTime ? self::DATE_PHP_FORMAT : self::DATETIME_PHP_FORMAT;
146
+ return date($format);
147
+ }
148
+
149
+ /**
150
+ * Format date to internal format
151
+ *
152
+ * @param string|Zend_Date|true|null $date
153
+ * @param boolean $includeTime
154
+ * @return string|null
155
+ */
156
+ public static function formatDate($date, $includeTime = true)
157
+ {
158
+ if ($date === true) {
159
+ return self::now(!$includeTime);
160
+ }
161
+
162
+ if ($date instanceof Zend_Date) {
163
+ if ($includeTime) {
164
+ return $date->toString(self::DATETIME_INTERNAL_FORMAT);
165
+ } else {
166
+ return $date->toString(self::DATE_INTERNAL_FORMAT);
167
+ }
168
+ }
169
+
170
+ if (empty($date)) {
171
+ return null;
172
+ }
173
+
174
+ if (!is_numeric($date)) {
175
+ $date = self::toTimestamp($date);
176
+ }
177
+
178
+ $format = $includeTime ? self::DATETIME_PHP_FORMAT : self::DATE_PHP_FORMAT;
179
+ return date($format, $date);
180
+ }
181
+ }
182
+
lib/Varien/Db/Adapter/Interface.php ADDED
@@ -0,0 +1,965 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2010 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Varien Database Adapter Interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Db
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Db_Adapter_Interface
36
+ {
37
+ const INDEX_TYPE_PRIMARY = 'primary';
38
+ const INDEX_TYPE_UNIQUE = 'unique';
39
+ const INDEX_TYPE_INDEX = 'index';
40
+ const INDEX_TYPE_FULLTEXT = 'fulltext';
41
+
42
+ const FK_ACTION_CASCADE = 'CASCADE';
43
+ const FK_ACTION_SET_NULL = 'SET NULL';
44
+ const FK_ACTION_NO_ACTION = 'NO ACTION';
45
+ const FK_ACTION_RESTRICT = 'RESTRICT';
46
+ const FK_ACTION_SET_DEFAULT = 'SET DEFAULT';
47
+
48
+ const INSERT_ON_DUPLICATE = 1;
49
+ const INSERT_IGNORE = 2;
50
+
51
+ const ISO_DATE_FORMAT = 'yyyy-MM-dd';
52
+ const ISO_DATETIME_FORMAT = 'yyyy-MM-dd HH-mm-ss';
53
+
54
+ const INTERVAL_SECOND = 'SECOND';
55
+ const INTERVAL_MINUTE = 'MINUTES';
56
+ const INTERVAL_HOUR = 'HOURS';
57
+ const INTERVAL_DAY = 'DAYS';
58
+ const INTERVAL_MONTH = 'MONTHS';
59
+ const INTERVAL_YEAR = 'YEARS';
60
+
61
+ /**
62
+ * Begin new DB transaction for connection
63
+ *
64
+ * @return Varien_Db_Adapter_Pdo_Mysql
65
+ */
66
+ public function beginTransaction();
67
+
68
+ /**
69
+ * Commit DB transaction
70
+ *
71
+ * @return Varien_Db_Adapter_Pdo_Mysql
72
+ */
73
+ public function commit();
74
+
75
+ /**
76
+ * Roll-back DB transaction
77
+ *
78
+ * @return Varien_Db_Adapter_Pdo_Mysql
79
+ */
80
+ public function rollBack();
81
+
82
+ /**
83
+ * Retrieve DDL object for new table
84
+ *
85
+ * @param string $tableName the table name
86
+ * @param string $schemaName the database or schema name
87
+ * @return Varien_Db_Ddl_Table
88
+ */
89
+ public function newTable($tableName = null, $schemaName = null);
90
+
91
+ /**
92
+ * Create table from DDL object
93
+ *
94
+ * @param Varien_Db_Ddl_Table $table
95
+ * @throws Zend_Db_Exception
96
+ * @return Zend_Db_Statement_Interface
97
+ */
98
+ public function createTable(Varien_Db_Ddl_Table $table);
99
+
100
+ /**
101
+ * Drop table from database
102
+ *
103
+ * @param string $tableName
104
+ * @param string $schemaName
105
+ * @return boolean
106
+ */
107
+ public function dropTable($tableName, $schemaName = null);
108
+
109
+ /**
110
+ * Truncate a table
111
+ *
112
+ * @param string $tableName
113
+ * @param string $schemaName
114
+ * @return Varien_Db_Adapter_Interface
115
+ */
116
+ public function truncateTable($tableName, $schemaName = null);
117
+
118
+ /**
119
+ * Checks if table exists
120
+ *
121
+ * @param string $tableName
122
+ * @param string $schemaName
123
+ * @return boolean
124
+ */
125
+ public function isTableExists($tableName, $schemaName = null);
126
+
127
+ /**
128
+ * Returns short table status array
129
+ *
130
+ * @param string $tableName
131
+ * @param string $schemaName
132
+ * @return array|false
133
+ */
134
+ public function showTableStatus($tableName, $schemaName = null);
135
+
136
+ /**
137
+ * Returns the column descriptions for a table.
138
+ *
139
+ * The return value is an associative array keyed by the column name,
140
+ * as returned by the RDBMS.
141
+ *
142
+ * The value of each array element is an associative array
143
+ * with the following keys:
144
+ *
145
+ * SCHEMA_NAME => string; name of database or schema
146
+ * TABLE_NAME => string;
147
+ * COLUMN_NAME => string; column name
148
+ * COLUMN_POSITION => number; ordinal position of column in table
149
+ * DATA_TYPE => string; SQL datatype name of column
150
+ * DEFAULT => string; default expression of column, null if none
151
+ * NULLABLE => boolean; true if column can have nulls
152
+ * LENGTH => number; length of CHAR/VARCHAR
153
+ * SCALE => number; scale of NUMERIC/DECIMAL
154
+ * PRECISION => number; precision of NUMERIC/DECIMAL
155
+ * UNSIGNED => boolean; unsigned property of an integer type
156
+ * PRIMARY => boolean; true if column is part of the primary key
157
+ * PRIMARY_POSITION => integer; position of column in primary key
158
+ * IDENTITY => integer; true if column is auto-generated with unique values
159
+ *
160
+ * @param string $tableName
161
+ * @param string $schemaName OPTIONAL
162
+ * @return array
163
+ */
164
+ public function describeTable($tableName, $schemaName = null);
165
+
166
+ /**
167
+ * Create Varien_Db_Ddl_Table object by data from describe table
168
+ *
169
+ * @param $tableName
170
+ * @param $newTableName
171
+ * @return Varien_Db_Ddl_Table
172
+ */
173
+ public function createTableByDdl($tableName, $newTableName);
174
+
175
+ /**
176
+ * Modify the column definition by data from describe table
177
+ *
178
+ * @param string $tableName
179
+ * @param string $columnName
180
+ * @param array|string $definition
181
+ * @param boolean $flushData
182
+ * @param string $schemaName
183
+ * @return Varien_Db_Adapter_Pdo_Mysql
184
+ */
185
+ public function modifyColumnByDdl($tableName, $columnName, $definition, $flushData = false, $schemaName = null);
186
+
187
+ /**
188
+ * Rename table
189
+ *
190
+ * @param string $oldTableName
191
+ * @param string $newTableName
192
+ * @param string $schemaName
193
+ * @return boolean
194
+ */
195
+ public function renameTable($oldTableName, $newTableName, $schemaName = null);
196
+
197
+ /**
198
+ * Adds new column to the table.
199
+ *
200
+ * Generally $defintion must be array with column data to keep this call cross-DB compatible.
201
+ * Using string as $definition is allowed only for concrete DB adapter.
202
+ *
203
+ * @param string $tableName
204
+ * @param string $columnName
205
+ * @param array|string $definition string specific or universal array DB Server definition
206
+ * @param string $schemaName
207
+ * @return Varien_Db_Adapter_Interface
208
+ */
209
+ public function addColumn($tableName, $columnName, $definition, $schemaName = null);
210
+
211
+ /**
212
+ * Change the column name and definition
213
+ *
214
+ * For change definition of column - use modifyColumn
215
+ *
216
+ * @param string $tableName
217
+ * @param string $oldColumnName
218
+ * @param string $newColumnName
219
+ * @param array|string $definition
220
+ * @param boolean $flushData flush table statistic
221
+ * @param string $schemaName
222
+ * @return Varien_Db_Adapter_Interface
223
+ */
224
+ public function changeColumn($tableName, $oldColumnName, $newColumnName, $definition, $flushData = false,
225
+ $schemaName = null);
226
+
227
+ /**
228
+ * Modify the column definition
229
+ *
230
+ * @param string $tableName
231
+ * @param string $columnName
232
+ * @param array|string $definition
233
+ * @param boolean $flushData
234
+ * @param string $schemaName
235
+ * @return Varien_Db_Adapter_Interface
236
+ */
237
+ public function modifyColumn($tableName, $columnName, $definition, $flushData = false, $schemaName = null);
238
+
239
+ /**
240
+ * Drop the column from table
241
+ *
242
+ * @param string $tableName
243
+ * @param string $columnName
244
+ * @param string $schemaName
245
+ * @return boolean
246
+ */
247
+ public function dropColumn($tableName, $columnName, $schemaName = null);
248
+
249
+ /**
250
+ * Check is table column exists
251
+ *
252
+ * @param string $tableName
253
+ * @param string $columnName
254
+ * @param string $schemaName
255
+ * @return boolean
256
+ */
257
+ public function tableColumnExists($tableName, $columnName, $schemaName = null);
258
+
259
+ /**
260
+ * Add new index to table name
261
+ *
262
+ * @param string $tableName
263
+ * @param string $indexName
264
+ * @param string|array $fields the table column name or array of ones
265
+ * @param string $indexType the index type
266
+ * @param string $schemaName
267
+ * @return Zend_Db_Statement_Interface
268
+ */
269
+ public function addIndex($tableName, $indexName, $fields, $indexType = self::INDEX_TYPE_INDEX, $schemaName = null);
270
+
271
+ /**
272
+ * Drop the index from table
273
+ *
274
+ * @param string $tableName
275
+ * @param string $keyName
276
+ * @param string $schemaName
277
+ * @return bool|Zend_Db_Statement_Interface
278
+ */
279
+ public function dropIndex($tableName, $keyName, $schemaName = null);
280
+
281
+ /**
282
+ * Returns the table index information
283
+ *
284
+ * The return value is an associative array keyed by the UPPERCASE index key (except for primary key,
285
+ * that is always stored under 'PRIMARY' key) as returned by the RDBMS.
286
+ *
287
+ * The value of each array element is an associative array
288
+ * with the following keys:
289
+ *
290
+ * SCHEMA_NAME => string; name of database or schema
291
+ * TABLE_NAME => string; name of the table
292
+ * KEY_NAME => string; the original index name
293
+ * COLUMNS_LIST => array; array of index column names
294
+ * INDEX_TYPE => string; lowercase, create index type
295
+ * INDEX_METHOD => string; index method using
296
+ * type => string; see INDEX_TYPE
297
+ * fields => array; see COLUMNS_LIST
298
+ *
299
+ * @param string $tableName
300
+ * @param string $schemaName
301
+ * @return array
302
+ */
303
+ public function getIndexList($tableName, $schemaName = null);
304
+
305
+ /**
306
+ * Add new Foreign Key to table
307
+ * If Foreign Key with same name is exist - it will be deleted
308
+ *
309
+ * @param string $fkName
310
+ * @param string $tableName
311
+ * @param string $columnName
312
+ * @param string $refTableName
313
+ * @param string $refColumnName
314
+ * @param string $onDelete
315
+ * @param string $onUpdate
316
+ * @param boolean $purge trying remove invalid data
317
+ * @param string $schemaName
318
+ * @param string $refSchemaName
319
+ * @return Varien_Db_Adapter_Interface
320
+ */
321
+ public function addForeignKey($fkName, $tableName, $columnName, $refTableName, $refColumnName,
322
+ $onDelete = self::FK_ACTION_CASCADE, $onUpdate = self::FK_ACTION_CASCADE,
323
+ $purge = false, $schemaName = null, $refSchemaName = null);
324
+
325
+ /**
326
+ * Drop the Foreign Key from table
327
+ *
328
+ * @param string $tableName
329
+ * @param string $fkName
330
+ * @param string $schemaName
331
+ * @return Varien_Db_Adapter_Interface
332
+ */
333
+ public function dropForeignKey($tableName, $fkName, $schemaName = null);
334
+
335
+ /**
336
+ * Retrieve the foreign keys descriptions for a table.
337
+ *
338
+ * The return value is an associative array keyed by the UPPERCASE foreign key,
339
+ * as returned by the RDBMS.
340
+ *
341
+ * The value of each array element is an associative array
342
+ * with the following keys:
343
+ *
344
+ * FK_NAME => string; original foreign key name
345
+ * SCHEMA_NAME => string; name of database or schema
346
+ * TABLE_NAME => string;
347
+ * COLUMN_NAME => string; column name
348
+ * REF_SCHEMA_NAME => string; name of reference database or schema
349
+ * REF_TABLE_NAME => string; reference table name
350
+ * REF_COLUMN_NAME => string; reference column name
351
+ * ON_DELETE => string; action type on delete row
352
+ * ON_UPDATE => string; action type on update row
353
+ *
354
+ * @param string $tableName
355
+ * @param string $schemaName
356
+ * @return array
357
+ */
358
+ public function getForeignKeys($tableName, $schemaName = null);
359
+
360
+ /**
361
+ * Creates and returns a new Varien_Db_Select object for this adapter.
362
+ *
363
+ * @return Varien_Db_Select
364
+ */
365
+ public function select();
366
+
367
+ /**
368
+ * Inserts a table row with specified data.
369
+ *
370
+ * @param mixed $table The table to insert data into.
371
+ * @param array $data Column-value pairs or array of column-value pairs.
372
+ * @param arrat $fields update fields pairs or values
373
+ * @return int The number of affected rows.
374
+ */
375
+ public function insertOnDuplicate($table, array $data, array $fields = array());
376
+
377
+ /**
378
+ * Inserts a table multiply rows with specified data.
379
+ *
380
+ * @param mixed $table The table to insert data into.
381
+ * @param array $data Column-value pairs or array of Column-value pairs.
382
+ * @return int The number of affected rows.
383
+ */
384
+ public function insertMultiple($table, array $data);
385
+
386
+ /**
387
+ * Insert array to table based on columns definition
388
+ *
389
+ * @param string $table
390
+ * @param array $columns the data array column map
391
+ * @param array $data
392
+ * @return int
393
+ */
394
+ public function insertArray($table, array $columns, array $data);
395
+
396
+ /**
397
+ * Inserts a table row with specified data.
398
+ *
399
+ * @param mixed $table The table to insert data into.
400
+ * @param array $bind Column-value pairs.
401
+ * @return int The number of affected rows.
402
+ */
403
+ public function insert($table, array $bind);
404
+
405
+ /**
406
+ * Inserts a table row with specified data
407
+ * Special for Zero values to identity column
408
+ *
409
+ * @param string $table
410
+ * @param array $bind
411
+ * @return int The number of affected rows.
412
+ */
413
+ public function insertForce($table, array $bind);
414
+
415
+ /**
416
+ * Updates table rows with specified data based on a WHERE clause.
417
+ *
418
+ * @param mixed $table The table to update.
419
+ * @param array $bind Column-value pairs.
420
+ * @param mixed $where UPDATE WHERE clause(s).
421
+ * @return int The number of affected rows.
422
+ */
423
+ public function update($table, array $bind, $where = '');
424
+
425
+ /**
426
+ * Deletes table rows based on a WHERE clause.
427
+ *
428
+ * @param mixed $table The table to update.
429
+ * @param mixed $where DELETE WHERE clause(s).
430
+ * @return int The number of affected rows.
431
+ */
432
+ public function delete($table, $where = '');
433
+
434
+ /**
435
+ * Prepares and executes an SQL statement with bound data.
436
+ *
437
+ * @param mixed $sql The SQL statement with placeholders.
438
+ * May be a string or Zend_Db_Select.
439
+ * @param mixed $bind An array of data or data itself to bind to the placeholders.
440
+ * @return Zend_Db_Statement_Interface
441
+ */
442
+ public function query($sql, $bind = array());
443
+
444
+ /**
445
+ * Executes a SQL statement(s)
446
+ *
447
+ * @param string $sql
448
+ * @return Varien_Db_Adapter_Interface
449
+ */
450
+ public function multiQuery($sql);
451
+
452
+ /**
453
+ * Fetches all SQL result rows as a sequential array.
454
+ * Uses the current fetchMode for the adapter.
455
+ *
456
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
457
+ * @param mixed $bind Data to bind into SELECT placeholders.
458
+ * @param mixed $fetchMode Override current fetch mode.
459
+ * @return array
460
+ */
461
+ public function fetchAll($sql, $bind = array(), $fetchMode = null);
462
+
463
+ /**
464
+ * Fetches the first row of the SQL result.
465
+ * Uses the current fetchMode for the adapter.
466
+ *
467
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
468
+ * @param mixed $bind Data to bind into SELECT placeholders.
469
+ * @param mixed $fetchMode Override current fetch mode.
470
+ * @return array
471
+ */
472
+ public function fetchRow($sql, $bind = array(), $fetchMode = null);
473
+
474
+ /**
475
+ * Fetches all SQL result rows as an associative array.
476
+ *
477
+ * The first column is the key, the entire row array is the
478
+ * value. You should construct the query to be sure that
479
+ * the first column contains unique values, or else
480
+ * rows with duplicate values in the first column will
481
+ * overwrite previous data.
482
+ *
483
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
484
+ * @param mixed $bind Data to bind into SELECT placeholders.
485
+ * @return array
486
+ */
487
+ public function fetchAssoc($sql, $bind = array());
488
+
489
+ /**
490
+ * Fetches the first column of all SQL result rows as an array.
491
+ *
492
+ * The first column in each row is used as the array key.
493
+ *
494
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
495
+ * @param mixed $bind Data to bind into SELECT placeholders.
496
+ * @return array
497
+ */
498
+ public function fetchCol($sql, $bind = array());
499
+
500
+ /**
501
+ * Fetches all SQL result rows as an array of key-value pairs.
502
+ *
503
+ * The first column is the key, the second column is the
504
+ * value.
505
+ *
506
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
507
+ * @param mixed $bind Data to bind into SELECT placeholders.
508
+ * @return array
509
+ */
510
+ public function fetchPairs($sql, $bind = array());
511
+
512
+ /**
513
+ * Fetches the first column of the first row of the SQL result.
514
+ *
515
+ * @param string|Zend_Db_Select $sql An SQL SELECT statement.
516
+ * @param mixed $bind Data to bind into SELECT placeholders.
517
+ * @return string
518
+ */
519
+ public function fetchOne($sql, $bind = array());
520
+
521
+ /**
522
+ * Safely quotes a value for an SQL statement.
523
+ *
524
+ * If an array is passed as the value, the array values are quoted
525
+ * and then returned as a comma-separated string.
526
+ *
527
+ * @param mixed $value The value to quote.
528
+ * @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
529
+ * @return mixed An SQL-safe quoted value (or string of separated values).
530
+ */
531
+ public function quote($value, $type = null);
532
+
533
+ /**
534
+ * Quotes a value and places into a piece of text at a placeholder.
535
+ *
536
+ * The placeholder is a question-mark; all placeholders will be replaced
537
+ * with the quoted value. For example:
538
+ *
539
+ * <code>
540
+ * $text = "WHERE date < ?";
541
+ * $date = "2005-01-02";
542
+ * $safe = $sql->quoteInto($text, $date);
543
+ * // $safe = "WHERE date < '2005-01-02'"
544
+ * </code>
545
+ *
546
+ * @param string $text The text with a placeholder.
547
+ * @param mixed $value The value to quote.
548
+ * @param string $type OPTIONAL SQL datatype
549
+ * @param integer $count OPTIONAL count of placeholders to replace
550
+ * @return string An SQL-safe quoted value placed into the original text.
551
+ */
552
+ public function quoteInto($text, $value, $type = null, $count = null);
553
+
554
+ /**
555
+ * Quotes an identifier.
556
+ *
557
+ * Accepts a string representing a qualified indentifier. For Example:
558
+ * <code>
559
+ * $adapter->quoteIdentifier('myschema.mytable')
560
+ * </code>
561
+ * Returns: "myschema"."mytable"
562
+ *
563
+ * Or, an array of one or more identifiers that may form a qualified identifier:
564
+ * <code>
565
+ * $adapter->quoteIdentifier(array('myschema','my.table'))
566
+ * </code>
567
+ * Returns: "myschema"."my.table"
568
+ *
569
+ * The actual quote character surrounding the identifiers may vary depending on
570
+ * the adapter.
571
+ *
572
+ * @param string|array|Zend_Db_Expr $ident The identifier.
573
+ * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
574
+ * @return string The quoted identifier.
575
+ */
576
+ public function quoteIdentifier($ident, $auto = false);
577
+
578
+ /**
579
+ * Quote a column identifier and alias.
580
+ *
581
+ * @param string|array|Zend_Db_Expr $ident The identifier or expression.
582
+ * @param string $alias An alias for the column.
583
+ * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
584
+ * @return string The quoted identifier and alias.
585
+ */
586
+ public function quoteColumnAs($ident, $alias, $auto = false);
587
+
588
+ /**
589
+ * Quote a table identifier and alias.
590
+ *
591
+ * @param string|array|Zend_Db_Expr $ident The identifier or expression.
592
+ * @param string $alias An alias for the table.
593
+ * @param boolean $auto If true, heed the AUTO_QUOTE_IDENTIFIERS config option.
594
+ * @return string The quoted identifier and alias.
595
+ */
596
+ public function quoteTableAs($ident, $alias = null, $auto = false);
597
+
598
+ /**
599
+ * Format Date to internal database date format
600
+ *
601
+ * @param int|string|Zend_Date $date
602
+ * @param boolean $includeTime
603
+ * @return Zend_Db_Expr
604
+ */
605
+ public function formatDate($date, $includeTime = true);
606
+
607
+ /**
608
+ * Run additional environment before setup
609
+ *
610
+ * @return Varien_Db_Adapter_Interface
611
+ */
612
+ public function startSetup();
613
+
614
+ /**
615
+ * Run additional environment after setup
616
+ *
617
+ * @return Varien_Db_Adapter_Interface
618
+ */
619
+ public function endSetup();
620
+
621
+ /**
622
+ * Set cache adapter
623
+ *
624
+ * @param Zend_Cache_Backend_Interface $adapter
625
+ * @return Varien_Db_Adapter_Interface
626
+ */
627
+ public function setCacheAdapter($adapter);
628
+
629
+ /**
630
+ * Allow DDL caching
631
+ *
632
+ * @return Varien_Db_Adapter_Interface
633
+ */
634
+ public function allowDdlCache();
635
+
636
+ /**
637
+ * Disallow DDL caching
638
+ *
639
+ * @return Varien_Db_Adapter_Interface
640
+ */
641
+ public function disallowDdlCache();
642
+
643
+ /**
644
+ * Reset cached DDL data from cache
645
+ * if table name is null - reset all cached DDL data
646
+ *
647
+ * @param string $tableName
648
+ * @param string $schemaName OPTIONAL
649
+ * @return Varien_Db_Adapter_Interface
650
+ */
651
+ public function resetDdlCache($tableName = null, $schemaName = null);
652
+
653
+ /**
654
+ * Save DDL data into cache
655
+ *
656
+ * @param string $tableCacheKey
657
+ * @param int $ddlType
658
+ * @return Varien_Db_Adapter_Interface
659
+ */
660
+ public function saveDdlCache($tableCacheKey, $ddlType, $data);
661
+
662
+ /**
663
+ * Load DDL data from cache
664
+ * Return false if cache does not exists
665
+ *
666
+ * @param string $tableCacheKey the table cache key
667
+ * @param int $ddlType the DDL constant
668
+ * @return string|array|int|false
669
+ */
670
+ public function loadDdlCache($tableCacheKey, $ddlType);
671
+
672
+ /**
673
+ * Build SQL statement for condition
674
+ *
675
+ * If $condition integer or string - exact value will be filtered ('eq' condition)
676
+ *
677
+ * If $condition is array - one of the following structures is expected:
678
+ * - array("from" => $fromValue, "to" => $toValue)
679
+ * - array("eq" => $equalValue)
680
+ * - array("neq" => $notEqualValue)
681
+ * - array("like" => $likeValue)
682
+ * - array("in" => array($inValues))
683
+ * - array("nin" => array($notInValues))
684
+ * - array("notnull" => $valueIsNotNull)
685
+ * - array("null" => $valueIsNull)
686
+ * - array("moreq" => $moreOrEqualValue)
687
+ * - array("gt" => $greaterValue)
688
+ * - array("lt" => $lessValue)
689
+ * - array("gteq" => $greaterOrEqualValue)
690
+ * - array("lteq" => $lessOrEqualValue)
691
+ * - array("finset" => $valueInSet)
692
+ * - array("regexp" => $regularExpression)
693
+ * - array("seq" => $stringValue)
694
+ * - array("sneq" => $stringValue)
695
+ *
696
+ * If non matched - sequential array is expected and OR conditions
697
+ * will be built using above mentioned structure
698
+ *
699
+ * @param string $fieldName
700
+ * @param integer|string|array $condition
701
+ * @return string
702
+ */
703
+ public function prepareSqlCondition($fieldName, $condition);
704
+
705
+ /**
706
+ * Prepare value for save in column
707
+ * Return converted to column data type value
708
+ *
709
+ * @param array $column the column describe array
710
+ * @param mixed $value
711
+ * @return mixed
712
+ */
713
+ public function prepareColumnValue(array $column, $value);
714
+
715
+ /**
716
+ * Generate fragment of SQL, that check condition and return true or false value
717
+ *
718
+ * @param string $condition expression
719
+ * @param string $true true value
720
+ * @param string $false false value
721
+ * @return Zend_Db_Expr
722
+ */
723
+ public function getCheckSql($condition, $true, $false);
724
+
725
+ /**
726
+ * Returns valid IFNULL expression
727
+ *
728
+ * @param string $column
729
+ * @param string $value OPTIONAL. Applies when $expression is NULL
730
+ * @return Zend_Db_Expr
731
+ */
732
+
733
+ public function getIfNullSql($expression, $value = 0);
734
+
735
+ /**
736
+ * Generate fragment of SQL, that combine together (concatenate) the results from data array
737
+ * All arguments in data must be quoted
738
+ *
739
+ * @param array $data
740
+ * @param string $separator concatenate with separator
741
+ * @return Zend_Db_Expr
742
+ */
743
+ public function getConcatSql(array $data, $separator = null);
744
+
745
+ /**
746
+ * Generate fragment of SQL that returns length of character string
747
+ * The string argument must be quoted
748
+ *
749
+ * @param string $string
750
+ * @return Zend_Db_Expr
751
+ */
752
+ public function getLengthSql($string);
753
+
754
+ /**
755
+ * Generate fragment of SQL, that compare with two or more arguments, and returns the smallest
756
+ * (minimum-valued) argument
757
+ * All arguments in data must be quoted
758
+ *
759
+ * @param array $data
760
+ * @return Zend_Db_Expr
761
+ */
762
+ public function getLeastSql(array $data);
763
+
764
+ /**
765
+ * Generate fragment of SQL, that compare with two or more arguments, and returns the largest
766
+ * (maximum-valued) argument
767
+ * All arguments in data must be quoted
768
+ *
769
+ * @param array $data
770
+ * @return Zend_Db_Expr
771
+ */
772
+ public function getGreatestSql(array $data);
773
+
774
+ /**
775
+ * Add time values (intervals) to a date value
776
+ *
777
+ * @see INTERVAL_* constants for $unit
778
+ *
779
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
780
+ * @param int $interval
781
+ * @param string $unit
782
+ * @return Zend_Db_Expr
783
+ */
784
+ public function getDateAddSql($date, $interval, $unit);
785
+
786
+ /**
787
+ * Subtract time values (intervals) to a date value
788
+ *
789
+ * @see INTERVAL_* constants for $unit
790
+ *
791
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
792
+ * @param int|string $interval
793
+ * @param string $unit
794
+ * @return Zend_Db_Expr
795
+ */
796
+ public function getDateSubSql($date, $interval, $unit);
797
+
798
+ /**
799
+ * Format date as specified
800
+ *
801
+ * Supported format Specifier
802
+ *
803
+ * %H Hour (00..23)
804
+ * %i Minutes, numeric (00..59)
805
+ * %s Seconds (00..59)
806
+ * %d Day of the month, numeric (00..31)
807
+ * %m Month, numeric (00..12)
808
+ * %Y Year, numeric, four digits
809
+ *
810
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
811
+ * @param string $format
812
+ * @return Zend_Db_Expr
813
+ */
814
+ public function getDateFormatSql($date, $format);
815
+
816
+ /**
817
+ * Extract the date part of a date or datetime expression
818
+ *
819
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
820
+ * @return Zend_Db_Expr
821
+ */
822
+ public function getDatePartSql($date);
823
+
824
+ /**
825
+ * Extract part of a date
826
+ *
827
+ * @see INTERVAL_* constants for $unit
828
+ *
829
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
830
+ * @param string $unit
831
+ * @return Zend_Db_Expr
832
+ */
833
+ public function getDateExtractSql($date, $unit);
834
+
835
+ /**
836
+ * Retrieve valid table name
837
+ * Check table name length and allowed symbols
838
+ *
839
+ * @param string $tableName
840
+ * @return string
841
+ */
842
+ public function getTableName($tableName);
843
+
844
+ /**
845
+ * Retrieve valid index name
846
+ * Check index name length and allowed symbols
847
+ *
848
+ * @param string $tableName
849
+ * @param string|array $fields the columns list
850
+ * @param string $indexType
851
+ * @return string
852
+ */
853
+ public function getIndexName($tableName, $fields, $indexType = '');
854
+
855
+ /**
856
+ * Retrieve valid foreign key name
857
+ * Check foreign key name length and allowed symbols
858
+ *
859
+ * @param string $priTableName
860
+ * @param string $priColumnName
861
+ * @param string $refTableName
862
+ * @param string $refColumnName
863
+ * @return string
864
+ */
865
+ public function getForeignKeyName($priTableName, $priColumnName, $refTableName, $refColumnName);
866
+
867
+ /**
868
+ * Stop updating indexes
869
+ *
870
+ * @param string $tableName
871
+ * @param string $schemaName
872
+ * @return Varien_Db_Adapter_Interface
873
+ */
874
+ public function disableTableKeys($tableName, $schemaName = null);
875
+
876
+ /**
877
+ * Re-create missing indexes
878
+ *
879
+ * @param string $tableName
880
+ * @param string $schemaName
881
+ * @return Varien_Db_Adapter_Interface
882
+ */
883
+ public function enableTableKeys($tableName, $schemaName = null);
884
+
885
+ /**
886
+ * Get insert from Select object query
887
+ *
888
+ * @param Varien_Db_Select $select
889
+ * @param string $table insert into table
890
+ * @param array $fields
891
+ * @param int $mode
892
+ * @return string
893
+ */
894
+ public function insertFromSelect(Varien_Db_Select $select, $table, array $fields = array(), $mode = false);
895
+
896
+ /**
897
+ * Get update table query using select object for join and update
898
+ *
899
+ * @param Varien_Db_Select $select
900
+ * @param string|array $table
901
+ * @return string
902
+ */
903
+ public function updateFromSelect(Varien_Db_Select $select, $table);
904
+
905
+ /**
906
+ * Get delete from select object query
907
+ *
908
+ * @param Varien_Db_Select $select
909
+ * @param string $table the table name or alias used in select
910
+ * @return string|int
911
+ */
912
+ public function deleteFromSelect(Varien_Db_Select $select, $table);
913
+
914
+ /**
915
+ * Return array of table(s) checksum as table name - checksum pairs
916
+ *
917
+ * @param array|string $tableNames
918
+ * @param string $schemaName
919
+ * @return array
920
+ */
921
+ public function getTablesChecksum($tableNames, $schemaName = null);
922
+
923
+ /**
924
+ * Check if the database support STRAIGHT JOIN
925
+ *
926
+ * @return boolean
927
+ */
928
+ public function supportStraightJoin();
929
+
930
+ /**
931
+ * Adds order by random to select object
932
+ * Possible using integer field for optimization
933
+ *
934
+ * @param Varien_Db_Select $select
935
+ * @param string $field
936
+ * @return Varien_Db_Adapter_Interface
937
+ */
938
+ public function orderRand(Varien_Db_Select $select, $field = null);
939
+
940
+ /**
941
+ * Render SQL FOR UPDATE clause
942
+ *
943
+ * @param string $sql
944
+ * @return string
945
+ */
946
+ public function forUpdate($sql);
947
+
948
+ /**
949
+ * Try to find installed primary key name, if not - formate new one.
950
+ *
951
+ * @param string $tableName Table name
952
+ * @param string $schemaName OPTIONAL
953
+ * @return string Primary Key name
954
+ */
955
+ public function getPrimaryKeyName($tableName, $schemaName = null);
956
+
957
+ /**
958
+ * Converts fetched blob into raw binary PHP data.
959
+ * Some DB drivers return blobs as hex-coded strings, so we need to process them.
960
+ *
961
+ * @mixed $value
962
+ * @return mixed
963
+ */
964
+ public function decodeVarbinary($value);
965
+ }
lib/Varien/Db/Adapter/Mysqli.php ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli
29
+ {
30
+ const ISO_DATE_FORMAT = 'yyyy-MM-dd';
31
+ const ISO_DATETIME_FORMAT = 'yyyy-MM-dd HH-mm-ss';
32
+
33
+ /**
34
+ * Creates a real connection to the database with multi-query capability.
35
+ *
36
+ * @return void
37
+ * @throws Zend_Db_Adapter_Mysqli_Exception
38
+ */
39
+ protected function _connect()
40
+ {
41
+ if ($this->_connection) {
42
+ return;
43
+ }
44
+ if (!extension_loaded('mysqli')) {
45
+ throw new Zend_Db_Adapter_Exception('mysqli extension is not installed');
46
+ }
47
+ // Suppress connection warnings here.
48
+ // Throw an exception instead.
49
+ @$conn = new mysqli();
50
+ if (false===$conn || mysqli_connect_errno()) {
51
+ throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_errno());
52
+ }
53
+
54
+ $conn->init();
55
+ $conn->options(MYSQLI_OPT_LOCAL_INFILE, true);
56
+ #$conn->options(MYSQLI_CLIENT_MULTI_QUERIES, true);
57
+
58
+ $port = !empty($this->_config['port']) ? $this->_config['port'] : null;
59
+ $socket = !empty($this->_config['unix_socket']) ? $this->_config['unix_socket'] : null;
60
+ // socket specified in host config
61
+ if (strpos($this->_config['host'], '/')!==false) {
62
+ $socket = $this->_config['host'];
63
+ $this->_config['host'] = null;
64
+ } elseif (strpos($this->_config['host'], ':')!==false) {
65
+ list($this->_config['host'], $port) = explode(':', $this->_config['host']);
66
+ }
67
+
68
+ #echo "<pre>".print_r($this->_config,1)."</pre>"; die;
69
+ @$conn->real_connect(
70
+ $this->_config['host'],
71
+ $this->_config['username'],
72
+ $this->_config['password'],
73
+ $this->_config['dbname'],
74
+ $port,
75
+ $socket
76
+ );
77
+ if (mysqli_connect_errno()) {
78
+ throw new Zend_Db_Adapter_Mysqli_Exception(mysqli_connect_error());
79
+ }
80
+
81
+ $this->_connection = $conn;
82
+
83
+ /** @link http://bugs.mysql.com/bug.php?id=18551 */
84
+ $this->_connection->query("SET SQL_MODE=''");
85
+ }
86
+
87
+ /**
88
+ * Run RAW Query
89
+ *
90
+ * @param string $sql
91
+ * @return Zend_Db_Statement_Interface
92
+ */
93
+ public function raw_query($sql)
94
+ {
95
+ $timeoutMessage = 'SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction';
96
+ $tries = 0;
97
+ do {
98
+ $retry = false;
99
+ try {
100
+ $this->clear_result();
101
+ $result = $this->getConnection()->query($sql);
102
+ $this->clear_result();
103
+ } catch (Exception $e) {
104
+ if ($tries < 10 && $e->getMessage() == $timeoutMessage) {
105
+ $retry = true;
106
+ $tries++;
107
+ } else {
108
+ throw $e;
109
+ }
110
+ }
111
+ } while ($retry);
112
+
113
+ return $result;
114
+ }
115
+
116
+
117
+ public function convertDate($date)
118
+ {
119
+ if ($date instanceof Zend_Date) {
120
+ return $date->toString(self::ISO_DATE_FORMAT);
121
+ }
122
+ return strftime('%Y-%m-%d', strtotime($date));
123
+ }
124
+
125
+ public function convertDateTime($datetime)
126
+ {
127
+ if ($datetime instanceof Zend_Date) {
128
+ return $datetime->toString(self::ISO_DATETIME_FORMAT);
129
+ }
130
+ return strftime('%Y-%m-%d %H:%M:%S', strtotime($datetime));
131
+ }
132
+
133
+
134
+
135
+ public function raw_fetchRow($sql, $field=null)
136
+ {
137
+ if (!$result = $this->raw_query($sql)) {
138
+ return false;
139
+ }
140
+ if (!$row = $result->fetch_assoc()) {
141
+ return false;
142
+ }
143
+ if (empty($field)) {
144
+ return $row;
145
+ } else {
146
+ return isset($row[$field]) ? $row[$field] : false;
147
+ }
148
+ }
149
+
150
+ public function multi_query($sql)
151
+ {
152
+ $this->beginTransaction();
153
+ try {
154
+ $this->clear_result();
155
+ if ($this->getConnection()->multi_query($sql)) {
156
+ $this->clear_result();
157
+ $this->commit();
158
+ } else {
159
+ throw new Zend_Db_Adapter_Mysqli_Exception('multi_query: '.$this->getConnection()->error);
160
+ }
161
+ } catch (Exception $e) {
162
+ $this->rollback();
163
+ throw $e;
164
+ }
165
+
166
+ return true;
167
+ }
168
+
169
+ public function clear_result()
170
+ {
171
+ while ($this->getConnection()->next_result()) {
172
+ if ($result = $this->getConnection()->store_result()) {
173
+ $result->free_result();
174
+ } elseif($this->getConnection()->error) {
175
+ throw new Zend_Db_Adapter_Mysqli_Exception('clear_result: '.$this->getConnection()->error);
176
+ }
177
+ }
178
+
179
+ }
180
+
181
+ public function dropForeignKey($table, $fk)
182
+ {
183
+ $create = $this->raw_fetchRow("show create table `$table`", 'Create Table');
184
+ if (strpos($create, "CONSTRAINT `$fk` FOREIGN KEY (")!==false) {
185
+ return $this->raw_query("ALTER TABLE `$table` DROP FOREIGN KEY `$fk`");
186
+ }
187
+ return true;
188
+ }
189
+
190
+ public function dropKey($table, $key)
191
+ {
192
+ $create = $this->raw_fetchRow("show create table `$table`", 'Create Table');
193
+ if (strpos($create, "KEY `$key` (")!==false) {
194
+ return $this->raw_query("ALTER TABLE `$table` DROP KEY `$key`");
195
+ }
196
+ return true;
197
+ }
198
+
199
+ /**
200
+ * ADD CONSTRAINT
201
+ *
202
+ *
203
+ * @param string $fkName
204
+ * @param string $tableName
205
+ * @param string $keyName
206
+ * @param string $refTableName
207
+ * @param string $refKeyName
208
+ * @param string $onUpdate
209
+ * @param string $onDelete
210
+ */
211
+ public function addConstraint($fkName, $tableName, $keyName, $refTableName,
212
+ $refKeyName, $onDelete = 'cascade', $onUpdate = 'cascade')
213
+ {
214
+ if (substr($fkName, 0, 3) != 'FK_') {
215
+ $fkName = 'FK_' . $fkName;
216
+ }
217
+
218
+ $sql = 'ALTER TABLE `'.$tableName.'` ADD CONSTRAINT `'.$fkName.'`'
219
+ . 'FOREIGN KEY (`'.$keyName.'`) REFERENCES `'.$refTableName.'` (`'.$refKeyName.'`)';
220
+ if (!is_null($onDelete)) {
221
+ $sql .= ' ON DELETE ' . strtoupper($onDelete);
222
+ }
223
+ if (!is_null($onUpdate)) {
224
+ $sql .= ' ON UPDATE ' . strtoupper($onUpdate);
225
+ }
226
+
227
+ return $this->raw_query($sql);
228
+ }
229
+
230
+ public function tableColumnExists($tableName, $columnName)
231
+ {
232
+ foreach ($this->fetchAll('DESCRIBE `'.$tableName.'`') as $row) {
233
+ if ($row['Field'] == $columnName) {
234
+ return true;
235
+ }
236
+ }
237
+ return false;
238
+ }
239
+
240
+ public function addColumn($tableName, $columnName, $definition)
241
+ {
242
+ if ($this->tableColumnExists($tableName, $columnName)) {
243
+ return true;
244
+ }
245
+ $result = $this->raw_query("alter table `$tableName` add column `$columnName` ".$definition);
246
+ return $result;
247
+ }
248
+
249
+ public function dropColumn($tableName, $columnName)
250
+ {
251
+ if (!$this->tableColumnExists($tableName, $columnName)) {
252
+ return true;
253
+ }
254
+
255
+ $create = $this->raw_fetchRow('SHOW CREATE TABLE `'.$tableName.'`', 'Create Table');
256
+
257
+ $alterDrop = array();
258
+ $alterDrop[] = 'DROP COLUMN `'.$columnName.'`';
259
+
260
+ /**
261
+ * find foreign keys for column
262
+ */
263
+ $matches = array();
264
+ preg_match_all('/CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\)/', $create, $matches, PREG_SET_ORDER);
265
+ foreach ($matches as $match) {
266
+ if ($match[2] == $columnName) {
267
+ $alterDrop[] = 'DROP FOREIGN KEY `'.$match[1].'`';
268
+ }
269
+ }
270
+
271
+ return $this->raw_query('ALTER TABLE `'.$tableName.'` ' . join(', ', $alterDrop));
272
+ }
273
+
274
+ /**
275
+ * Creates and returns a new Zend_Db_Select object for this adapter.
276
+ *
277
+ * @return Varien_Db_Select
278
+ */
279
+ public function select()
280
+ {
281
+ return new Varien_Db_Select($this);
282
+ }
283
+ }
lib/Varien/Db/Adapter/Pdo/Mysql.php ADDED
@@ -0,0 +1,3488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Mysql PDO DB adapter
29
+ */
30
+ class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements Varien_Db_Adapter_Interface
31
+ {
32
+ const DEBUG_CONNECT = 0;
33
+ const DEBUG_TRANSACTION = 1;
34
+ const DEBUG_QUERY = 2;
35
+
36
+ const TIMESTAMP_FORMAT = 'Y-m-d H:i:s';
37
+ const DATETIME_FORMAT = 'Y-m-d H:i:s';
38
+ const DATE_FORMAT = 'Y-m-d';
39
+
40
+ const DDL_DESCRIBE = 1;
41
+ const DDL_CREATE = 2;
42
+ const DDL_INDEX = 3;
43
+ const DDL_FOREIGN_KEY = 4;
44
+ const DDL_CACHE_PREFIX = 'DB_PDO_MYSQL_DDL';
45
+ const DDL_CACHE_TAG = 'DB_PDO_MYSQL_DDL';
46
+
47
+ const LENGTH_TABLE_NAME = 64;
48
+ const LENGTH_INDEX_NAME = 64;
49
+ const LENGTH_FOREIGN_NAME = 64;
50
+
51
+ /**
52
+ * Default class name for a DB statement.
53
+ *
54
+ * @var string
55
+ */
56
+ protected $_defaultStmtClass = 'Varien_Db_Statement_Pdo_Mysql';
57
+
58
+ /**
59
+ * Current Transaction Level
60
+ *
61
+ * @var int
62
+ */
63
+ protected $_transactionLevel = 0;
64
+
65
+ /**
66
+ * Set attribute to connection flag
67
+ *
68
+ * @var bool
69
+ */
70
+ protected $_connectionFlagsSet = false;
71
+
72
+ /**
73
+ * Tables DDL cache
74
+ *
75
+ * @var array
76
+ */
77
+ protected $_ddlCache = array();
78
+
79
+ /**
80
+ * SQL bind params. Used temporarily by regexp callback.
81
+ *
82
+ * @var array
83
+ */
84
+ protected $_bindParams = array();
85
+
86
+ /**
87
+ * Autoincrement for bind value. Used by regexp callback.
88
+ *
89
+ * @var int
90
+ */
91
+ protected $_bindIncrement = 0;
92
+
93
+ /**
94
+ * Write SQL debug data to file
95
+ *
96
+ * @var bool
97
+ */
98
+ protected $_debug = false;
99
+
100
+ /**
101
+ * Minimum query duration time to be logged
102
+ *
103
+ * @var float
104
+ */
105
+ protected $_logQueryTime = 0.05;
106
+
107
+ /**
108
+ * Log all queries (ignored minimum query duration time)
109
+ *
110
+ * @var bool
111
+ */
112
+ protected $_logAllQueries = false;
113
+
114
+ /**
115
+ * Add to log call stack data (backtrace)
116
+ *
117
+ * @var bool
118
+ */
119
+ protected $_logCallStack = false;
120
+
121
+ /**
122
+ * Path to SQL debug data log
123
+ *
124
+ * @var string
125
+ */
126
+ protected $_debugFile = 'var/debug/pdo_mysql.log';
127
+
128
+ /**
129
+ * Io File Adapter
130
+ *
131
+ * @var Varien_Io_File
132
+ */
133
+ protected $_debugIoAdapter;
134
+
135
+ /**
136
+ * Debug timer start value
137
+ *
138
+ * @var float
139
+ */
140
+ protected $_debugTimer = 0;
141
+
142
+ /**
143
+ * Cache frontend adapter instance
144
+ *
145
+ * @var Zend_Cache_Core
146
+ */
147
+ protected $_cacheAdapter;
148
+
149
+ /**
150
+ * DDL cache allowing flag
151
+ * @var bool
152
+ */
153
+ protected $_isDdlCacheAllowed = true;
154
+
155
+ /**
156
+ * MySQL column - Table DDL type pairs
157
+ *
158
+ * @var array
159
+ */
160
+ protected $_ddlColumnTypes = array(
161
+ Varien_Db_Ddl_Table::TYPE_BOOLEAN => 'bool',
162
+ Varien_Db_Ddl_Table::TYPE_SMALLINT => 'smallint',
163
+ Varien_Db_Ddl_Table::TYPE_INTEGER => 'int',
164
+ Varien_Db_Ddl_Table::TYPE_BIGINT => 'bigint',
165
+ Varien_Db_Ddl_Table::TYPE_FLOAT => 'float',
166
+ Varien_Db_Ddl_Table::TYPE_DECIMAL => 'decimal',
167
+ Varien_Db_Ddl_Table::TYPE_NUMERIC => 'decimal',
168
+ Varien_Db_Ddl_Table::TYPE_DATE => 'date',
169
+ Varien_Db_Ddl_Table::TYPE_TIMESTAMP => 'timestamp',
170
+ Varien_Db_Ddl_Table::TYPE_DATETIME => 'datetime',
171
+ Varien_Db_Ddl_Table::TYPE_TEXT => 'text',
172
+ Varien_Db_Ddl_Table::TYPE_BLOB => 'blob',
173
+ Varien_Db_Ddl_Table::TYPE_VARBINARY => 'blob'
174
+ );
175
+
176
+ /**
177
+ * Allowed interval units array
178
+ *
179
+ * @var array
180
+ */
181
+ protected $_intervalUnits = array(
182
+ self::INTERVAL_YEAR => 'YEAR',
183
+ self::INTERVAL_MONTH => 'MONTH',
184
+ self::INTERVAL_DAY => 'DAY',
185
+ self::INTERVAL_HOUR => 'HOUR',
186
+ self::INTERVAL_MINUTE => 'MINUTE',
187
+ self::INTERVAL_SECOND => 'SECOND',
188
+ );
189
+
190
+ /**
191
+ * Hook callback to modify queries. Mysql specific property, designed only for backwards compatibility.
192
+ *
193
+ * @var array|null
194
+ */
195
+ protected $_queryHook = null;
196
+
197
+ /**
198
+ * Begin new DB transaction for connection
199
+ *
200
+ * @return Varien_Db_Adapter_Pdo_Mysql
201
+ */
202
+ public function beginTransaction()
203
+ {
204
+ if ($this->_transactionLevel === 0) {
205
+ $this->_debugTimer();
206
+ parent::beginTransaction();
207
+ $this->_debugStat(self::DEBUG_TRANSACTION, 'BEGIN');
208
+ }
209
+ ++$this->_transactionLevel;
210
+ return $this;
211
+ }
212
+
213
+ /**
214
+ * Commit DB transaction
215
+ *
216
+ * @return Varien_Db_Adapter_Pdo_Mysql
217
+ */
218
+ public function commit()
219
+ {
220
+ if ($this->_transactionLevel === 1) {
221
+ $this->_debugTimer();
222
+ parent::commit();
223
+ $this->_debugStat(self::DEBUG_TRANSACTION, 'COMMIT');
224
+ }
225
+ --$this->_transactionLevel;
226
+ return $this;
227
+ }
228
+
229
+ /**
230
+ * Rollback DB transaction
231
+ *
232
+ * @return Varien_Db_Adapter_Pdo_Mysql
233
+ */
234
+ public function rollback()
235
+ {
236
+ if ($this->_transactionLevel === 1) {
237
+ $this->_debugTimer();
238
+ parent::rollback();
239
+ $this->_debugStat(self::DEBUG_TRANSACTION, 'ROLLBACK');
240
+ }
241
+ --$this->_transactionLevel;
242
+ return $this;
243
+ }
244
+
245
+ /**
246
+ * Get adapter transaction level state. Return 0 if all transactions are complete
247
+ *
248
+ * @return int
249
+ */
250
+ public function getTransactionLevel()
251
+ {
252
+ return $this->_transactionLevel;
253
+ }
254
+
255
+ /**
256
+ * Convert date to DB format
257
+ *
258
+ * @param mixed $date
259
+ * @return string
260
+ */
261
+ public function convertDate($date)
262
+ {
263
+ return $this->formatDate($date, false);
264
+ }
265
+
266
+ /**
267
+ * Convert date and time to DB format
268
+ *
269
+ * @param mixed $date
270
+ * @return string
271
+ */
272
+ public function convertDateTime($datetime)
273
+ {
274
+ return $this->formatDate($datetime, true);
275
+ }
276
+
277
+ /**
278
+ * Creates a PDO object and connects to the database.
279
+ *
280
+ * @throws Zend_Db_Adapter_Exception
281
+ */
282
+ protected function _connect()
283
+ {
284
+ if ($this->_connection) {
285
+ return;
286
+ }
287
+
288
+ if (!extension_loaded('pdo_mysql')) {
289
+ throw new Zend_Db_Adapter_Exception('pdo_mysql extension is not installed');
290
+ }
291
+
292
+ if (strpos($this->_config['host'], '/') !== false) {
293
+ $this->_config['unix_socket'] = $this->_config['host'];
294
+ unset($this->_config['host']);
295
+ } else if (strpos($this->_config['host'], ':') !== false) {
296
+ list($this->_config['host'], $this->_config['port']) = explode(':', $this->_config['host']);
297
+ }
298
+
299
+ $this->_debugTimer();
300
+ parent::_connect();
301
+ $this->_debugStat(self::DEBUG_CONNECT, '');
302
+
303
+ /** @link http://bugs.mysql.com/bug.php?id=18551 */
304
+ $this->_connection->query("SET SQL_MODE=''");
305
+
306
+ if (!$this->_connectionFlagsSet) {
307
+ $this->_connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
308
+ $this->_connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
309
+ $this->_connectionFlagsSet = true;
310
+ }
311
+ }
312
+
313
+ /**
314
+ * Run RAW Query
315
+ *
316
+ * @param string $sql
317
+ * @return Zend_Db_Statement_Interface
318
+ * @throws PDOException
319
+ */
320
+ public function raw_query($sql)
321
+ {
322
+ $lostConnectionMessage = 'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query';
323
+ $tries = 0;
324
+ do {
325
+ $retry = false;
326
+ try {
327
+ $result = $this->query($sql);
328
+ } catch (Exception $e) {
329
+ // Convert to PDOException to maintain backwards compatibility with usage of MySQL adapter
330
+ if ($e instanceof Zend_Db_Statement_Exception) {
331
+ $e = $e->getPrevious();
332
+ if (!($e instanceof PDOException)) {
333
+ $e = new PDOException($e->getMessage(), $e->getCode());
334
+ }
335
+ }
336
+ // Check to reconnect
337
+ if ($tries < 10 && $e->getMessage() == $lostConnectionMessage) {
338
+ $retry = true;
339
+ $tries++;
340
+ } else {
341
+ throw $e;
342
+ }
343
+ }
344
+ } while ($retry);
345
+
346
+ return $result;
347
+ }
348
+
349
+ /**
350
+ * Run RAW query and Fetch First row
351
+ *
352
+ * @param string $sql
353
+ * @param string|int $field
354
+ * @return boolean
355
+ */
356
+ public function raw_fetchRow($sql, $field = null)
357
+ {
358
+ $result = $this->raw_query($sql);
359
+ if (!$result) {
360
+ return false;
361
+ }
362
+
363
+ $row = $result->fetch(PDO::FETCH_ASSOC);
364
+ if (!$row) {
365
+ return false;
366
+ }
367
+
368
+ if (empty($field)) {
369
+ return $row;
370
+ } else {
371
+ return isset($row[$field]) ? $row[$field] : false;
372
+ }
373
+ }
374
+
375
+ /**
376
+ * Special handling for PDO query().
377
+ * All bind parameter names must begin with ':'.
378
+ *
379
+ * @param string|Zend_Db_Select $sql The SQL statement with placeholders.
380
+ * @param mixed $bind An array of data or data itself to bind to the placeholders.
381
+ * @return Zend_Db_Pdo_Statement
382
+ * @throws Zend_Db_Adapter_Exception To re-throw PDOException.
383
+ */
384
+ public function query($sql, $bind = array())
385
+ {
386
+ $this->_debugTimer();
387
+ try {
388
+ $this->_prepareQuery($sql, $bind);
389
+ $result = parent::query($sql, $bind);
390
+ } catch (Exception $e) {
391
+ $this->_debugStat(self::DEBUG_QUERY, $sql, $bind);
392
+ $this->_debugException($e);
393
+ }
394
+ $this->_debugStat(self::DEBUG_QUERY, $sql, $bind, $result);
395
+ return $result;
396
+ }
397
+
398
+ /**
399
+ * Prepares SQL query by moving to bind all special parameters that can be confused with bind placeholders
400
+ * (e.g. "foo:bar"). And also changes named bind to positional one, because underlying library has problems
401
+ * with named binds.
402
+ *
403
+ * @param Zend_Db_Select|string $sql
404
+ * @param mixed $bind
405
+ * @return Varien_Db_Adapter_Pdo_Mysql
406
+ */
407
+ protected function _prepareQuery(&$sql, &$bind = array())
408
+ {
409
+ $sql = (string) $sql;
410
+ if (!is_array($bind)) {
411
+ $bind = array($bind);
412
+ }
413
+
414
+ // Mixed bind is not supported - so remember whether it is named bind, to normalize later if required
415
+ $isNamedBind = false;
416
+ if ($bind) {
417
+ foreach ($bind as $k => $v) {
418
+ if (!is_int($k)) {
419
+ $isNamedBind = true;
420
+ if ($k[0] != ':') {
421
+ $bind[":{$k}"] = $v;
422
+ unset($bind[$k]);
423
+ }
424
+ }
425
+ }
426
+ }
427
+
428
+ if (strpos($sql, ':') !== false || strpos($sql, '?') !== false) {
429
+ $before = count($bind);
430
+ $this->_bindParams = $bind; // Used by callback
431
+ $sql = preg_replace_callback('#(([\'"])((\\2)|((.*?[^\\\\])\\2)))#',
432
+ array($this, 'proccessBindCallback'),
433
+ $sql);
434
+ Varien_Exception::processPcreError();
435
+ $bind = $this->_bindParams;
436
+
437
+ // If _processBindCallbacks() has added named entries to positional bind - normalize it to positional
438
+ if (!$isNamedBind && $before && (count($bind) != $before)) {
439
+ $this->_convertMixedBind($sql, $bind);
440
+ }
441
+ }
442
+
443
+ // Special query hook
444
+ if ($this->_queryHook) {
445
+ $object = $this->_queryHook['object'];
446
+ $method = $this->_queryHook['method'];
447
+ $object->$method($sql, $bind);
448
+ }
449
+
450
+ return $this;
451
+ }
452
+
453
+ /**
454
+ * Callback function for preparation of query and bind by regexp.
455
+ * Checks query parameters for special symbols and moves such parameters to bind array as named ones.
456
+ * This method writes to $_bindParams, where query bind parameters are kept.
457
+ * This method requires further normalizing, if bind array is positional.
458
+ *
459
+ * @param array $matches
460
+ * @return string
461
+ */
462
+ public function proccessBindCallback($matches)
463
+ {
464
+ if (isset($matches[6]) && (
465
+ strpos($matches[6], "'") !== false ||
466
+ strpos($matches[6], ':') !== false ||
467
+ strpos($matches[6], '?') !== false)
468
+ ) {
469
+ $bindName = ':_mage_bind_var_' . (++$this->_bindIncrement);
470
+ $this->_bindParams[$bindName] = $this->_unQuote($matches[6]);
471
+ return ' ' . $bindName;
472
+ }
473
+ return $matches[0];
474
+ }
475
+
476
+ /**
477
+ * Unquote raw string (use for auto-bind)
478
+ *
479
+ * @param string $string
480
+ * @return string
481
+ */
482
+ protected function _unQuote($string)
483
+ {
484
+ $translate = array(
485
+ "\\000" => "\000",
486
+ "\\n" => "\n",
487
+ "\\r" => "\r",
488
+ "\\\\" => "\\",
489
+ "\'" => "'",
490
+ "\\\"" => "\"",
491
+ "\\032" => "\032"
492
+ );
493
+ return strtr($string, $translate);
494
+ }
495
+
496
+ /**
497
+ * Normalizes mixed positional-named bind to positional bind, and replaces named placeholders in query to
498
+ * '?' placeholders.
499
+ *
500
+ * @param string $sql
501
+ * @param array $bind
502
+ * @return Varien_Db_Adapter_Pdo_Mysql
503
+ */
504
+ protected function _convertMixedBind(&$sql, &$bind)
505
+ {
506
+ $positions = array();
507
+ $offset = 0;
508
+ // get positions
509
+ while (true) {
510
+ $pos = strpos($sql, '?', $offset);
511
+ if ($pos !== false) {
512
+ $positions[] = $pos;
513
+ $offset = ++$pos;
514
+ } else {
515
+ break;
516
+ }
517
+ }
518
+
519
+ $bindResult = array();
520
+ $map = array();
521
+ foreach ($bind as $k => $v) {
522
+ // positional
523
+ if (is_int($k)) {
524
+ if (!isset($positions[$k])) {
525
+ continue;
526
+ }
527
+ $bindResult[$positions[$k]] = $v;
528
+ } else {
529
+ $offset = 0;
530
+ while (true) {
531
+ $pos = strpos($sql, $k, $offset);
532
+ if ($pos === false) {
533
+ break;
534
+ } else {
535
+ $offset = $pos + strlen($k);
536
+ $bindResult[$pos] = $v;
537
+ }
538
+ }
539
+ $map[$k] = '?';
540
+ }
541
+ }
542
+
543
+ ksort($bindResult);
544
+ $bind = array_values($bindResult);
545
+ $sql = strtr($sql, $map);
546
+
547
+ return $this;
548
+ }
549
+
550
+ /**
551
+ * Sets (removes) query hook.
552
+ *
553
+ * $hook must be either array with 'object' and 'method' entries, or null to remove hook.
554
+ * Previous hook is returned.
555
+ *
556
+ * @param array $hook
557
+ * @return mixed
558
+ */
559
+ public function setQueryHook($hook)
560
+ {
561
+ $prev = $this->_queryHook;
562
+ $this->_queryHook = $hook;
563
+ return $prev;
564
+ }
565
+
566
+ /**
567
+ * Executes a SQL statement(s)
568
+ *
569
+ * @param string $sql
570
+ * @throws Zend_Db_Exception
571
+ * @return Varien_Db_Adapter_Pdo_Mysql
572
+ */
573
+ public function multiQuery($sql)
574
+ {
575
+ return $this->multi_query($sql);
576
+ }
577
+
578
+ /**
579
+ * Run Multi Query
580
+ *
581
+ * @param string $sql
582
+ * @return array
583
+ */
584
+ public function multi_query($sql)
585
+ {
586
+ ##$result = $this->raw_query($sql);
587
+
588
+ #$this->beginTransaction();
589
+ try {
590
+ $stmts = $this->_splitMultiQuery($sql);
591
+ $result = array();
592
+ foreach ($stmts as $stmt) {
593
+ $result[] = $this->raw_query($stmt);
594
+ }
595
+ #$this->commit();
596
+ } catch (Exception $e) {
597
+ #$this->rollback();
598
+ throw $e;
599
+ }
600
+
601
+ $this->resetDdlCache();
602
+
603
+ return $result;
604
+ }
605
+
606
+ /**
607
+ * Split multi statement query
608
+ *
609
+ * @param $sql string
610
+ * @return array
611
+ */
612
+ protected function _splitMultiQuery($sql)
613
+ {
614
+ $parts = preg_split('#(;|\'|"|\\\\|//|--|\n|/\*|\*/)#', $sql, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
615
+
616
+ $q = false;
617
+ $c = false;
618
+ $stmts = array();
619
+ $s = '';
620
+
621
+ foreach ($parts as $i => $part) {
622
+ // strings
623
+ if (($part === "'" || $part === '"') && ($i === 0 || $parts[$i-1] !== '\\')) {
624
+ if ($q === false) {
625
+ $q = $part;
626
+ } elseif ($q === $part) {
627
+ $q = false;
628
+ }
629
+ }
630
+
631
+ // single line comments
632
+ if (($part === '//' || $part === '--') && ($i === 0 || $parts[$i-1] === "\n")) {
633
+ $c = $part;
634
+ } elseif ($part === "\n" && ($c === '//' || $c === '--')) {
635
+ $c = false;
636
+ }
637
+
638
+ // multi line comments
639
+ if ($part === '/*' && $c === false) {
640
+ $c = '/*';
641
+ } elseif ($part === '*/' && $c === '/*') {
642
+ $c = false;
643
+ }
644
+
645
+ // statements
646
+ if ($part === ';' && $q === false && $c === false) {
647
+ if (trim($s)!=='') {
648
+ $stmts[] = trim($s);
649
+ $s = '';
650
+ }
651
+ } else {
652
+ $s .= $part;
653
+ }
654
+ }
655
+ if (trim($s) !== '') {
656
+ $stmts[] = trim($s);
657
+ }
658
+
659
+ return $stmts;
660
+ }
661
+
662
+ /**
663
+ * Drop the Foreign Key from table
664
+ *
665
+ * @param string $tableName
666
+ * @param string $fkName
667
+ * @param string $schemaName
668
+ * @return Varien_Db_Adapter_Pdo_Mysql
669
+ */
670
+ public function dropForeignKey($tableName, $fkName, $schemaName = null)
671
+ {
672
+ $foreignKeys = $this->getForeignKeys($tableName, $schemaName);
673
+ $fkName = strtoupper($fkName);
674
+ if (isset($foreignKeys[$fkName])) {
675
+ $sql = sprintf('ALTER TABLE %s DROP FOREIGN KEY %s',
676
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
677
+ $this->quoteIdentifier($foreignKeys[$fkName]['FK_NAME'])
678
+ );
679
+ $this->resetDdlCache($tableName, $schemaName);
680
+ $this->raw_query($sql);
681
+ }
682
+ return $this;
683
+ }
684
+
685
+ /**
686
+ * Delete index from a table if it exists
687
+ *
688
+ * @deprecated since 1.4.0.1
689
+ * @param string $tableName
690
+ * @param string $keyName
691
+ * @param string $schemaName
692
+ * @return bool|Zend_Db_Statement_Interface
693
+ */
694
+ public function dropKey($tableName, $keyName, $schemaName = null)
695
+ {
696
+ return $this->dropIndex($tableName, $keyName, $schemaName);
697
+ }
698
+
699
+ /**
700
+ * Prepare table before add constraint foreign key
701
+ *
702
+ * @param string $tableName
703
+ * @param string $columnName
704
+ * @param string $refTableName
705
+ * @param string $refColumnName
706
+ * @param string $onDelete
707
+ * @return Varien_Db_Adapter_Pdo_Mysql
708
+ */
709
+ public function purgeOrphanRecords($tableName, $columnName, $refTableName, $refColumnName,
710
+ $onDelete = Varien_Db_Adapter_Interface::FK_ACTION_CASCADE)
711
+ {
712
+ $onDelete = strtoupper($onDelete);
713
+ if ($onDelete == Varien_Db_Adapter_Interface::FK_ACTION_CASCADE
714
+ || $onDelete == Varien_Db_Adapter_Interface::FK_ACTION_RESTRICT
715
+ ) {
716
+ $sql = sprintf("DELETE p.* FROM %s AS p LEFT JOIN %s AS r ON p.%s = r.%s WHERE r.%s IS NULL",
717
+ $this->quoteIdentifier($tableName),
718
+ $this->quoteIdentifier($refTableName),
719
+ $this->quoteIdentifier($columnName),
720
+ $this->quoteIdentifier($refColumnName),
721
+ $this->quoteIdentifier($refColumnName));
722
+ $this->raw_query($sql);
723
+ } elseif ($onDelete == Varien_Db_Adapter_Interface::FK_ACTION_SET_NULL) {
724
+ $sql = sprintf("UPDATE %s AS p LEFT JOIN %s AS r ON p.%s = r.%s SET p.%s = NULL WHERE r.%s IS NULL",
725
+ $this->quoteIdentifier($tableName),
726
+ $this->quoteIdentifier($refTableName),
727
+ $this->quoteIdentifier($columnName),
728
+ $this->quoteIdentifier($refColumnName),
729
+ $this->quoteIdentifier($columnName),
730
+ $this->quoteIdentifier($refColumnName));
731
+ $this->raw_query($sql);
732
+ }
733
+
734
+ return $this;
735
+ }
736
+
737
+ /**
738
+ * Add foreign key to table. If FK with same name exist - it will be deleted
739
+ *
740
+ * @deprecated since 1.4.0.1
741
+ * @param string $fkName foreign key name
742
+ * @param string $tableName main table name
743
+ * @param string $keyName main table field name
744
+ * @param string $refTableName refered table name
745
+ * @param string $refKeyName refered table field name
746
+ * @param string $onUpdate on update statement
747
+ * @param string $onDelete on delete statement
748
+ * @param bool $purge
749
+ * @return mixed
750
+ */
751
+ public function addConstraint($fkName, $tableName, $columnName,
752
+ $refTableName, $refColumnName, $onDelete = Varien_Db_Adapter_Interface::FK_ACTION_CASCADE,
753
+ $onUpdate = Varien_Db_Adapter_Interface::FK_ACTION_CASCADE, $purge = false)
754
+ {
755
+ return $this->addForeignKey($fkName, $tableName, $columnName, $refTableName, $refColumnName,
756
+ $onDelete, $onUpdate, $purge);
757
+ }
758
+
759
+ /**
760
+ * Check does table column exist
761
+ *
762
+ * @param string $tableName
763
+ * @param string $columnName
764
+ * @param string $schemaName
765
+ * @return boolean
766
+ */
767
+ public function tableColumnExists($tableName, $columnName, $schemaName = null)
768
+ {
769
+ $describe = $this->describeTable($tableName, $schemaName);
770
+ foreach ($describe as $column) {
771
+ if ($column['COLUMN_NAME'] == $columnName) {
772
+ return true;
773
+ }
774
+ }
775
+ return false;
776
+ }
777
+
778
+ /**
779
+ * Adds new column to table.
780
+ *
781
+ * Generally $defintion must be array with column data to keep this call cross-DB compatible.
782
+ * Using string as $definition is allowed only for concrete DB adapter.
783
+ * Adds primary key if needed
784
+ *
785
+ * @param string $tableName
786
+ * @param string $columnName
787
+ * @param array|string $definition string specific or universal array DB Server definition
788
+ * @param string $schemaName
789
+ * @return int|boolean
790
+ * @throws Zend_Db_Exception
791
+ */
792
+ public function addColumn($tableName, $columnName, $definition, $schemaName = null)
793
+ {
794
+ if ($this->tableColumnExists($tableName, $columnName, $schemaName)) {
795
+ return true;
796
+ }
797
+
798
+ $primaryKey = '';
799
+ if (is_array($definition)) {
800
+ $definition = array_change_key_case($definition, CASE_UPPER);
801
+ if (empty($definition['COMMENT'])) {
802
+ throw new Zend_Db_Exception("Impossible to create a column without comment.");
803
+ }
804
+ if (!empty($definition['PRIMARY'])) {
805
+ $primaryKey = sprintf(', ADD PRIMARY KEY (%s)', $this->quoteIdentifier($columnName));
806
+ }
807
+ $definition = $this->_getColumnDefinition($definition);
808
+ }
809
+
810
+ $sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s %s',
811
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
812
+ $this->quoteIdentifier($columnName),
813
+ $definition,
814
+ $primaryKey
815
+ );
816
+
817
+ $result = $this->raw_query($sql);
818
+
819
+ $this->resetDdlCache($tableName, $schemaName);
820
+
821
+ return $result;
822
+ }
823
+
824
+ /**
825
+ * Delete table column
826
+ *
827
+ * @param string $tableName
828
+ * @param string $columnName
829
+ * @param string $schemaName
830
+ * @return bool
831
+ */
832
+ public function dropColumn($tableName, $columnName, $schemaName = null)
833
+ {
834
+ if (!$this->tableColumnExists($tableName, $columnName, $schemaName)) {
835
+ return true;
836
+ }
837
+
838
+ $alterDrop = array();
839
+
840
+ $foreignKeys = $this->getForeignKeys($tableName, $schemaName);
841
+ foreach ($foreignKeys as $fkProp) {
842
+ if ($fkProp['COLUMN_NAME'] == $columnName) {
843
+ $alterDrop[] = 'DROP FOREIGN KEY ' . $this->quoteIdentifier($fkProp['FK_NAME']);
844
+ }
845
+ }
846
+
847
+ $alterDrop[] = 'DROP COLUMN ' . $this->quoteIdentifier($columnName);
848
+ $sql = sprintf('ALTER TABLE %s %s',
849
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
850
+ implode(', ', $alterDrop));
851
+
852
+ $result = $this->raw_query($sql);
853
+ $this->resetDdlCache($tableName, $schemaName);
854
+
855
+ return $result;
856
+ }
857
+
858
+ /**
859
+ * Change the column name and definition
860
+ *
861
+ * For change definition of column - use modifyColumn
862
+ *
863
+ * @param string $tableName
864
+ * @param string $oldColumnName
865
+ * @param string $newColumnName
866
+ * @param array $definition
867
+ * @param boolean $flushData flush table statistic
868
+ * @param string $schemaName
869
+ * @return Varien_Db_Adapter_Pdo_Mysql
870
+ * @throws Zend_Db_Exception
871
+ */
872
+ public function changeColumn($tableName, $oldColumnName, $newColumnName, $definition, $flushData = false,
873
+ $schemaName = null)
874
+ {
875
+ if (!$this->tableColumnExists($tableName, $oldColumnName, $schemaName)) {
876
+ throw new Zend_Db_Exception(sprintf('Column "%s" does not exists on table "%s"', $oldColumnName, $tableName));
877
+ }
878
+
879
+ if (is_array($definition)) {
880
+ $definition = $this->_getColumnDefinition($definition);
881
+ }
882
+
883
+ $sql = sprintf('ALTER TABLE %s CHANGE COLUMN %s %s %s',
884
+ $this->quoteIdentifier($tableName),
885
+ $this->quoteIdentifier($oldColumnName),
886
+ $this->quoteIdentifier($newColumnName),
887
+ $definition);
888
+
889
+ $result = $this->raw_query($sql);
890
+
891
+ if ($flushData) {
892
+ $this->showTableStatus($tableName, $schemaName);
893
+ }
894
+ $this->resetDdlCache($tableName, $schemaName);
895
+
896
+ return $result;
897
+ }
898
+
899
+ /**
900
+ * Modify the column definition
901
+ *
902
+ * @param string $tableName
903
+ * @param string $columnName
904
+ * @param array|string $definition
905
+ * @param boolean $flushData
906
+ * @param string $schemaName
907
+ * @return Varien_Db_Adapter_Pdo_Mysql
908
+ * @throws Zend_Db_Exception
909
+ */
910
+ public function modifyColumn($tableName, $columnName, $definition, $flushData = false, $schemaName = null)
911
+ {
912
+ if (!$this->tableColumnExists($tableName, $columnName, $schemaName)) {
913
+ throw new Zend_Db_Exception(sprintf('Column "%s" does not exists on table "%s"', $columnName, $tableName));
914
+ }
915
+ if (is_array($definition)) {
916
+ $definition = $this->_getColumnDefinition($definition);
917
+ }
918
+
919
+ $sql = sprintf('ALTER TABLE %s MODIFY COLUMN %s %s',
920
+ $this->quoteIdentifier($tableName),
921
+ $this->quoteIdentifier($columnName),
922
+ $definition);
923
+
924
+ $this->raw_query($sql);
925
+ if ($flushData) {
926
+ $this->showTableStatus($tableName, $schemaName);
927
+ }
928
+ $this->resetDdlCache($tableName, $schemaName);
929
+
930
+ return $this;
931
+ }
932
+
933
+ /**
934
+ * Show table status
935
+ *
936
+ * @param string $tableName
937
+ * @param string $schemaName
938
+ * @return array|false
939
+ */
940
+ public function showTableStatus($tableName, $schemaName = null)
941
+ {
942
+ $fromDbName = null;
943
+ if ($schemaName !== null) {
944
+ $fromDbName = ' FROM ' . $this->quoteIdentifier($schemaName);
945
+ }
946
+ $query = sprintf('SHOW TABLE STATUS%s LIKE %s', $fromDbName, $this->quote($tableName));
947
+
948
+ return $this->raw_fetchRow($query);
949
+ }
950
+
951
+ /**
952
+ * Retrieve table index key list
953
+ *
954
+ * @deprecated use getIndexList(
955
+ * @param string $tableName
956
+ * @param string $schemaName
957
+ * @return array
958
+ */
959
+ public function getKeyList($tableName, $schemaName = null)
960
+ {
961
+ $keyList = array();
962
+ $indexList = $this->getIndexList($tableName, $schemaName);
963
+
964
+ foreach ($indexList as $indexProp) {
965
+ $keyList[$indexProp['KEY_NAME']] = $indexProp['COLUMNS_LIST'];
966
+ }
967
+
968
+ return $keyList;
969
+ }
970
+
971
+ /**
972
+ * Retrieve Create Table SQL
973
+ *
974
+ * @param string $tableName
975
+ * @param string $schemaName
976
+ * @return string
977
+ */
978
+ public function getCreateTable($tableName, $schemaName = null)
979
+ {
980
+ $cacheKey = $this->_getTableName($tableName, $schemaName);
981
+ $ddl = $this->loadDdlCache($cacheKey, self::DDL_CREATE);
982
+ if ($ddl === false) {
983
+ $sql = 'SHOW CREATE TABLE ' . $this->quoteIdentifier($tableName);
984
+ $ddl = $this->raw_fetchRow($sql, 'Create Table');
985
+ $this->saveDdlCache($cacheKey, self::DDL_CREATE, $ddl);
986
+ }
987
+ return $ddl;
988
+ }
989
+
990
+ /**
991
+ * Retrieve the foreign keys descriptions for a table.
992
+ *
993
+ * The return value is an associative array keyed by the UPPERCASE foreign key,
994
+ * as returned by the RDBMS.
995
+ *
996
+ * The value of each array element is an associative array
997
+ * with the following keys:
998
+ *
999
+ * FK_NAME => string; original foreign key name
1000
+ * SCHEMA_NAME => string; name of database or schema
1001
+ * TABLE_NAME => string;
1002
+ * COLUMN_NAME => string; column name
1003
+ * REF_SCHEMA_NAME => string; name of reference database or schema
1004
+ * REF_TABLE_NAME => string; reference table name
1005
+ * REF_COLUMN_NAME => string; reference column name
1006
+ * ON_DELETE => string; action type on delete row
1007
+ * ON_UPDATE => string; action type on update row
1008
+ *
1009
+ * @param string $tableName
1010
+ * @param string $schemaName
1011
+ * @return array
1012
+ */
1013
+ public function getForeignKeys($tableName, $schemaName = null)
1014
+ {
1015
+ $cacheKey = $this->_getTableName($tableName, $schemaName);
1016
+ $ddl = $this->loadDdlCache($cacheKey, self::DDL_FOREIGN_KEY);
1017
+ if ($ddl === false) {
1018
+ $ddl = array();
1019
+ $createSql = $this->getCreateTable($tableName, $schemaName);
1020
+
1021
+ // collect CONSTRAINT
1022
+ $regExp = '#,\s+CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) '
1023
+ . 'REFERENCES (`[^`]*\.)?`([^`]*)` \(`([^`]*)`\)'
1024
+ . '( ON DELETE (RESTRICT|CASCADE|SET NULL|NO ACTION))?'
1025
+ . '( ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION))?#';
1026
+ $matches = array();
1027
+ preg_match_all($regExp, $createSql, $matches, PREG_SET_ORDER);
1028
+ foreach ($matches as $match) {
1029
+ $ddl[strtoupper($match[1])] = array(
1030
+ 'FK_NAME' => $match[1],
1031
+ 'SCHEMA_NAME' => $schemaName,
1032
+ 'TABLE_NAME' => $tableName,
1033
+ 'COLUMN_NAME' => $match[2],
1034
+ 'REF_SHEMA_NAME' => isset($match[3]) ? $match[3] : $schemaName,
1035
+ 'REF_TABLE_NAME' => $match[4],
1036
+ 'REF_COLUMN_NAME' => $match[5],
1037
+ 'ON_DELETE' => isset($match[6]) ? $match[7] : '',
1038
+ 'ON_UPDATE' => isset($match[8]) ? $match[9] : ''
1039
+ );
1040
+ }
1041
+
1042
+ $this->saveDdlCache($cacheKey, self::DDL_FOREIGN_KEY, $ddl);
1043
+ }
1044
+
1045
+ return $ddl;
1046
+ }
1047
+
1048
+ /**
1049
+ * Retrieve the foreign keys tree for all tables
1050
+ *
1051
+ * @return array
1052
+ */
1053
+ public function getForeignKeysTree()
1054
+ {
1055
+ $tree = array();
1056
+ foreach ($this->listTables() as $table) {
1057
+ foreach($this->getForeignKeys($table) as $key) {
1058
+ $tree[$table][$key['COLUMN_NAME']] = $key;
1059
+ }
1060
+ }
1061
+ return $tree;
1062
+ }
1063
+
1064
+ /**
1065
+ * Modify tables, used for upgrade process
1066
+ * Change columns definitions, reset foreign keys, change tables comments and engines.
1067
+ *
1068
+ * The value of each array element is an associative array
1069
+ * with the following keys:
1070
+ *
1071
+ * columns => array; list of columns definitions
1072
+ * comment => string; table comment
1073
+ * engine => string; table engine
1074
+ *
1075
+ * @return Varien_Db_Adapter_Pdo_Mysql
1076
+ */
1077
+ public function modifyTables($tables)
1078
+ {
1079
+ $foreignKeys = $this->getForeignKeysTree();
1080
+ foreach ($tables as $table => $tableData) {
1081
+ if (!$this->isTableExists($table)) {
1082
+ continue;
1083
+ }
1084
+ foreach ($tableData['columns'] as $column =>$columnDefinition) {
1085
+ if (!$this->tableColumnExists($table, $column)) {
1086
+ continue;
1087
+ }
1088
+ $droppedKeys = array();
1089
+ foreach($foreignKeys as $keyTable => $columns) {
1090
+ foreach($columns as $columnName => $keyOptions) {
1091
+ if ($table == $keyOptions['REF_TABLE_NAME'] && $column == $keyOptions['REF_COLUMN_NAME']) {
1092
+ $this->dropForeignKey($keyTable, $keyOptions['FK_NAME']);
1093
+ $droppedKeys[] = $keyOptions;
1094
+ }
1095
+ }
1096
+ }
1097
+
1098
+ $this->modifyColumn($table, $column, $columnDefinition);
1099
+
1100
+ foreach ($droppedKeys as $options) {
1101
+ unset($columnDefinition['identity'], $columnDefinition['primary'], $columnDefinition['comment']);
1102
+
1103
+ $onDelete = $options['ON_DELETE'];
1104
+ $onUpdate = $options['ON_UPDATE'];
1105
+
1106
+ if ($onDelete == Varien_Db_Adapter_Interface::FK_ACTION_SET_NULL
1107
+ || $onUpdate == Varien_Db_Adapter_Interface::FK_ACTION_SET_NULL) {
1108
+ $columnDefinition['nullable'] = true;
1109
+ }
1110
+ $this->modifyColumn($options['TABLE_NAME'], $options['COLUMN_NAME'], $columnDefinition);
1111
+ $this->addForeignKey(
1112
+ $options['FK_NAME'],
1113
+ $options['TABLE_NAME'],
1114
+ $options['COLUMN_NAME'],
1115
+ $options['REF_TABLE_NAME'],
1116
+ $options['REF_COLUMN_NAME'],
1117
+ ($onDelete) ? $onDelete : Varien_Db_Adapter_Interface::FK_ACTION_NO_ACTION,
1118
+ ($onUpdate) ? $onUpdate : Varien_Db_Adapter_Interface::FK_ACTION_NO_ACTION
1119
+ );
1120
+ }
1121
+ }
1122
+ if (!empty($tableData['comment'])) {
1123
+ $this->changeTableComment($table, $tableData['comment']);
1124
+ }
1125
+ if (!empty($tableData['engine'])) {
1126
+ $this->changeTableEngine($table, $tableData['engine']);
1127
+ }
1128
+ }
1129
+ return $this;
1130
+ }
1131
+
1132
+ /**
1133
+ * Retrieve table index information
1134
+ *
1135
+ * The return value is an associative array keyed by the UPPERCASE index key (except for primary key,
1136
+ * that is always stored under 'PRIMARY' key) as returned by the RDBMS.
1137
+ *
1138
+ * The value of each array element is an associative array
1139
+ * with the following keys:
1140
+ *
1141
+ * SCHEMA_NAME => string; name of database or schema
1142
+ * TABLE_NAME => string; name of the table
1143
+ * KEY_NAME => string; the original index name
1144
+ * COLUMNS_LIST => array; array of index column names
1145
+ * INDEX_TYPE => string; lowercase, create index type
1146
+ * INDEX_METHOD => string; index method using
1147
+ * type => string; see INDEX_TYPE
1148
+ * fields => array; see COLUMNS_LIST
1149
+ *
1150
+ * @param string $tableName
1151
+ * @param string $schemaName
1152
+ * @return array
1153
+ */
1154
+ public function getIndexList($tableName, $schemaName = null)
1155
+ {
1156
+ $cacheKey = $this->_getTableName($tableName, $schemaName);
1157
+ $ddl = $this->loadDdlCache($cacheKey, self::DDL_INDEX);
1158
+ if ($ddl === false) {
1159
+ $ddl = array();
1160
+
1161
+ $sql = sprintf('SHOW INDEX FROM %s',
1162
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)));
1163
+ foreach ($this->fetchAll($sql) as $row) {
1164
+ $fieldKeyName = 'Key_name';
1165
+ $fieldNonUnique = 'Non_unique';
1166
+ $fieldColumn = 'Column_name';
1167
+ $fieldIndexType = 'Index_type';
1168
+
1169
+ if (strtolower($row[$fieldKeyName]) == Varien_Db_Adapter_Interface::INDEX_TYPE_PRIMARY) {
1170
+ $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_PRIMARY;
1171
+ } elseif ($row[$fieldNonUnique] == 0) {
1172
+ $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE;
1173
+ } elseif (strtolower($row[$fieldIndexType]) == Varien_Db_Adapter_Interface::INDEX_TYPE_FULLTEXT) {
1174
+ $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_FULLTEXT;
1175
+ } else {
1176
+ $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX;
1177
+ }
1178
+
1179
+ $upperKeyName = strtoupper($row[$fieldKeyName]);
1180
+ if (isset($ddl[$upperKeyName])) {
1181
+ $ddl[$upperKeyName]['fields'][] = $row[$fieldColumn]; // for compatible
1182
+ $ddl[$upperKeyName]['COLUMNS_LIST'][] = $row[$fieldColumn];
1183
+ } else {
1184
+ $ddl[$upperKeyName] = array(
1185
+ 'SCHEMA_NAME' => $schemaName,
1186
+ 'TABLE_NAME' => $tableName,
1187
+ 'KEY_NAME' => $row[$fieldKeyName],
1188
+ 'COLUMNS_LIST' => array($row[$fieldColumn]),
1189
+ 'INDEX_TYPE' => $indexType,
1190
+ 'INDEX_METHOD' => $row[$fieldIndexType],
1191
+ 'type' => strtolower($indexType), // for compatibility
1192
+ 'fields' => array($row[$fieldColumn]) // for compatibility
1193
+ );
1194
+ }
1195
+ }
1196
+ $this->saveDdlCache($cacheKey, self::DDL_INDEX, $ddl);
1197
+ }
1198
+ return $ddl;
1199
+ }
1200
+
1201
+ /**
1202
+ * Add Index Key
1203
+ *
1204
+ * @deprecated since 1.5.0.0
1205
+ * @param string $tableName
1206
+ * @param string $indexName
1207
+ * @param string|array $fields
1208
+ * @param string $indexType
1209
+ * @param string $schemaName
1210
+ * @return Zend_Db_Statement_Interface
1211
+ */
1212
+ public function addKey($tableName, $indexName, $fields, $indexType = 'index', $schemaName = null)
1213
+ {
1214
+ return $this->addIndex($tableName, $indexName, $fields, $indexType, $schemaName);
1215
+ }
1216
+
1217
+ /**
1218
+ * Remove duplicate entry for create key
1219
+ *
1220
+ * @param string $table
1221
+ * @param array $fields
1222
+ * @param array $ids
1223
+ * @return Varien_Db_Adapter_Pdo_Mysql
1224
+ */
1225
+ protected function _removeDuplicateEntry($table, $fields, $ids)
1226
+ {
1227
+ $where = array();
1228
+ $i = 0;
1229
+ foreach ($fields as $field) {
1230
+ $where[] = $this->quoteInto($field . '=?', $ids[$i++]);
1231
+ }
1232
+
1233
+ if (!$where) {
1234
+ return $this;
1235
+ }
1236
+ $whereCond = implode(' AND ', $where);
1237
+ $sql = sprintf('SELECT COUNT(*) as `cnt` FROM `%s` WHERE %s', $table, $whereCond);
1238
+
1239
+ $cnt = $this->raw_fetchRow($sql, 'cnt');
1240
+ if ($cnt > 1) {
1241
+ $sql = sprintf('DELETE FROM `%s` WHERE %s LIMIT %d',
1242
+ $table,
1243
+ $whereCond,
1244
+ $cnt - 1
1245
+ );
1246
+ $this->raw_query($sql);
1247
+ }
1248
+
1249
+ return $this;
1250
+ }
1251
+
1252
+ /**
1253
+ * Creates and returns a new Zend_Db_Select object for this adapter.
1254
+ *
1255
+ * @return Varien_Db_Select
1256
+ */
1257
+ public function select()
1258
+ {
1259
+ return new Varien_Db_Select($this);
1260
+ }
1261
+
1262
+ /**
1263
+ * Start debug timer
1264
+ *
1265
+ * @return Varien_Db_Adapter_Pdo_Mysql
1266
+ */
1267
+ protected function _debugTimer()
1268
+ {
1269
+ if ($this->_debug) {
1270
+ $this->_debugTimer = microtime(true);
1271
+ }
1272
+ return $this;
1273
+ }
1274
+
1275
+ /**
1276
+ * Logging debug information
1277
+ *
1278
+ * @param int $type
1279
+ * @param string $sql
1280
+ * @param array $bind
1281
+ * @param Zend_Db_Statement_Pdo $result
1282
+ * @return Varien_Db_Adapter_Pdo_Mysql
1283
+ */
1284
+ protected function _debugStat($type, $sql, $bind = array(), $result = null)
1285
+ {
1286
+ if (!$this->_debug) {
1287
+ return $this;
1288
+ }
1289
+
1290
+ $code = '## ' . getmypid() . ' ## ';
1291
+ $nl = "\n";
1292
+ $time = sprintf('%.4f', microtime(true) - $this->_debugTimer);
1293
+
1294
+ if (!$this->_logAllQueries && $time < $this->_logQueryTime) {
1295
+ return $this;
1296
+ }
1297
+ switch ($type) {
1298
+ case self::DEBUG_CONNECT:
1299
+ $code .= 'CONNECT' . $nl;
1300
+ break;
1301
+ case self::DEBUG_TRANSACTION:
1302
+ $code .= 'TRANSACTION ' . $sql . $nl;
1303
+ break;
1304
+ case self::DEBUG_QUERY:
1305
+ $code .= 'QUERY' . $nl;
1306
+ $code .= 'SQL: ' . $sql . $nl;
1307
+ if ($bind) {
1308
+ $code .= 'BIND: ' . var_export($bind, true) . $nl;
1309
+ }
1310
+ if ($result instanceof Zend_Db_Statement_Pdo) {
1311
+ $code .= 'AFF: ' . $result->rowCount() . $nl;
1312
+ }
1313
+ break;
1314
+ }
1315
+ $code .= 'TIME: ' . $time . $nl;
1316
+
1317
+ if ($this->_logCallStack) {
1318
+ $code .= 'TRACE: ' . Varien_Debug::backtrace(true, false) . $nl;
1319
+ }
1320
+
1321
+ $code .= $nl;
1322
+
1323
+ $this->_debugWriteToFile($code);
1324
+
1325
+ return $this;
1326
+ }
1327
+
1328
+ /**
1329
+ * Write exception and thow
1330
+ *
1331
+ * @param Exception $e
1332
+ * @throws Exception
1333
+ */
1334
+ protected function _debugException(Exception $e)
1335
+ {
1336
+ if (!$this->_debug) {
1337
+ throw $e;
1338
+ }
1339
+
1340
+ $nl = "\n";
1341
+ $code = 'EXCEPTION ' . $nl . $e . $nl . $nl;
1342
+ $this->_debugWriteToFile($code);
1343
+
1344
+ throw $e;
1345
+ }
1346
+
1347
+ /**
1348
+ * Debug write to file process
1349
+ *
1350
+ * @param string $str
1351
+ */
1352
+ protected function _debugWriteToFile($str)
1353
+ {
1354
+ $str = '## ' . date('Y-m-d H:i:s') . "\r\n" . $str;
1355
+ if (!$this->_debugIoAdapter) {
1356
+ $this->_debugIoAdapter = new Varien_Io_File();
1357
+ $dir = Mage::getBaseDir() . DS . $this->_debugIoAdapter->dirname($this->_debugFile);
1358
+ $this->_debugIoAdapter->checkAndCreateFolder($dir);
1359
+ $this->_debugIoAdapter->open(array('path' => $dir));
1360
+ $this->_debugFile = basename($this->_debugFile);
1361
+ }
1362
+
1363
+ $this->_debugIoAdapter->streamOpen($this->_debugFile, 'a');
1364
+ $this->_debugIoAdapter->streamLock();
1365
+ $this->_debugIoAdapter->streamWrite($str);
1366
+ $this->_debugIoAdapter->streamUnlock();
1367
+ $this->_debugIoAdapter->streamClose();
1368
+ }
1369
+
1370
+ /**
1371
+ * Quotes a value and places into a piece of text at a placeholder.
1372
+ *
1373
+ * Method revrited for handle empty arrays in value param
1374
+ *
1375
+ * @param string $text The text with a placeholder.
1376
+ * @param mixed $value The value to quote.
1377
+ * @param string $type OPTIONAL SQL datatype
1378
+ * @param integer $count OPTIONAL count of placeholders to replace
1379
+ * @return string An SQL-safe quoted value placed into the orignal text.
1380
+ */
1381
+ public function quoteInto($text, $value, $type = null, $count = null)
1382
+ {
1383
+ if (is_array($value) && empty($value)) {
1384
+ $value = new Zend_Db_Expr('NULL');
1385
+ }
1386
+ return parent::quoteInto($text, $value, $type, $count);
1387
+ }
1388
+
1389
+ /**
1390
+ * Retrieve ddl cache name
1391
+ *
1392
+ * @param string $tableName
1393
+ * @param string $schemaName
1394
+ */
1395
+ protected function _getTableName($tableName, $schemaName = null)
1396
+ {
1397
+ return ($schemaName ? $schemaName . '.' : '') . $tableName;
1398
+ }
1399
+
1400
+ /**
1401
+ * Retrieve Id for cache
1402
+ *
1403
+ * @param string $tableKey
1404
+ * @param int $ddlType
1405
+ * @return string
1406
+ */
1407
+ protected function _getCacheId($tableKey, $ddlType)
1408
+ {
1409
+ return sprintf('%s_%s_%s', self::DDL_CACHE_PREFIX, $tableKey, $ddlType);
1410
+ }
1411
+
1412
+ /**
1413
+ * Load DDL data from cache
1414
+ * Return false if cache does not exists
1415
+ *
1416
+ * @param string $tableCacheKey the table cache key
1417
+ * @param int $ddlType the DDL constant
1418
+ * @return string|array|int|false
1419
+ */
1420
+ public function loadDdlCache($tableCacheKey, $ddlType)
1421
+ {
1422
+ if (!$this->_isDdlCacheAllowed) {
1423
+ return false;
1424
+ }
1425
+ if (isset($this->_ddlCache[$ddlType][$tableCacheKey])) {
1426
+ return $this->_ddlCache[$ddlType][$tableCacheKey];
1427
+ }
1428
+
1429
+ if ($this->_cacheAdapter instanceof Zend_Cache_Core) {
1430
+ $cacheId = $this->_getCacheId($tableCacheKey, $ddlType);
1431
+ $data = $this->_cacheAdapter->load($cacheId);
1432
+ if ($data !== false) {
1433
+ $data = unserialize($data);
1434
+ $this->_ddlCache[$ddlType][$tableCacheKey] = $data;
1435
+ }
1436
+ return $data;
1437
+ }
1438
+
1439
+ return false;
1440
+ }
1441
+
1442
+ /**
1443
+ * Save DDL data into cache
1444
+ *
1445
+ * @param string $tableCacheKey
1446
+ * @param int $ddlType
1447
+ * @return Varien_Db_Adapter_Pdo_Mysql
1448
+ */
1449
+ public function saveDdlCache($tableCacheKey, $ddlType, $data)
1450
+ {
1451
+ if (!$this->_isDdlCacheAllowed) {
1452
+ return $this;
1453
+ }
1454
+ $this->_ddlCache[$ddlType][$tableCacheKey] = $data;
1455
+
1456
+ if ($this->_cacheAdapter instanceof Zend_Cache_Core) {
1457
+ $cacheId = $this->_getCacheId($tableCacheKey, $ddlType);
1458
+ $data = serialize($data);
1459
+ $this->_cacheAdapter->save($data, $cacheId, array(self::DDL_CACHE_TAG));
1460
+ }
1461
+
1462
+ return $this;
1463
+ }
1464
+
1465
+ /**
1466
+ * Reset cached DDL data from cache
1467
+ * if table name is null - reset all cached DDL data
1468
+ *
1469
+ * @param string $tableName
1470
+ * @param string $schemaName OPTIONAL
1471
+ * @return Varien_Db_Adapter_Pdo_Mysql
1472
+ */
1473
+ public function resetDdlCache($tableName = null, $schemaName = null)
1474
+ {
1475
+ if (!$this->_isDdlCacheAllowed) {
1476
+ return $this;
1477
+ }
1478
+ if ($tableName === null) {
1479
+ $this->_ddlCache = array();
1480
+ if ($this->_cacheAdapter instanceof Zend_Cache_Core) {
1481
+ $this->_cacheAdapter->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array(self::DDL_CACHE_TAG));
1482
+ }
1483
+ } else {
1484
+ $cacheKey = $this->_getTableName($tableName, $schemaName);
1485
+
1486
+ $ddlTypes = array(self::DDL_DESCRIBE, self::DDL_CREATE, self::DDL_INDEX, self::DDL_FOREIGN_KEY);
1487
+ foreach ($ddlTypes as $ddlType) {
1488
+ unset($this->_ddlCache[$ddlType][$cacheKey]);
1489
+ }
1490
+
1491
+ if ($this->_cacheAdapter instanceof Zend_Cache_Core) {
1492
+ foreach ($ddlTypes as $ddlType) {
1493
+ $cacheId = $this->_getCacheId($cacheKey, $ddlType);
1494
+ $this->_cacheAdapter->remove($cacheId);
1495
+ }
1496
+ }
1497
+ }
1498
+
1499
+ return $this;
1500
+ }
1501
+
1502
+ /**
1503
+ * Disallow DDL caching
1504
+ * @return Varien_Db_Adapter_Pdo_Mysql
1505
+ */
1506
+ public function disallowDdlCache()
1507
+ {
1508
+ $this->_isDdlCacheAllowed = false;
1509
+ return $this;
1510
+ }
1511
+
1512
+ /**
1513
+ * Allow DDL caching
1514
+ * @return Varien_Db_Adapter_Pdo_Mysql
1515
+ */
1516
+ public function allowDdlCache()
1517
+ {
1518
+ $this->_isDdlCacheAllowed = true;
1519
+ return $this;
1520
+ }
1521
+
1522
+ /**
1523
+ * Returns the column descriptions for a table.
1524
+ *
1525
+ * The return value is an associative array keyed by the column name,
1526
+ * as returned by the RDBMS.
1527
+ *
1528
+ * The value of each array element is an associative array
1529
+ * with the following keys:
1530
+ *
1531
+ * SCHEMA_NAME => string; name of database or schema
1532
+ * TABLE_NAME => string;
1533
+ * COLUMN_NAME => string; column name
1534
+ * COLUMN_POSITION => number; ordinal position of column in table
1535
+ * DATA_TYPE => string; SQL datatype name of column
1536
+ * DEFAULT => string; default expression of column, null if none
1537
+ * NULLABLE => boolean; true if column can have nulls
1538
+ * LENGTH => number; length of CHAR/VARCHAR
1539
+ * SCALE => number; scale of NUMERIC/DECIMAL
1540
+ * PRECISION => number; precision of NUMERIC/DECIMAL
1541
+ * UNSIGNED => boolean; unsigned property of an integer type
1542
+ * PRIMARY => boolean; true if column is part of the primary key
1543
+ * PRIMARY_POSITION => integer; position of column in primary key
1544
+ * IDENTITY => integer; true if column is auto-generated with unique values
1545
+ *
1546
+ * @param string $tableName
1547
+ * @param string $schemaName OPTIONAL
1548
+ * @return array
1549
+ */
1550
+ public function describeTable($tableName, $schemaName = null)
1551
+ {
1552
+ $cacheKey = $this->_getTableName($tableName, $schemaName);
1553
+ $ddl = $this->loadDdlCache($cacheKey, self::DDL_DESCRIBE);
1554
+ if ($ddl === false) {
1555
+ $ddl = parent::describeTable($tableName, $schemaName);
1556
+ /**
1557
+ * Remove bug in some MySQL versions, when int-column without default value is described as:
1558
+ * having default empty string value
1559
+ */
1560
+ $affected = array('tinyint', 'smallint', 'mediumint', 'int', 'bigint');
1561
+ foreach ($ddl as $key => $columnData) {
1562
+ if (($columnData['DEFAULT'] === '') && (array_search($columnData['DATA_TYPE'], $affected) !== FALSE)) {
1563
+ $ddl[$key]['DEFAULT'] = null;
1564
+ }
1565
+ }
1566
+ $this->saveDdlCache($cacheKey, self::DDL_DESCRIBE, $ddl);
1567
+ }
1568
+
1569
+ return $ddl;
1570
+ }
1571
+
1572
+ /**
1573
+ * Create Varien_Db_Ddl_Table object by data from describe table
1574
+ *
1575
+ * @param $tableName
1576
+ * @param $newTableName
1577
+ * @return Varien_Db_Ddl_Table
1578
+ */
1579
+ public function createTableByDdl($tableName, $newTableName)
1580
+ {
1581
+ $describe = $this->describeTable($tableName);
1582
+ $table = $this->newTable($newTableName)
1583
+ ->setComment(ucwords(str_replace('_', ' ', $newTableName)));
1584
+ foreach ($describe as $columnData) {
1585
+ $type = $this->_getColumnTypeByDdl($columnData);
1586
+ $options = array();
1587
+ if ($columnData['IDENTITY'] === true) {
1588
+ $options['identity'] = true;
1589
+ }
1590
+ if ($columnData['UNSIGNED'] === true) {
1591
+ $options['unsigned'] = true;
1592
+ }
1593
+ if ($columnData['NULLABLE'] === false
1594
+ && !($type == Varien_Db_Ddl_Table::TYPE_TEXT && strlen($columnData['DEFAULT']) != 0)
1595
+ ) {
1596
+ $options['nullable'] = false;
1597
+ }
1598
+ if ($columnData['PRIMARY'] === true) {
1599
+ $options['primary'] = true;
1600
+ }
1601
+ if (!is_null($columnData['DEFAULT'])
1602
+ && $type != Varien_Db_Ddl_Table::TYPE_TEXT
1603
+ ) {
1604
+ $options['default'] = $this->quote($columnData['DEFAULT']);
1605
+ }
1606
+ if (strlen($columnData['SCALE']) > 0) {
1607
+ $options['scale'] = $columnData['SCALE'];
1608
+ }
1609
+ if (strlen($columnData['PRECISION']) > 0) {
1610
+ $options['precision'] = $columnData['PRECISION'];
1611
+ }
1612
+ $comment = ucwords(str_replace('_', ' ', $columnData['COLUMN_NAME']));
1613
+ $table->addColumn($columnData['COLUMN_NAME'], $type, $columnData['LENGTH'], $options, $comment);
1614
+ }
1615
+
1616
+ $indexes = $this->getIndexList($tableName);
1617
+ foreach ($indexes as $indexData) {
1618
+ /**
1619
+ * Do not create primary index - it is created with identity column.
1620
+ * For reliability check both name and type, because these values can start to differ in future.
1621
+ */
1622
+ if (($indexData['KEY_NAME'] == 'PRIMARY')
1623
+ || ($indexData['INDEX_TYPE'] == Varien_Db_Adapter_Interface::INDEX_TYPE_PRIMARY)) {
1624
+ continue;
1625
+ }
1626
+
1627
+ $fields = $indexData['COLUMNS_LIST'];
1628
+ $options = array('type' => $indexData['INDEX_TYPE']);
1629
+ $table->addIndex($this->getIndexName($newTableName, $fields, $indexData['INDEX_TYPE']), $fields, $options);
1630
+ }
1631
+
1632
+ $foreignKeys = $this->getForeignKeys($tableName);
1633
+ foreach ($foreignKeys as $keyData) {
1634
+ $fkName = $this->getForeignKeyName(
1635
+ $newTableName, $keyData['COLUMN_NAME'], $keyData['REF_TABLE_NAME'], $keyData['REF_COLUMN_NAME']
1636
+ );
1637
+ $onDelete = $this->_getDdlAction($keyData['ON_DELETE']);
1638
+ $onUpdate = $this->_getDdlAction($keyData['ON_UPDATE']);
1639
+
1640
+ $table->addForeignKey(
1641
+ $fkName, $keyData['COLUMN_NAME'], $keyData['REF_TABLE_NAME'],
1642
+ $keyData['REF_COLUMN_NAME'], $onDelete, $onUpdate
1643
+ );
1644
+ }
1645
+
1646
+ // Set additional options
1647
+ $tableData = $this->showTableStatus($tableName);
1648
+ $table->setOption('type', $tableData['Engine']);
1649
+
1650
+ return $table;
1651
+ }
1652
+
1653
+ /**
1654
+ * Modify the column definition by data from describe table
1655
+ *
1656
+ * @param string $tableName
1657
+ * @param string $columnName
1658
+ * @param array $definition
1659
+ * @param boolean $flushData
1660
+ * @param string $schemaName
1661
+ * @return Varien_Db_Adapter_Pdo_Mysql
1662
+ */
1663
+ public function modifyColumnByDdl($tableName, $columnName, $definition, $flushData = false, $schemaName = null)
1664
+ {
1665
+ $definition = array_change_key_case($definition, CASE_UPPER);
1666
+ $definition['COLUMN_TYPE'] = $this->_getColumnTypeByDdl($definition);
1667
+ if (array_key_exists('DEFAULT', $definition) && is_null($definition['DEFAULT'])) {
1668
+ unset($definition['DEFAULT']);
1669
+ }
1670
+ return $this->modifyColumn($tableName, $columnName, $definition, $flushData, $schemaName);
1671
+ }
1672
+
1673
+ /**
1674
+ * Retrieve column data type by data from describe table
1675
+ *
1676
+ * @param array $column
1677
+ * @return string
1678
+ */
1679
+ protected function _getColumnTypeByDdl($column)
1680
+ {
1681
+ switch ($column['DATA_TYPE']) {
1682
+ case 'bool':
1683
+ return Varien_Db_Ddl_Table::TYPE_BOOLEAN;
1684
+ case 'tinytext':
1685
+ case 'char':
1686
+ case 'varchar':
1687
+ case 'text':
1688
+ case 'mediumtext':
1689
+ case 'longtext':
1690
+ return Varien_Db_Ddl_Table::TYPE_TEXT;
1691
+ case 'blob':
1692
+ case 'mediumblob':
1693
+ case 'longblob':
1694
+ return Varien_Db_Ddl_Table::TYPE_BLOB;
1695
+ case 'tinyint':
1696
+ case 'smallint':
1697
+ return Varien_Db_Ddl_Table::TYPE_SMALLINT;
1698
+ case 'mediumint':
1699
+ case 'int':
1700
+ return Varien_Db_Ddl_Table::TYPE_INTEGER;
1701
+ case 'bigint':
1702
+ return Varien_Db_Ddl_Table::TYPE_BIGINT;
1703
+ case 'datetime':
1704
+ return Varien_Db_Ddl_Table::TYPE_DATETIME;
1705
+ case 'timestamp':
1706
+ return Varien_Db_Ddl_Table::TYPE_TIMESTAMP;
1707
+ case 'date':
1708
+ return Varien_Db_Ddl_Table::TYPE_DATE;
1709
+ case 'float':
1710
+ return Varien_Db_Ddl_Table::TYPE_FLOAT;
1711
+ case 'decimal':
1712
+ case 'numeric':
1713
+ return Varien_Db_Ddl_Table::TYPE_DECIMAL;
1714
+ }
1715
+ }
1716
+
1717
+ /**
1718
+ * Truncate table
1719
+ *
1720
+ * @deprecated since 1.4.0.1
1721
+ * @param string $tableName
1722
+ * @param string $schemaName
1723
+ * @return Varien_Db_Adapter_Pdo_Mysql
1724
+ */
1725
+ public function truncate($tableName, $schemaName = null)
1726
+ {
1727
+ return $this->truncateTable($tableName, $schemaName);
1728
+ }
1729
+
1730
+ /**
1731
+ * Change table storage engine
1732
+ *
1733
+ * @param string $tableName
1734
+ * @param string $engine
1735
+ * @param string $schemaName
1736
+ * @return mixed
1737
+ */
1738
+ public function changeTableEngine($tableName, $engine, $schemaName = null)
1739
+ {
1740
+ $table = $this->quoteIdentifier($this->_getTableName($tableName, $schemaName));
1741
+ $sql = sprintf('ALTER TABLE %s ENGINE=%s', $table, $engine);
1742
+ return $this->raw_query($sql);
1743
+ }
1744
+
1745
+ /**
1746
+ * Change table comment
1747
+ *
1748
+ * @param string $tableName
1749
+ * @param string $comment
1750
+ * @param string $schemaName
1751
+ * @return mixed
1752
+ */
1753
+ public function changeTableComment($tableName, $comment, $schemaName = null)
1754
+ {
1755
+ $table = $this->quoteIdentifier($this->_getTableName($tableName, $schemaName));
1756
+ $sql = sprintf("ALTER TABLE %s COMMENT='%s'", $table, $comment);
1757
+ return $this->raw_query($sql);
1758
+ }
1759
+
1760
+ /**
1761
+ * Inserts a table row with specified data
1762
+ * Special for Zero values to identity column
1763
+ *
1764
+ * @param string $table
1765
+ * @param array $bind
1766
+ * @return int The number of affected rows.
1767
+ */
1768
+ public function insertForce($table, array $bind)
1769
+ {
1770
+ $this->raw_query("SET @OLD_INSERT_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");
1771
+ $result = $this->insert($table, $bind);
1772
+ $this->raw_query("SET SQL_MODE=IFNULL(@OLD_INSERT_SQL_MODE,'')");
1773
+ return $result;
1774
+ }
1775
+
1776
+ /**
1777
+ * Inserts a table row with specified data.
1778
+ *
1779
+ * @param mixed $table The table to insert data into.
1780
+ * @param array $data Column-value pairs or array of column-value pairs.
1781
+ * @param array $fields update fields pairs or values
1782
+ * @return int The number of affected rows.
1783
+ * @throws Zend_Db_Exception
1784
+ */
1785
+ public function insertOnDuplicate($table, array $data, array $fields = array())
1786
+ {
1787
+ // extract and quote col names from the array keys
1788
+ $row = reset($data); // get first element from data array
1789
+ $bind = array(); // SQL bind array
1790
+ $values = array();
1791
+
1792
+ if (is_array($row)) { // Array of column-value pairs
1793
+ $cols = array_keys($row);
1794
+ foreach ($data as $row) {
1795
+ if (array_diff($cols, array_keys($row))) {
1796
+ throw new Zend_Db_Exception('Invalid data for insert');
1797
+ }
1798
+ $values[] = $this->_prepareInsertData($row, $bind);
1799
+ }
1800
+ unset($row);
1801
+ } else { // Column-value pairs
1802
+ $cols = array_keys($data);
1803
+ $values[] = $this->_prepareInsertData($data, $bind);
1804
+ }
1805
+
1806
+ $updateFields = array();
1807
+ if (empty($fields)) {
1808
+ $fields = $cols;
1809
+ }
1810
+
1811
+ // quote column names
1812
+ // $cols = array_map(array($this, 'quoteIdentifier'), $cols);
1813
+
1814
+ // prepare ON DUPLICATE KEY conditions
1815
+ foreach ($fields as $k => $v) {
1816
+ $field = $value = null;
1817
+ if (!is_numeric($k)) {
1818
+ $field = $this->quoteIdentifier($k);
1819
+ if ($v instanceof Zend_Db_Expr) {
1820
+ $value = $v->__toString();
1821
+ } elseif (is_string($v)) {
1822
+ $value = sprintf('VALUES(%s)', $this->quoteIdentifier($v));
1823
+ } elseif (is_numeric($v)) {
1824
+ $value = $this->quoteInto('?', $v);
1825
+ }
1826
+ } elseif (is_string($v)) {
1827
+ $value = sprintf('VALUES(%s)', $this->quoteIdentifier($v));
1828
+ $field = $v;
1829
+ }
1830
+
1831
+ if ($field && $value) {
1832
+ $updateFields[] = sprintf('%s = %s', $field, $value);
1833
+ }
1834
+ }
1835
+
1836
+ $insertSql = $this->_getInsertSqlQuery($table, $cols, $values);
1837
+ if ($updateFields) {
1838
+ $insertSql .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $updateFields);
1839
+ }
1840
+ // execute the statement and return the number of affected rows
1841
+ $stmt = $this->query($insertSql, array_values($bind));
1842
+ $result = $stmt->rowCount();
1843
+
1844
+ return $result;
1845
+ }
1846
+
1847
+ /**
1848
+ * Inserts a table multiply rows with specified data.
1849
+ *
1850
+ * @param mixed $table The table to insert data into.
1851
+ * @param array $data Column-value pairs or array of Column-value pairs.
1852
+ * @return int The number of affected rows.
1853
+ * @throws Zend_Db_Exception
1854
+ */
1855
+ public function insertMultiple($table, array $data)
1856
+ {
1857
+ $row = reset($data);
1858
+ // support insert syntaxes
1859
+ if (!is_array($row)) {
1860
+ return $this->insert($table, $data);
1861
+ }
1862
+
1863
+ // validate data array
1864
+ $cols = array_keys($row);
1865
+ $insertArray = array();
1866
+ foreach ($data as $row) {
1867
+ $line = array();
1868
+ if (array_diff($cols, array_keys($row))) {
1869
+ throw new Zend_Db_Exception('Invalid data for insert');
1870
+ }
1871
+ foreach ($cols as $field) {
1872
+ $line[] = $row[$field];
1873
+ }
1874
+ $insertArray[] = $line;
1875
+ }
1876
+ unset($row);
1877
+
1878
+ return $this->insertArray($table, $cols, $insertArray);
1879
+ }
1880
+
1881
+ /**
1882
+ * Insert array to table based on columns definition
1883
+ *
1884
+ * @param string $table
1885
+ * @param array $columns
1886
+ * @param array $data
1887
+ * @return int
1888
+ * @throws Zend_Db_Exception
1889
+ */
1890
+ public function insertArray($table, array $columns, array $data)
1891
+ {
1892
+ $values = array();
1893
+ $bind = array();
1894
+ $columnsCount = count($columns);
1895
+ foreach ($data as $row) {
1896
+ if ($columnsCount != count($row)) {
1897
+ throw new Zend_Db_Exception('Invalid data for insert');
1898
+ }
1899
+ $values[] = $this->_prepareInsertData($row, $bind);
1900
+ }
1901
+
1902
+ $insertQuery = $this->_getInsertSqlQuery($table, $columns, $values);
1903
+
1904
+ // execute the statement and return the number of affected rows
1905
+ $stmt = $this->query($insertQuery, $bind);
1906
+ $result = $stmt->rowCount();
1907
+ return $result;
1908
+ }
1909
+
1910
+ /**
1911
+ * Set cache adapter
1912
+ *
1913
+ * @param Zend_Cache_Backend_Interface $adapter
1914
+ * @return Varien_Db_Adapter_Pdo_Mysql
1915
+ */
1916
+ public function setCacheAdapter($adapter)
1917
+ {
1918
+ $this->_cacheAdapter = $adapter;
1919
+ return $this;
1920
+ }
1921
+
1922
+ /**
1923
+ * Return new DDL Table object
1924
+ *
1925
+ * @param string $tableName the table name
1926
+ * @param string $schemaName the database/schema name
1927
+ * @return Varien_Db_Ddl_Table
1928
+ */
1929
+ public function newTable($tableName = null, $schemaName = null)
1930
+ {
1931
+ $table = new Varien_Db_Ddl_Table();
1932
+ if ($tableName !== null) {
1933
+ $table->setName($tableName);
1934
+ }
1935
+ if ($schemaName !== null) {
1936
+ $table->setSchema($schemaName);
1937
+ }
1938
+ return $table;
1939
+ }
1940
+
1941
+ /**
1942
+ * Create table
1943
+ *
1944
+ * @param Varien_Db_Ddl_Table $table
1945
+ * @throws Zend_Db_Exception
1946
+ * @return Zend_Db_Pdo_Statement
1947
+ */
1948
+ public function createTable(Varien_Db_Ddl_Table $table)
1949
+ {
1950
+ $columns = $table->getColumns();
1951
+ foreach ($columns as $columnEntry) {
1952
+ if (empty($columnEntry['COMMENT'])) {
1953
+ throw new Zend_Db_Exception("Cannot create table without columns comments");
1954
+ }
1955
+ }
1956
+
1957
+ $sqlFragment = array_merge(
1958
+ $this->_getColumnsDefinition($table),
1959
+ $this->_getIndexesDefinition($table),
1960
+ $this->_getForeignKeysDefinition($table)
1961
+ );
1962
+ $tableOptions = $this->_getOptionsDefinition($table);
1963
+ $sql = sprintf("CREATE TABLE %s (\n%s\n) %s",
1964
+ $this->quoteIdentifier($table->getName()),
1965
+ implode(",\n", $sqlFragment),
1966
+ implode(" ", $tableOptions));
1967
+
1968
+ return $this->query($sql);
1969
+ }
1970
+
1971
+ /**
1972
+ * Retrieve columns and primary keys definition array for create table
1973
+ *
1974
+ * @param Varien_Db_Ddl_Table $table
1975
+ * @return array
1976
+ * @throws Zend_Db_Exception
1977
+ */
1978
+ protected function _getColumnsDefinition(Varien_Db_Ddl_Table $table)
1979
+ {
1980
+ $definition = array();
1981
+ $primary = array();
1982
+ $columns = $table->getColumns();
1983
+ if (empty($columns)) {
1984
+ throw new Zend_Db_Exception('Table columns are not defined');
1985
+ }
1986
+
1987
+ foreach ($columns as $columnData) {
1988
+ $columnDefinition = $this->_getColumnDefinition($columnData);
1989
+ if ($columnData['PRIMARY']) {
1990
+ $primary[$columnData['COLUMN_NAME']] = $columnData['PRIMARY_POSITION'];
1991
+ }
1992
+
1993
+ $definition[] = sprintf(' %s %s',
1994
+ $this->quoteIdentifier($columnData['COLUMN_NAME']),
1995
+ $columnDefinition
1996
+ );
1997
+ }
1998
+
1999
+ // PRIMARY KEY
2000
+ if (!empty($primary)) {
2001
+ asort($primary, SORT_NUMERIC);
2002
+ $primary = array_map(array($this, 'quoteIdentifier'), array_keys($primary));
2003
+ $definition[] = sprintf(' PRIMARY KEY (%s)', implode(', ', $primary));
2004
+ }
2005
+ return $definition;
2006
+ }
2007
+
2008
+ /**
2009
+ * Retrieve table indexes definition array for create table
2010
+ *
2011
+ * @param Varien_Db_Ddl_Table $table
2012
+ * @return array
2013
+ */
2014
+ protected function _getIndexesDefinition(Varien_Db_Ddl_Table $table)
2015
+ {
2016
+ $definition = array();
2017
+ $indexes = $table->getIndexes();
2018
+ if (!empty($indexes)) {
2019
+ foreach ($indexes as $indexData) {
2020
+ if (!empty($indexData['TYPE'])) {
2021
+ switch ($indexData['TYPE']) {
2022
+ case 'primary':
2023
+ $indexType = 'PRIMARY KEY';
2024
+ unset($indexData['INDEX_NAME']);
2025
+ break;
2026
+ default:
2027
+ $indexType = strtoupper($indexData['TYPE']);
2028
+ break;
2029
+ }
2030
+ } else {
2031
+ $indexType = 'KEY';
2032
+ }
2033
+
2034
+ $columns = array();
2035
+ foreach ($indexData['COLUMNS'] as $columnData) {
2036
+ $column = $this->quoteIdentifier($columnData['NAME']);
2037
+ if (!empty($columnData['SIZE'])) {
2038
+ $column .= sprintf('(%d)', $columnData['SIZE']);
2039
+ }
2040
+ $columns[] = $column;
2041
+ }
2042
+ $indexName = isset($indexData['INDEX_NAME']) ? $this->quoteIdentifier($indexData['INDEX_NAME']) : '';
2043
+ $definition[] = sprintf(' %s %s (%s)',
2044
+ $indexType,
2045
+ $indexName,
2046
+ implode(', ', $columns)
2047
+ );
2048
+ }
2049
+ }
2050
+
2051
+ return $definition;
2052
+ }
2053
+
2054
+ /**
2055
+ * Retrieve table foreign keys definition array for create table
2056
+ *
2057
+ * @param Varien_Db_Ddl_Table $table
2058
+ * @return array
2059
+ */
2060
+ protected function _getForeignKeysDefinition(Varien_Db_Ddl_Table $table)
2061
+ {
2062
+ $definition = array();
2063
+ $relations = $table->getForeignKeys();
2064
+
2065
+ if (!empty($relations)) {
2066
+ foreach ($relations as $fkData) {
2067
+ $onDelete = $this->_getDdlAction($fkData['ON_DELETE']);
2068
+ $onUpdate = $this->_getDdlAction($fkData['ON_UPDATE']);
2069
+
2070
+ $definition[] = sprintf(' CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) ON DELETE %s ON UPDATE %s',
2071
+ $this->quoteIdentifier($fkData['FK_NAME']),
2072
+ $this->quoteIdentifier($fkData['COLUMN_NAME']),
2073
+ $this->quoteIdentifier($fkData['REF_TABLE_NAME']),
2074
+ $this->quoteIdentifier($fkData['REF_COLUMN_NAME']),
2075
+ $onDelete,
2076
+ $onUpdate
2077
+ );
2078
+ }
2079
+ }
2080
+
2081
+ return $definition;
2082
+ }
2083
+
2084
+ /**
2085
+ * Retrieve table options definition array for create table
2086
+ *
2087
+ * @param Varien_Db_Ddl_Table $table
2088
+ * @return array
2089
+ * @throws Zend_Db_Exception
2090
+ */
2091
+ protected function _getOptionsDefinition(Varien_Db_Ddl_Table $table)
2092
+ {
2093
+ $definition = array();
2094
+ $comment = $table->getComment();
2095
+ if (empty($comment)) {
2096
+ throw new Zend_Db_Exception('Comment for table is required and must be defined');
2097
+ }
2098
+ $definition[] = $this->quoteInto('COMMENT=?', $comment);
2099
+
2100
+ $tableProps = array(
2101
+ 'type' => 'ENGINE=%s',
2102
+ 'checksum' => 'CHECKSUM=%d',
2103
+ 'auto_increment' => 'AUTO_INCREMENT=%d',
2104
+ 'avg_row_length' => 'AVG_ROW_LENGTH=%d',
2105
+ 'max_rows' => 'MAX_ROWS=%d',
2106
+ 'min_rows' => 'MIN_ROWS=%d',
2107
+ 'delay_key_write' => 'DELAY_KEY_WRITE=%d',
2108
+ 'row_format' => 'row_format=%s',
2109
+ 'charset' => 'charset=%s',
2110
+ 'collate' => 'COLLATE=%s'
2111
+ );
2112
+ foreach ($tableProps as $key => $mask) {
2113
+ $v = $table->getOption($key);
2114
+ if ($v !== null) {
2115
+ $definition[] = sprintf($mask, $v);
2116
+ }
2117
+ }
2118
+
2119
+ return $definition;
2120
+ }
2121
+ /**
2122
+ * Retrieve column definition fragment
2123
+ *
2124
+ * @param array $options
2125
+ * @param string $ddlType Table DDL Column type constant
2126
+ * @throws Varien_Exception
2127
+ * @return string
2128
+ * @throws Zend_Db_Exception
2129
+ */
2130
+ protected function _getColumnDefinition($options, $ddlType = null)
2131
+ {
2132
+ // convert keys to uppercase
2133
+ $options = array_change_key_case($options, CASE_UPPER);
2134
+ $cType = null;
2135
+ $cUnsigned = false;
2136
+ $cNullable = true;
2137
+ $cDefault = false;
2138
+ $cIdentity = false;
2139
+
2140
+ // detect and validate column type
2141
+ if ($ddlType === null) {
2142
+ $ddlType = $this->_getDdlType($options);
2143
+ }
2144
+
2145
+ if (empty($ddlType) || !isset($this->_ddlColumnTypes[$ddlType])) {
2146
+ throw new Zend_Db_Exception('Invalid column definition data');
2147
+ }
2148
+
2149
+ // column size
2150
+ $cType = $this->_ddlColumnTypes[$ddlType];
2151
+ switch ($ddlType) {
2152
+ case Varien_Db_Ddl_Table::TYPE_SMALLINT:
2153
+ case Varien_Db_Ddl_Table::TYPE_INTEGER:
2154
+ case Varien_Db_Ddl_Table::TYPE_BIGINT:
2155
+ if (!empty($options['UNSIGNED'])) {
2156
+ $cUnsigned = true;
2157
+ }
2158
+ break;
2159
+ case Varien_Db_Ddl_Table::TYPE_DECIMAL:
2160
+ case Varien_Db_Ddl_Table::TYPE_NUMERIC:
2161
+ $precision = 10;
2162
+ $scale = 0;
2163
+ $match = array();
2164
+ if (!empty($options['LENGTH']) && preg_match('#^\(?(\d+),(\d+)\)?$#', $options['LENGTH'], $match)) {
2165
+ $precision = $match[1];
2166
+ $scale = $match[2];
2167
+ } else {
2168
+ if (isset($options['SCALE']) && is_numeric($options['SCALE'])) {
2169
+ $scale = $options['SCALE'];
2170
+ }
2171
+ if (isset($options['PRECISION']) && is_numeric($options['PRECISION'])) {
2172
+ $precision = $options['PRECISION'];
2173
+ }
2174
+ }
2175
+ $cType .= sprintf('(%d,%d)', $precision, $scale);
2176
+ break;
2177
+ case Varien_Db_Ddl_Table::TYPE_TEXT:
2178
+ case Varien_Db_Ddl_Table::TYPE_BLOB:
2179
+ case Varien_Db_Ddl_Table::TYPE_VARBINARY:
2180
+ if (empty($options['LENGTH'])) {
2181
+ $length = Varien_Db_Ddl_Table::DEFAULT_TEXT_SIZE;
2182
+ } else {
2183
+ $length = $this->_parseTextSize($options['LENGTH']);
2184
+ }
2185
+ if ($length <= 255) {
2186
+ $cType = $ddlType == Varien_Db_Ddl_Table::TYPE_TEXT ? 'varchar' : 'varbinary';
2187
+ $cType = sprintf('%s(%d)', $cType, $length);
2188
+ } else if ($length > 255 && $length <= 65536) {
2189
+ $cType = $ddlType == Varien_Db_Ddl_Table::TYPE_TEXT ? 'text' : 'blob';
2190
+ } else if ($length > 65536 && $length <= 16777216) {
2191
+ $cType = $ddlType == Varien_Db_Ddl_Table::TYPE_TEXT ? 'mediumtext' : 'mediumblob';
2192
+ } else {
2193
+ $cType = $ddlType == Varien_Db_Ddl_Table::TYPE_TEXT ? 'longtext' : 'longblob';
2194
+ }
2195
+ break;
2196
+ }
2197
+
2198
+ if (array_key_exists('DEFAULT', $options)) {
2199
+ $cDefault = $options['DEFAULT'];
2200
+ }
2201
+ if (array_key_exists('NULLABLE', $options)) {
2202
+ $cNullable = (bool)$options['NULLABLE'];
2203
+ }
2204
+ if (!empty($options['IDENTITY']) || !empty($options['AUTO_INCREMENT'])) {
2205
+ $cIdentity = true;
2206
+ }
2207
+
2208
+ /* For cases when tables created from createTableByDdl()
2209
+ * where default value can be quoted already.
2210
+ * We need to avoid "double-quoting" here
2211
+ */
2212
+ if ( $cDefault !== null && strlen($cDefault)) {
2213
+ $cDefault = str_replace("'", '', $cDefault);
2214
+ }
2215
+
2216
+ // prepare default value string
2217
+ if ($ddlType == Varien_Db_Ddl_Table::TYPE_TIMESTAMP) {
2218
+ if ($cDefault === null) {
2219
+ $cDefault = new Zend_Db_Expr('NULL');
2220
+ } elseif ($cDefault == Varien_Db_Ddl_Table::TIMESTAMP_INIT) {
2221
+ $cDefault = new Zend_Db_Expr('CURRENT_TIMESTAMP');
2222
+ } else if ($cDefault == Varien_Db_Ddl_Table::TIMESTAMP_UPDATE) {
2223
+ $cDefault = new Zend_Db_Expr('0 ON UPDATE CURRENT_TIMESTAMP');
2224
+ } else if ($cDefault == Varien_Db_Ddl_Table::TIMESTAMP_INIT_UPDATE) {
2225
+ $cDefault = new Zend_Db_Expr('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
2226
+ } else {
2227
+ $cDefault = false;
2228
+ }
2229
+ } else if (is_null($cDefault) && $cNullable) {
2230
+ $cDefault = new Zend_Db_Expr('NULL');
2231
+ }
2232
+
2233
+ if (empty($options['COMMENT'])) {
2234
+ $comment = '';
2235
+ } else {
2236
+ $comment = $options['COMMENT'];
2237
+ }
2238
+ return sprintf('%s%s%s%s%s COMMENT %s',
2239
+ $cType,
2240
+ $cUnsigned ? ' UNSIGNED' : '',
2241
+ $cNullable ? ' NULL' : ' NOT NULL',
2242
+ $cDefault !== false ? $this->quoteInto(' default ?', $cDefault) : '',
2243
+ $cIdentity ? ' auto_increment' : '',
2244
+ $this->quote($comment)
2245
+ );
2246
+ }
2247
+
2248
+ /**
2249
+ * Drop table from database
2250
+ *
2251
+ * @param string $tableName
2252
+ * @param string $schemaName
2253
+ * @return boolean
2254
+ */
2255
+ public function dropTable($tableName, $schemaName = null)
2256
+ {
2257
+ $table = $this->quoteIdentifier($this->_getTableName($tableName, $schemaName));
2258
+ $query = 'DROP TABLE IF EXISTS ' . $table;
2259
+ $this->query($query);
2260
+ return true;
2261
+ }
2262
+
2263
+ /**
2264
+ * Truncate a table
2265
+ *
2266
+ * @param string $tableName
2267
+ * @param string $schemaName
2268
+ * @return Varien_Db_Adapter_Pdo_Mysql
2269
+ * @throws Zend_Db_Exception
2270
+ */
2271
+ public function truncateTable($tableName, $schemaName = null)
2272
+ {
2273
+ if (!$this->isTableExists($tableName, $schemaName)) {
2274
+ throw new Zend_Db_Exception(sprintf('Table "%s" is not exists', $tableName));
2275
+ }
2276
+
2277
+ $table = $this->quoteIdentifier($this->_getTableName($tableName, $schemaName));
2278
+ $query = 'TRUNCATE TABLE ' . $table;
2279
+ $this->query($query);
2280
+
2281
+ return $this;
2282
+ }
2283
+
2284
+ /**
2285
+ * Check is a table exists
2286
+ *
2287
+ * @param string $tableName
2288
+ * @param string $schemaName
2289
+ * @return boolean
2290
+ */
2291
+ public function isTableExists($tableName, $schemaName = null)
2292
+ {
2293
+ return $this->showTableStatus($tableName, $schemaName) !== false;
2294
+ }
2295
+
2296
+ /**
2297
+ * Rename table
2298
+ *
2299
+ * @param string $oldTableName
2300
+ * @param string $newTableName
2301
+ * @param string $schemaName
2302
+ * @return boolean
2303
+ * @throws Zend_Db_Exception
2304
+ */
2305
+ public function renameTable($oldTableName, $newTableName, $schemaName = null)
2306
+ {
2307
+ if (!$this->isTableExists($oldTableName, $schemaName)) {
2308
+ throw new Zend_Db_Exception(sprintf('Table "%s" is not exists', $oldTableName));
2309
+ }
2310
+ if ($this->isTableExists($newTableName, $schemaName)) {
2311
+ throw new Zend_Db_Exception(sprintf('Table "%s" already exists', $newTableName));
2312
+ }
2313
+
2314
+ $oldTable = $this->_getTableName($oldTableName, $schemaName);
2315
+ $newTable = $this->_getTableName($newTableName, $schemaName);
2316
+
2317
+ $query = sprintf('ALTER TABLE %s RENAME TO %s', $oldTable, $newTable);
2318
+ $this->query($query);
2319
+
2320
+ $this->resetDdlCache($oldTableName, $schemaName);
2321
+
2322
+ return true;
2323
+ }
2324
+
2325
+ /**
2326
+ * Add new index to table name
2327
+ *
2328
+ * @param string $tableName
2329
+ * @param string $indexName
2330
+ * @param string|array $fields the table column name or array of ones
2331
+ * @param string $indexType the index type
2332
+ * @param string $schemaName
2333
+ * @return Zend_Db_Statement_Interface
2334
+ * @throws Zend_Db_Exception|Exception
2335
+ */
2336
+ public function addIndex($tableName, $indexName, $fields,
2337
+ $indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX, $schemaName = null)
2338
+ {
2339
+ $columns = $this->describeTable($tableName, $schemaName);
2340
+ $keyList = $this->getIndexList($tableName, $schemaName);
2341
+
2342
+ $query = sprintf('ALTER TABLE %s', $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)));
2343
+ if (isset($keyList[strtoupper($indexName)])) {
2344
+ if ($keyList[strtoupper($indexName)]['INDEX_TYPE'] == Varien_Db_Adapter_Interface::INDEX_TYPE_PRIMARY) {
2345
+ $query .= ' DROP PRIMARY KEY,';
2346
+ } else {
2347
+ $query .= sprintf(' DROP INDEX %s,', $this->quoteIdentifier($indexName));
2348
+ }
2349
+ }
2350
+
2351
+ if (!is_array($fields)) {
2352
+ $fields = array($fields);
2353
+ }
2354
+
2355
+ $fieldSql = array();
2356
+ foreach ($fields as $field) {
2357
+ if (!isset($columns[$field])) {
2358
+ $msg = sprintf('There is no field "%s" that you are trying to create an index on "%s"',
2359
+ $field, $tableName);
2360
+ throw new Zend_Db_Exception($msg);
2361
+ }
2362
+ $fieldSql[] = $this->quoteIdentifier($field);
2363
+ }
2364
+ $fieldSql = implode(',', $fieldSql);
2365
+
2366
+ switch (strtolower($indexType)) {
2367
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_PRIMARY:
2368
+ $condition = 'PRIMARY KEY';
2369
+ break;
2370
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE:
2371
+ $condition = 'UNIQUE ' . $this->quoteIdentifier($indexName);
2372
+ break;
2373
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_FULLTEXT:
2374
+ $condition = 'FULLTEXT ' . $this->quoteIdentifier($indexName);
2375
+ break;
2376
+ default:
2377
+ $condition = 'INDEX ' . $this->quoteIdentifier($indexName);
2378
+ break;
2379
+ }
2380
+
2381
+ $query .= sprintf(' ADD %s (%s)', $condition, $fieldSql);
2382
+
2383
+ $cycle = true;
2384
+ while ($cycle === true) {
2385
+ try {
2386
+ $result = $this->raw_query($query);
2387
+ $cycle = false;
2388
+ } catch (Exception $e) {
2389
+ if (in_array(strtolower($indexType), array('primary', 'unique'))) {
2390
+ $match = array();
2391
+ if (preg_match('#SQLSTATE\[23000\]: [^:]+: 1062[^\']+\'([\d-\.]+)\'#', $e->getMessage(), $match)) {
2392
+ $ids = explode('-', $match[1]);
2393
+ $this->_removeDuplicateEntry($tableName, $fields, $ids);
2394
+ continue;
2395
+ }
2396
+ }
2397
+ throw $e;
2398
+ }
2399
+ }
2400
+
2401
+ $this->resetDdlCache($tableName, $schemaName);
2402
+
2403
+ return $result;
2404
+ }
2405
+
2406
+ /**
2407
+ * Drop the index from table
2408
+ *
2409
+ * @param string $tableName
2410
+ * @param string $keyName
2411
+ * @param string $schemaName
2412
+ * @return bool|Zend_Db_Statement_Interface
2413
+ */
2414
+ public function dropIndex($tableName, $keyName, $schemaName = null)
2415
+ {
2416
+ $indexList = $this->getIndexList($tableName, $schemaName);
2417
+ $keyName = strtoupper($keyName);
2418
+ if (!isset($indexList[$keyName])) {
2419
+ return true;
2420
+ }
2421
+
2422
+ if ($keyName == 'PRIMARY') {
2423
+ $cond = 'DROP PRIMARY KEY';
2424
+ } else {
2425
+ $cond = 'DROP KEY ' . $this->quoteIdentifier($indexList[$keyName]['KEY_NAME']);
2426
+ }
2427
+ $sql = sprintf('ALTER TABLE %s %s',
2428
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
2429
+ $cond);
2430
+
2431
+ $this->resetDdlCache($tableName, $schemaName);
2432
+ return $this->raw_query($sql);
2433
+ }
2434
+
2435
+ /**
2436
+ * Retrieve Foreign Key name
2437
+ * @param string $fkName
2438
+ * @return string
2439
+ */
2440
+ protected function _getForeignKeyName($fkName)
2441
+ {
2442
+ if (substr($fkName, 0, 3) != 'FK_') {
2443
+ $fkName = 'FK_' . $fkName;
2444
+ }
2445
+ return $fkName;
2446
+ }
2447
+
2448
+ /**
2449
+ * Add new Foreign Key to table
2450
+ * If Foreign Key with same name is exist - it will be deleted
2451
+ *
2452
+ * @param string $fkName
2453
+ * @param string $tableName
2454
+ * @param string $columnName
2455
+ * @param string $refTableName
2456
+ * @param string $refColumnName
2457
+ * @param string $onDelete
2458
+ * @param string $onUpdate
2459
+ * @param boolean $purge trying remove invalid data
2460
+ * @param string $schemaName
2461
+ * @param string $refSchemaName
2462
+ * @return Varien_Db_Adapter_Pdo_Mysql
2463
+ */
2464
+ public function addForeignKey($fkName, $tableName, $columnName, $refTableName, $refColumnName,
2465
+ $onDelete = Varien_Db_Adapter_Interface::FK_ACTION_CASCADE,
2466
+ $onUpdate = Varien_Db_Adapter_Interface::FK_ACTION_CASCADE,
2467
+ $purge = false, $schemaName = null, $refSchemaName = null)
2468
+ {
2469
+ $fkName = $this->_getForeignKeyName($fkName);
2470
+
2471
+ $this->dropForeignKey($tableName, $fkName, $schemaName);
2472
+
2473
+ if ($purge) {
2474
+ $this->purgeOrphanRecords($tableName, $columnName, $refTableName, $refColumnName, $onDelete);
2475
+ }
2476
+
2477
+ $query = sprintf('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)',
2478
+ $this->quoteIdentifier($this->_getTableName($tableName, $schemaName)),
2479
+ $this->quoteIdentifier($fkName),
2480
+ $this->quoteIdentifier($columnName),
2481
+ $this->quoteIdentifier($this->_getTableName($refTableName, $refSchemaName)),
2482
+ $this->quoteIdentifier($refColumnName)
2483
+ );
2484
+
2485
+ if ($onDelete !== null) {
2486
+ $query .= ' ON DELETE ' . strtoupper($onDelete);
2487
+ }
2488
+ if ($onUpdate !== null) {
2489
+ $query .= ' ON UPDATE ' . strtoupper($onUpdate);
2490
+ }
2491
+
2492
+ $result = $this->raw_query($query);
2493
+ $this->resetDdlCache($tableName);
2494
+ return $result;
2495
+ }
2496
+
2497
+ /**
2498
+ * Format Date to internal database date format
2499
+ *
2500
+ * @param int|string|Zend_Date $date
2501
+ * @param boolean $includeTime
2502
+ * @return Zend_Db_Expr
2503
+ */
2504
+ public function formatDate($date, $includeTime = true)
2505
+ {
2506
+ $date = Varien_Date::formatDate($date, $includeTime);
2507
+
2508
+ if ($date === null) {
2509
+ return new Zend_Db_Expr('NULL');
2510
+ }
2511
+
2512
+ return new Zend_Db_Expr($this->quote($date));
2513
+ }
2514
+
2515
+ /**
2516
+ * Run additional environment before setup
2517
+ *
2518
+ * @return Varien_Db_Adapter_Pdo_Mysql
2519
+ */
2520
+ public function startSetup()
2521
+ {
2522
+ $this->raw_query("SET SQL_MODE=''");
2523
+ $this->raw_query("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0");
2524
+ $this->raw_query("SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");
2525
+
2526
+ return $this;
2527
+ }
2528
+
2529
+ /**
2530
+ * Run additional environment after setup
2531
+ *
2532
+ * @return Varien_Db_Adapter_Pdo_Mysql
2533
+ */
2534
+ public function endSetup()
2535
+ {
2536
+ $this->raw_query("SET SQL_MODE=IFNULL(@OLD_SQL_MODE,'')");
2537
+ $this->raw_query("SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS=0, 0, 1)");
2538
+
2539
+ return $this;
2540
+ }
2541
+
2542
+ /**
2543
+ * Build SQL statement for condition
2544
+ *
2545
+ * If $condition integer or string - exact value will be filtered ('eq' condition)
2546
+ *
2547
+ * If $condition is array is - one of the following structures is expected:
2548
+ * - array("from" => $fromValue, "to" => $toValue)
2549
+ * - array("eq" => $equalValue)
2550
+ * - array("neq" => $notEqualValue)
2551
+ * - array("like" => $likeValue)
2552
+ * - array("in" => array($inValues))
2553
+ * - array("nin" => array($notInValues))
2554
+ * - array("notnull" => $valueIsNotNull)
2555
+ * - array("null" => $valueIsNull)
2556
+ * - array("gt" => $greaterValue)
2557
+ * - array("lt" => $lessValue)
2558
+ * - array("gteq" => $greaterOrEqualValue)
2559
+ * - array("lteq" => $lessOrEqualValue)
2560
+ * - array("finset" => $valueInSet)
2561
+ * - array("regexp" => $regularExpression)
2562
+ * - array("seq" => $stringValue)
2563
+ * - array("sneq" => $stringValue)
2564
+ *
2565
+ * If non matched - sequential array is expected and OR conditions
2566
+ * will be built using above mentioned structure
2567
+ *
2568
+ * @param string|array $fieldName
2569
+ * @param integer|string|array $condition
2570
+ * @return string
2571
+ */
2572
+ public function prepareSqlCondition($fieldName, $condition)
2573
+ {
2574
+ $conditionKeyMap = array(
2575
+ 'eq' => "{{fieldName}} = ?",
2576
+ 'neq' => "{{fieldName}} != ?",
2577
+ 'like' => "{{fieldName}} LIKE ?",
2578
+ 'nlike' => "{{fieldName}} NOT LIKE ?",
2579
+ 'in' => "{{fieldName}} IN(?)",
2580
+ 'nin' => "{{fieldName}} NOT IN(?)",
2581
+ 'is' => "{{fieldName}} IS ?",
2582
+ 'notnull' => "{{fieldName}} IS NOT NULL",
2583
+ 'null' => "{{fieldName}} IS NULL",
2584
+ 'gt' => "{{fieldName}} > ?",
2585
+ 'lt' => "{{fieldName}} < ?",
2586
+ 'gteq' => "{{fieldName}} >= ?",
2587
+ 'lteq' => "{{fieldName}} <= ?",
2588
+ 'finset' => "FIND_IN_SET(?, {{fieldName}})",
2589
+ 'regexp' => "{{fieldName}} REGEXP ?",
2590
+ 'from' => "{{fieldName}} >= ?",
2591
+ 'to' => "{{fieldName}} <= ?",
2592
+ 'seq' => null,
2593
+ 'sneq' => null
2594
+ );
2595
+
2596
+ $query = '';
2597
+ if (is_array($condition)) {
2598
+ if (isset($condition['field_expr'])) {
2599
+ $fieldName = str_replace('#?', $this->quoteIdentifier($fieldName), $condition['field_expr']);
2600
+ unset($condition['field_expr']);
2601
+ }
2602
+ $key = key(array_intersect_key($condition, $conditionKeyMap));
2603
+
2604
+ if (isset($condition['from']) || isset($condition['to'])) {
2605
+ if (isset($condition['from'])) {
2606
+ $from = $this->_prepareSqlDateCondition($condition, 'from');
2607
+ $query = $this->_prepareQuotedSqlCondition($conditionKeyMap['from'], $from, $fieldName);
2608
+ }
2609
+
2610
+ if (isset($condition['to'])) {
2611
+ $query .= empty($query) ? '' : ' AND ';
2612
+ $to = $this->_prepareSqlDateCondition($condition, 'to');
2613
+ $query = $this->_prepareQuotedSqlCondition($query . $conditionKeyMap['to'], $to, $fieldName);
2614
+ }
2615
+ } elseif (array_key_exists($key, $conditionKeyMap)) {
2616
+ $value = $condition[$key];
2617
+ if (($key == 'seq') || ($key == 'sneq')) {
2618
+ $key = $this->_transformStringSqlCondition($key, $value);
2619
+ }
2620
+ $query = $this->_prepareQuotedSqlCondition($conditionKeyMap[$key], $value, $fieldName);
2621
+ } else {
2622
+ $queries = array();
2623
+ foreach ($condition as $orCondition) {
2624
+ $queries[] = sprintf('(%s)', $this->prepareSqlCondition($fieldName, $orCondition));
2625
+ }
2626
+
2627
+ $query = sprintf('(%s)', implode(' OR ', $queries));
2628
+ }
2629
+ } else {
2630
+ $query = $this->_prepareQuotedSqlCondition($conditionKeyMap['eq'], (string)$condition, $fieldName);
2631
+ }
2632
+
2633
+ return $query;
2634
+ }
2635
+
2636
+ /**
2637
+ * Prepare Sql condition
2638
+ *
2639
+ * @param $text Condition value
2640
+ * @param mixed $value
2641
+ * @param string $fieldName
2642
+ * @return string
2643
+ */
2644
+ protected function _prepareQuotedSqlCondition($text, $value, $fieldName)
2645
+ {
2646
+ $sql = $this->quoteInto($text, $value);
2647
+ $sql = str_replace('{{fieldName}}', $fieldName, $sql);
2648
+ return $sql;
2649
+ }
2650
+
2651
+ /**
2652
+ * Transforms sql condition key 'seq' / 'sneq' that is used for comparing string values to its analog:
2653
+ * - 'null' / 'notnull' for empty strings
2654
+ * - 'eq' / 'neq' for non-empty strings
2655
+ *
2656
+ * @param string $conditionKey
2657
+ * @param mixed $value
2658
+ * @return string
2659
+ */
2660
+ protected function _transformStringSqlCondition($conditionKey, $value)
2661
+ {
2662
+ $value = (string) $value;
2663
+ if ($value == '') {
2664
+ return ($conditionKey == 'seq') ? 'null' : 'notnull';
2665
+ } else {
2666
+ return ($conditionKey == 'seq') ? 'eq' : 'neq';
2667
+ }
2668
+ }
2669
+
2670
+ /**
2671
+ * Prepare value for save in column
2672
+ * Return converted to column data type value
2673
+ *
2674
+ * @param array $column the column describe array
2675
+ * @param mixed $value
2676
+ * @return mixed
2677
+ */
2678
+ public function prepareColumnValue(array $column, $value)
2679
+ {
2680
+ if ($value instanceof Zend_Db_Expr) {
2681
+ return $value;
2682
+ }
2683
+ if ($value instanceof Varien_Db_Statement_Parameter) {
2684
+ return $value;
2685
+ }
2686
+
2687
+ // return original value if invalid column describe data
2688
+ if (!isset($column['DATA_TYPE'])) {
2689
+ return $value;
2690
+ }
2691
+
2692
+ // return null
2693
+ if (is_null($value) && $column['NULLABLE']) {
2694
+ return null;
2695
+ }
2696
+
2697
+ switch ($column['DATA_TYPE']) {
2698
+ case 'smallint':
2699
+ case 'int':
2700
+ case 'bigint':
2701
+ $value = (int)$value;
2702
+ break;
2703
+
2704
+ case 'decimal':
2705
+ $precision = 10;
2706
+ $scale = 0;
2707
+ if (isset($column['SCALE'])) {
2708
+ $scale = $column['SCALE'];
2709
+ }
2710
+ if (isset($column['PRECISION'])) {
2711
+ $precision = $column['PRECISION'];
2712
+ }
2713
+ $format = sprintf('%%%d.%dF', $precision - $scale, $scale);
2714
+ $value = (float)sprintf($format, $value);
2715
+ break;
2716
+
2717
+ case 'float':
2718
+ $value = (float)sprintf('%F', $value);
2719
+ break;
2720
+
2721
+ case 'date':
2722
+ $value = $this->formatDate($value, false);
2723
+ break;
2724
+ case 'datetime':
2725
+ case 'timestamp':
2726
+ $value = $this->formatDate($value);
2727
+ break;
2728
+
2729
+ case 'varchar':
2730
+ case 'mediumtext':
2731
+ case 'text':
2732
+ case 'longtext':
2733
+ $value = (string)$value;
2734
+ if ($column['NULLABLE'] && $value == '') {
2735
+ $value = null;
2736
+ }
2737
+ break;
2738
+
2739
+ case 'varbinary':
2740
+ case 'mediumblob':
2741
+ case 'blob':
2742
+ case 'longblob':
2743
+ // No special processing for MySQL is needed
2744
+ break;
2745
+ }
2746
+
2747
+ return $value;
2748
+ }
2749
+
2750
+ /**
2751
+ * Generate fragment of SQL, that check condition and return true or false value
2752
+ *
2753
+ * @param string $condition expression
2754
+ * @param string $true true value
2755
+ * @param string $false false value
2756
+ */
2757
+ public function getCheckSql($condition, $true, $false)
2758
+ {
2759
+ return new Zend_Db_Expr("IF({$condition}, {$true}, {$false})");
2760
+ }
2761
+
2762
+ /**
2763
+ * Returns valid IFNULL expression
2764
+ *
2765
+ * @param string $column
2766
+ * @param string $value OPTIONAL. Applies when $expression is NULL
2767
+ * @return Zend_Db_Expr
2768
+ */
2769
+ public function getIfNullSql($expression, $value = 0)
2770
+ {
2771
+ if ($expression instanceof Zend_Db_Expr || $expression instanceof Zend_Db_Select) {
2772
+ $expression = sprintf("IFNULL((%s), %s)", $expression, $value);
2773
+ } else {
2774
+ $expression = sprintf("IFNULL(%s, %s)", $expression, $value);
2775
+ }
2776
+
2777
+ return new Zend_Db_Expr($expression);
2778
+ }
2779
+
2780
+ /**
2781
+ * Generate fragment of SQL, that check value against multiple condition cases
2782
+ * and return different result depends on them
2783
+ *
2784
+ * @param string $valueName Name of value to check
2785
+ * @param array $casesResults Cases and results
2786
+ * @param string $defaultValue value to use if value doesn't confirm to any cases
2787
+ */
2788
+ public function getCaseSql($valueName, $casesResults, $defaultValue = null)
2789
+ {
2790
+ $expression = 'CASE ' . $valueName;
2791
+ foreach ($casesResults as $case => $result) {
2792
+ $expression .= ' WHEN ' . $case . ' THEN ' . $result;
2793
+ }
2794
+ if ($defaultValue !== null) {
2795
+ $expression .= ' ELSE ' . $defaultValue;
2796
+ }
2797
+ $expression .= ' END';
2798
+
2799
+ return new Zend_Db_Expr($expression);
2800
+ }
2801
+
2802
+ /**
2803
+ * Generate fragment of SQL, that combine together (concatenate) the results from data array
2804
+ * All arguments in data must be quoted
2805
+ *
2806
+ * @param array $data
2807
+ * @param string $separator concatenate with separator
2808
+ * @return Zend_Db_Expr
2809
+ */
2810
+ public function getConcatSql(array $data, $separator = null)
2811
+ {
2812
+ $format = empty($separator) ? 'CONCAT(%s)' : "CONCAT_WS('{$separator}', %s)";
2813
+ return new Zend_Db_Expr(sprintf($format, implode(', ', $data)));
2814
+ }
2815
+
2816
+ /**
2817
+ * Generate fragment of SQL that returns length of character string
2818
+ * The string argument must be quoted
2819
+ *
2820
+ * @param string $string
2821
+ * @return Zend_Db_Expr
2822
+ */
2823
+ public function getLengthSql($string)
2824
+ {
2825
+ return new Zend_Db_Expr(sprintf('LENGTH(%s)', $string));
2826
+ }
2827
+
2828
+ /**
2829
+ * Generate fragment of SQL, that compare with two or more arguments, and returns the smallest
2830
+ * (minimum-valued) argument
2831
+ * All arguments in data must be quoted
2832
+ *
2833
+ * @param array $data
2834
+ * @return Zend_Db_Expr
2835
+ */
2836
+ public function getLeastSql(array $data)
2837
+ {
2838
+ return new Zend_Db_Expr(sprintf('LEAST(%s)', implode(', ', $data)));
2839
+ }
2840
+
2841
+ /**
2842
+ * Generate fragment of SQL, that compare with two or more arguments, and returns the largest
2843
+ * (maximum-valued) argument
2844
+ * All arguments in data must be quoted
2845
+ *
2846
+ * @param array $data
2847
+ * @return Zend_Db_Expr
2848
+ */
2849
+ public function getGreatestSql(array $data)
2850
+ {
2851
+ return new Zend_Db_Expr(sprintf('GREATEST(%s)', implode(', ', $data)));
2852
+ }
2853
+
2854
+ /**
2855
+ * Get Interval Unit SQL fragment
2856
+ *
2857
+ * @param int $interval
2858
+ * @param string $unit
2859
+ * @return string
2860
+ * @throws Zend_Db_Exception
2861
+ */
2862
+ protected function _getIntervalUnitSql($interval, $unit)
2863
+ {
2864
+ if (!isset($this->_intervalUnits[$unit])) {
2865
+ throw new Zend_Db_Exception(sprintf('Undefined interval unit "%s" specified', $unit));
2866
+ }
2867
+
2868
+ return sprintf('INTERVAL %d %s', $interval, $this->_intervalUnits[$unit]);
2869
+ }
2870
+
2871
+ /**
2872
+ * Add time values (intervals) to a date value
2873
+ *
2874
+ * @see INTERVAL_* constants for $unit
2875
+ *
2876
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
2877
+ * @param int $interval
2878
+ * @param string $unit
2879
+ * @return Zend_Db_Expr
2880
+ */
2881
+ public function getDateAddSql($date, $interval, $unit)
2882
+ {
2883
+ $expr = sprintf('DATE_ADD(%s, %s)', $date, $this->_getIntervalUnitSql($interval, $unit));
2884
+ return new Zend_Db_Expr($expr);
2885
+ }
2886
+
2887
+ /**
2888
+ * Subtract time values (intervals) to a date value
2889
+ *
2890
+ * @see INTERVAL_* constants for $expr
2891
+ *
2892
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
2893
+ * @param int|string $interval
2894
+ * @param string $unit
2895
+ * @return Zend_Db_Expr
2896
+ */
2897
+ public function getDateSubSql($date, $interval, $unit)
2898
+ {
2899
+ $expr = sprintf('DATE_SUB(%s, %s)', $date, $this->_getIntervalUnitSql($interval, $unit));
2900
+ return new Zend_Db_Expr($expr);
2901
+ }
2902
+
2903
+ /**
2904
+ * Format date as specified
2905
+ *
2906
+ * Supported format Specifier
2907
+ *
2908
+ * %H Hour (00..23)
2909
+ * %i Minutes, numeric (00..59)
2910
+ * %s Seconds (00..59)
2911
+ * %d Day of the month, numeric (00..31)
2912
+ * %m Month, numeric (00..12)
2913
+ * %Y Year, numeric, four digits
2914
+ *
2915
+ * @param string $date quoted date value or non quoted SQL statement(field)
2916
+ * @param string $format
2917
+ * @return Zend_Db_Expr
2918
+ */
2919
+ public function getDateFormatSql($date, $format)
2920
+ {
2921
+ $expr = sprintf("DATE_FORMAT(%s, '%s')", $date, $format);
2922
+ return new Zend_Db_Expr($expr);
2923
+ }
2924
+
2925
+ /**
2926
+ * Extract the date part of a date or datetime expression
2927
+ *
2928
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
2929
+ * @return Zend_Db_Expr
2930
+ */
2931
+ public function getDatePartSql($date)
2932
+ {
2933
+ return new Zend_Db_Expr(sprintf('DATE(%s)', $date));
2934
+ }
2935
+
2936
+ /**
2937
+ * Extract part of a date
2938
+ *
2939
+ * @see INTERVAL_* constants for $unit
2940
+ *
2941
+ * @param Zend_Db_Expr|string $date quoted field name or SQL statement
2942
+ * @param string $unit
2943
+ * @return Zend_Db_Expr
2944
+ * @throws Zend_Db_Exception
2945
+ */
2946
+ public function getDateExtractSql($date, $unit)
2947
+ {
2948
+ if (!isset($this->_intervalUnits[$unit])) {
2949
+ throw new Zend_Db_Exception(sprintf('Undefined interval unit "%s" specified', $unit));
2950
+ }
2951
+
2952
+ $expr = sprintf('EXTRACT(%s FROM %s)', $this->_intervalUnits[$unit], $date);
2953
+ return new Zend_Db_Expr($expr);
2954
+ }
2955
+
2956
+ /**
2957
+ * Minus superfluous characters from hash.
2958
+ *
2959
+ * @param $hash
2960
+ * @param $prefix
2961
+ * @param $maxCharacters
2962
+ * @return string
2963
+ */
2964
+ protected function _minusSuperfluous($hash, $prefix, $maxCharacters)
2965
+ {
2966
+ $diff = strlen($hash) + strlen($prefix) - $maxCharacters;
2967
+ $superfluous = $diff / 2;
2968
+ $odd = $diff % 2;
2969
+ $hash = substr($hash, $superfluous, - ($superfluous + $odd));
2970
+ return $hash;
2971
+ }
2972
+
2973
+ /**
2974
+ * Retrieve valid table name
2975
+ * Check table name length and allowed symbols
2976
+ *
2977
+ * @param string $tableName
2978
+ * @return string
2979
+ */
2980
+ public function getTableName($tableName)
2981
+ {
2982
+ $prefix = 't_';
2983
+ if (strlen($tableName) > self::LENGTH_TABLE_NAME) {
2984
+ $shortName = Varien_Db_Helper::shortName($tableName);
2985
+ if (strlen($shortName) > self::LENGTH_TABLE_NAME) {
2986
+ $hash = md5($tableName);
2987
+ if (strlen($prefix.$hash) > self::LENGTH_TABLE_NAME) {
2988
+ $tableName = $this->_minusSuperfluous($hash, $prefix, self::LENGTH_TABLE_NAME);
2989
+ } else {
2990
+ $tableName = $prefix . $hash;
2991
+ }
2992
+ } else {
2993
+ $tableName = $shortName;
2994
+ }
2995
+ }
2996
+
2997
+ return $tableName;
2998
+ }
2999
+
3000
+ /**
3001
+ * Retrieve valid index name
3002
+ * Check index name length and allowed symbols
3003
+ *
3004
+ * @param string $tableName
3005
+ * @param string|array $fields the columns list
3006
+ * @param string $indexType
3007
+ * @return string
3008
+ */
3009
+ public function getIndexName($tableName, $fields, $indexType = '')
3010
+ {
3011
+ if (is_array($fields)) {
3012
+ $fields = implode('_', $fields);
3013
+ }
3014
+
3015
+ switch (strtolower($indexType)) {
3016
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE:
3017
+ $prefix = 'unq_';
3018
+ $shortPrefix = 'u_';
3019
+ break;
3020
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_FULLTEXT:
3021
+ $prefix = 'fti_';
3022
+ $shortPrefix = 'f_';
3023
+ break;
3024
+ case Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX:
3025
+ default:
3026
+ $prefix = 'idx_';
3027
+ $shortPrefix = 'i_';
3028
+ }
3029
+
3030
+ $hash = $tableName . '_' . $fields;
3031
+
3032
+ if (strlen($hash) + strlen($prefix) > self::LENGTH_INDEX_NAME) {
3033
+ $short = Varien_Db_Helper::shortName($prefix . $hash);
3034
+ if (strlen($short) > self::LENGTH_INDEX_NAME) {
3035
+ $hash = md5($hash);
3036
+ if (strlen($hash) + strlen($shortPrefix) > self::LENGTH_INDEX_NAME) {
3037
+ $hash = $this->_minusSuperfluous($hash, $shortPrefix, self::LENGTH_INDEX_NAME);
3038
+ }
3039
+ } else {
3040
+ $hash = $short;
3041
+ }
3042
+ } else {
3043
+ $hash = $prefix . $hash;
3044
+ }
3045
+
3046
+ return strtoupper($hash);
3047
+ }
3048
+
3049
+ /**
3050
+ * Retrieve valid foreign key name
3051
+ * Check foreign key name length and allowed symbols
3052
+ *
3053
+ * @param string $priTableName
3054
+ * @param string $priColumnName
3055
+ * @param string $refTableName
3056
+ * @param string $refColumnName
3057
+ * @return string
3058
+ */
3059
+ public function getForeignKeyName($priTableName, $priColumnName, $refTableName, $refColumnName)
3060
+ {
3061
+ $prefix = 'fk_';
3062
+ $hash = sprintf('%s_%s_%s_%s', $priTableName, $priColumnName, $refTableName, $refColumnName);
3063
+ if (strlen($prefix.$hash) > self::LENGTH_FOREIGN_NAME) {
3064
+ $short = Varien_Db_Helper::shortName($prefix.$hash);
3065
+ if (strlen($short) > self::LENGTH_FOREIGN_NAME) {
3066
+ $hash = md5($hash);
3067
+ if (strlen($prefix.$hash) > self::LENGTH_FOREIGN_NAME) {
3068
+ $hash = $this->_minusSuperfluous($hash, $prefix, self::LENGTH_FOREIGN_NAME);
3069
+ } else {
3070
+ $hash = $prefix . $hash;
3071
+ }
3072
+ } else {
3073
+ $hash = $short;
3074
+ }
3075
+ } else {
3076
+ $hash = $prefix . $hash;
3077
+ }
3078
+
3079
+ return strtoupper($hash);
3080
+ }
3081
+
3082
+ /**
3083
+ * Stop updating indexes
3084
+ *
3085
+ * @param string $tableName
3086
+ * @param string $schemaName
3087
+ * @return Varien_Db_Adapter_Pdo_Mysql
3088
+ */
3089
+ public function disableTableKeys($tableName, $schemaName = null)
3090
+ {
3091
+ $tableName = $this->_getTableName($tableName, $schemaName);
3092
+ $query = sprintf('ALTER TABLE %s DISABLE KEYS', $this->quoteIdentifier($tableName));
3093
+ $this->query($query);
3094
+
3095
+ return $this;
3096
+ }
3097
+
3098
+ /**
3099
+ * Re-create missing indexes
3100
+ *
3101
+ * @param string $tableName
3102
+ * @param string $schemaName
3103
+ * @return Varien_Db_Adapter_Pdo_Mysql
3104
+ */
3105
+ public function enableTableKeys($tableName, $schemaName = null)
3106
+ {
3107
+ $tableName = $this->_getTableName($tableName, $schemaName);
3108
+ $query = sprintf('ALTER TABLE %s ENABLE KEYS', $this->quoteIdentifier($tableName));
3109
+ $this->query($query);
3110
+
3111
+ return $this;
3112
+ }
3113
+
3114
+ /**
3115
+ * Get insert from Select object query
3116
+ *
3117
+ * @param Varien_Db_Select $select
3118
+ * @param string $table insert into table
3119
+ * @param array $fields
3120
+ * @param int $mode
3121
+ * @return string
3122
+ */
3123
+ public function insertFromSelect(Varien_Db_Select $select, $table, array $fields = array(), $mode = false)
3124
+ {
3125
+ $query = 'INSERT';
3126
+ if ($mode == self::INSERT_IGNORE) {
3127
+ $query .= ' IGNORE';
3128
+ }
3129
+ $query = sprintf('%s INTO %s', $query, $this->quoteIdentifier($table));
3130
+ if ($fields) {
3131
+ $columns = array_map(array($this, 'quoteIdentifier'), $fields);
3132
+ $query = sprintf('%s (%s)', $query, join(', ', $columns));
3133
+ }
3134
+
3135
+ $query = sprintf('%s %s', $query, $select->assemble());
3136
+
3137
+ if ($mode == self::INSERT_ON_DUPLICATE) {
3138
+ if (!$fields) {
3139
+ $describe = $this->describeTable($table);
3140
+ foreach ($describe as $column) {
3141
+ if ($column['PRIMARY'] === false) {
3142
+ $fields[] = $column['COLUMN_NAME'];
3143
+ }
3144
+ }
3145
+ }
3146
+ $update = array();
3147
+ foreach ($fields as $field) {
3148
+ $update[] = sprintf('%1$s = VALUES(%1$s)', $this->quoteIdentifier($field));
3149
+ }
3150
+
3151
+ if ($update) {
3152
+ $query = sprintf('%s ON DUPLICATE KEY UPDATE %s', $query, join(', ', $update));
3153
+ }
3154
+ }
3155
+
3156
+ return $query;
3157
+ }
3158
+
3159
+ /**
3160
+ * Get update table query using select object for join and update
3161
+ *
3162
+ * @param Varien_Db_Select $select
3163
+ * @param string|array $table
3164
+ * @return string
3165
+ */
3166
+ public function updateFromSelect(Varien_Db_Select $select, $table)
3167
+ {
3168
+ if (!is_array($table)) {
3169
+ $table = array($table => $table);
3170
+ }
3171
+
3172
+ // get table name and alias
3173
+ $keys = array_keys($table);
3174
+ $tableAlias = $keys[0];
3175
+ $tableName = $table[$keys[0]];
3176
+
3177
+ $query = sprintf('UPDATE %s', $this->quoteTableAs($tableName, $tableAlias));
3178
+
3179
+ // render JOIN conditions (FROM Part)
3180
+ $joinConds = array();
3181
+ foreach ($select->getPart(Zend_Db_Select::FROM) as $correlationName => $joinProp) {
3182
+ if ($joinProp['joinType'] == Zend_Db_Select::FROM) {
3183
+ $joinType = strtoupper(Zend_Db_Select::INNER_JOIN);
3184
+ } else {
3185
+ $joinType = strtoupper($joinProp['joinType']);
3186
+ }
3187
+ $joinTable = '';
3188
+ if ($joinProp['schema'] !== null) {
3189
+ $joinTable = sprintf('%s.', $this->quoteIdentifier($joinProp['schema']));
3190
+ }
3191
+ $joinTable .= $this->quoteTableAs($joinProp['tableName'], $correlationName);
3192
+
3193
+ $join = sprintf(' %s %s', $joinType, $joinTable);
3194
+
3195
+ if (!empty($joinProp['joinCondition'])) {
3196
+ $join = sprintf('%s ON %s', $join, $joinProp['joinCondition']);
3197
+ }
3198
+
3199
+ $joinConds[] = $join;
3200
+ }
3201
+
3202
+ if ($joinConds) {
3203
+ $query = sprintf("%s\n%s", $query, implode("\n", $joinConds));
3204
+ }
3205
+
3206
+ // render UPDATE SET
3207
+ $columns = array();
3208
+ foreach ($select->getPart(Zend_Db_Select::COLUMNS) as $columnEntry) {
3209
+ list($correlationName, $column, $alias) = $columnEntry;
3210
+ if (empty($alias)) {
3211
+ $alias = $column;
3212
+ }
3213
+ if (!$column instanceof Zend_Db_Expr && !empty($correlationName)) {
3214
+ $column = $this->quoteIdentifier(array($correlationName, $column));
3215
+ }
3216
+ $columns[] = sprintf('%s = %s', $this->quoteIdentifier(array($tableAlias, $alias)), $column);
3217
+ }
3218
+
3219
+ if (!$columns) {
3220
+ throw new Varien_Db_Exception('The columns for UPDATE statement are not defined');
3221
+ }
3222
+
3223
+ $query = sprintf("%s\nSET %s", $query, implode(', ', $columns));
3224
+
3225
+ // render WHERE
3226
+ $wherePart = $select->getPart(Zend_Db_Select::WHERE);
3227
+ if ($wherePart) {
3228
+ $query = sprintf("%s\nWHERE %s", $query, implode(' ', $wherePart));
3229
+ }
3230
+
3231
+ return $query;
3232
+ }
3233
+
3234
+ /**
3235
+ * Get delete from select object query
3236
+ *
3237
+ * @param Varien_Db_Select $select
3238
+ * @param string $table the table name or alias used in select
3239
+ * @return string|int
3240
+ */
3241
+ public function deleteFromSelect(Varien_Db_Select $select, $table)
3242
+ {
3243
+ $select = clone $select;
3244
+ $select->reset(Zend_Db_Select::DISTINCT);
3245
+ $select->reset(Zend_Db_Select::COLUMNS);
3246
+
3247
+ $query = sprintf('DELETE %s %s', $this->quoteIdentifier($table), $select->assemble());
3248
+
3249
+ return $query;
3250
+ }
3251
+
3252
+ /**
3253
+ * Calculate checksum for table or for group of tables
3254
+ *
3255
+ * @param array|string $tableNames array of tables names | table name
3256
+ * @param string $schemaName schema name
3257
+ * @return arrray
3258
+ */
3259
+ public function getTablesChecksum($tableNames, $schemaName = null)
3260
+ {
3261
+ $result = array();
3262
+ $tableNames = is_array($tableNames) ? $tableNames : array($tableNames);
3263
+
3264
+ foreach ($tableNames as $tableName) {
3265
+ $query = 'CHECKSUM TABLE ' . $this->_getTableName($tableName, $schemaName);
3266
+ $checkSumArray = $this->fetchRow($query);
3267
+ $result[$tableName] = $checkSumArray['Checksum'];
3268
+ }
3269
+
3270
+ return $result;
3271
+ }
3272
+
3273
+ /**
3274
+ * Check if the database support STRAIGHT JOIN
3275
+ *
3276
+ * @return boolean
3277
+ */
3278
+ public function supportStraightJoin()
3279
+ {
3280
+ return true;
3281
+ }
3282
+
3283
+ /**
3284
+ * Adds order by random to select object
3285
+ * Possible using integer field for optimization
3286
+ *
3287
+ * @param Varien_Db_Select $select
3288
+ * @param string $field
3289
+ * @return Varien_Db_Adapter_Pdo_Mysql
3290
+ */
3291
+ public function orderRand(Varien_Db_Select $select, $field = null)
3292
+ {
3293
+ if ($field !== null) {
3294
+ $expression = new Zend_Db_Expr(sprintf('RAND() * %s', $this->quoteIdentifier($field)));
3295
+ $select->columns(array('mage_rand' => $expression));
3296
+ $spec = new Zend_Db_Expr('mage_rand');
3297
+ } else {
3298
+ $spec = new Zend_Db_Expr('RAND()');
3299
+ }
3300
+ $select->order($spec);
3301
+
3302
+ return $this;
3303
+ }
3304
+
3305
+ /**
3306
+ * Render SQL FOR UPDATE clause
3307
+ *
3308
+ * @param string $sql
3309
+ * @return string
3310
+ */
3311
+ public function forUpdate($sql)
3312
+ {
3313
+ return sprintf('%s FOR UPDATE', $sql);
3314
+ }
3315
+
3316
+ /**
3317
+ * Prepare insert data
3318
+ *
3319
+ * @param mixed $row
3320
+ * @param array $bind
3321
+ * @return string
3322
+ */
3323
+ protected function _prepareInsertData($row, &$bind)
3324
+ {
3325
+ if (is_array($row)) {
3326
+ $line = array();
3327
+ foreach ($row as $value) {
3328
+ if ($value instanceof Zend_Db_Expr) {
3329
+ $line[] = $value->__toString();
3330
+ } else {
3331
+ $line[] = '?';
3332
+ $bind[] = $value;
3333
+ }
3334
+ }
3335
+ $line = implode(', ', $line);
3336
+ } elseif ($row instanceof Zend_Db_Expr) {
3337
+ $line = $row->__toString();
3338
+ } else {
3339
+ $line = '?';
3340
+ $bind[] = $row;
3341
+ }
3342
+
3343
+ return sprintf('(%s)', $line);
3344
+ }
3345
+
3346
+ /**
3347
+ * Return insert sql query
3348
+ *
3349
+ * @param string $tableName
3350
+ * @param array $columns
3351
+ * @param array $values
3352
+ * @return string
3353
+ */
3354
+ protected function _getInsertSqlQuery($tableName, array $columns, array $values)
3355
+ {
3356
+ $tableName = $this->quoteIdentifier($tableName, true);
3357
+ $columns = array_map(array($this, 'quoteIdentifier'), $columns);
3358
+ $columns = implode(',', $columns);
3359
+ $values = implode(', ', $values);
3360
+
3361
+ $insertSql = sprintf('INSERT INTO %s (%s) VALUES %s', $tableName, $columns, $values);
3362
+
3363
+ return $insertSql;
3364
+ }
3365
+
3366
+ /**
3367
+ * Return ddl type
3368
+ *
3369
+ * @param array $options
3370
+ * @return string
3371
+ */
3372
+ protected function _getDdlType($options)
3373
+ {
3374
+ $ddlType = null;
3375
+ if (isset($options['TYPE'])) {
3376
+ $ddlType = $options['TYPE'];
3377
+ } elseif (isset($options['COLUMN_TYPE'])) {
3378
+ $ddlType = $options['COLUMN_TYPE'];
3379
+ }
3380
+
3381
+ return $ddlType;
3382
+ }
3383
+
3384
+ /**
3385
+ * Return DDL action
3386
+ *
3387
+ * @param string $action
3388
+ * @return string
3389
+ */
3390
+ protected function _getDdlAction($action)
3391
+ {
3392
+ switch ($action) {
3393
+ case Varien_Db_Adapter_Interface::FK_ACTION_CASCADE:
3394
+ return Varien_Db_Ddl_Table::ACTION_CASCADE;
3395
+ case Varien_Db_Adapter_Interface::FK_ACTION_SET_NULL:
3396
+ return Varien_Db_Ddl_Table::ACTION_SET_NULL;
3397
+ case Varien_Db_Adapter_Interface::FK_ACTION_RESTRICT:
3398
+ return Varien_Db_Ddl_Table::ACTION_RESTRICT;
3399
+ default:
3400
+ return Varien_Db_Ddl_Table::ACTION_NO_ACTION;
3401
+ }
3402
+ }
3403
+
3404
+ /**
3405
+ * Prepare sql date condition
3406
+ *
3407
+ * @param array $condition
3408
+ * @param string $key
3409
+ * @return string
3410
+ */
3411
+ protected function _prepareSqlDateCondition($condition, $key)
3412
+ {
3413
+ if (empty($condition['date'])) {
3414
+ if (empty($condition['datetime'])) {
3415
+ $result = $condition[$key];
3416
+ } else {
3417
+ $result = $this->formatDate($condition[$key]);
3418
+ }
3419
+ } else {
3420
+ $result = $this->formatDate($condition[$key]);
3421
+ }
3422
+
3423
+ return $result;
3424
+ }
3425
+
3426
+ /**
3427
+ * Try to find installed primary key name, if not - formate new one.
3428
+ *
3429
+ * @param string $tableName Table name
3430
+ * @param string $schemaName OPTIONAL
3431
+ * @return string Primary Key name
3432
+ */
3433
+ public function getPrimaryKeyName($tableName, $schemaName = null)
3434
+ {
3435
+ $indexes = $this->getIndexList($tableName, $schemaName);
3436
+ if (isset($indexes['PRIMARY'])) {
3437
+ return $indexes['PRIMARY']['KEY_NAME'];
3438
+ } else {
3439
+ return 'PK_' . strtoupper($tableName);
3440
+ }
3441
+ }
3442
+
3443
+ /**
3444
+ * Parse text size
3445
+ * Returns max allowed size if value great it
3446
+ *
3447
+ * @param string|int $size
3448
+ * @return int
3449
+ */
3450
+ protected function _parseTextSize($size)
3451
+ {
3452
+ $size = trim($size);
3453
+ $last = strtolower(substr($size, -1));
3454
+
3455
+ switch ($last) {
3456
+ case 'k':
3457
+ $size = intval($size) * 1024;
3458
+ break;
3459
+ case 'm':
3460
+ $size = intval($size) * 1024 * 1024;
3461
+ break;
3462
+ case 'g':
3463
+ $size = intval($size) * 1024 * 1024 * 1024;
3464
+ break;
3465
+ }
3466
+
3467
+ if (empty($size)) {
3468
+ return Varien_Db_Ddl_Table::DEFAULT_TEXT_SIZE;
3469
+ }
3470
+ if ($size >= Varien_Db_Ddl_Table::MAX_TEXT_SIZE) {
3471
+ return Varien_Db_Ddl_Table::MAX_TEXT_SIZE;
3472
+ }
3473
+
3474
+ return intval($size);
3475
+ }
3476
+
3477
+ /**
3478
+ * Converts fetched blob into raw binary PHP data.
3479
+ * The MySQL drivers do it nice, no processing required.
3480
+ *
3481
+ * @mixed $value
3482
+ * @return mixed
3483
+ */
3484
+ public function decodeVarbinary($value)
3485
+ {
3486
+ return $value;
3487
+ }
3488
+ }
lib/Varien/Db/Ddl/Table.php ADDED
@@ -0,0 +1,694 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Data Definition for table
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Db
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Db_Ddl_Table
36
+ {
37
+ /**
38
+ * Types of columns
39
+ */
40
+ const TYPE_BOOLEAN = 'boolean';
41
+ const TYPE_SMALLINT = 'smallint';
42
+ const TYPE_INTEGER = 'integer';
43
+ const TYPE_BIGINT = 'bigint';
44
+ const TYPE_FLOAT = 'float';
45
+ const TYPE_NUMERIC = 'numeric';
46
+ const TYPE_DECIMAL = 'decimal';
47
+ const TYPE_DATE = 'date';
48
+ const TYPE_TIMESTAMP = 'timestamp'; // Capable to support date-time from 1970 + auto-triggers in some RDBMS
49
+ const TYPE_DATETIME = 'datetime'; // Capable to support long date-time before 1970
50
+ const TYPE_TEXT = 'text';
51
+ const TYPE_BLOB = 'blob'; // Used for back compatibility, when query param can't use statement options
52
+ const TYPE_VARBINARY = 'varbinary'; // A real blob, stored as binary inside DB
53
+
54
+ // Deprecated column types, support is left only in MySQL adapter.
55
+ const TYPE_TINYINT = 'tinyint'; // Internally converted to TYPE_SMALLINT
56
+ const TYPE_CHAR = 'char'; // Internally converted to TYPE_TEXT
57
+ const TYPE_VARCHAR = 'varchar'; // Internally converted to TYPE_TEXT
58
+ const TYPE_LONGVARCHAR = 'longvarchar'; // Internally converted to TYPE_TEXT
59
+ const TYPE_CLOB = 'cblob'; // Internally converted to TYPE_TEXT
60
+ const TYPE_DOUBLE = 'double'; // Internally converted to TYPE_FLOAT
61
+ const TYPE_REAL = 'real'; // Internally converted to TYPE_FLOAT
62
+ const TYPE_TIME = 'time'; // Internally converted to TYPE_TIMESTAMP
63
+ const TYPE_BINARY = 'binary'; // Internally converted to TYPE_BLOB
64
+ const TYPE_LONGVARBINARY = 'longvarbinary'; // Internally converted to TYPE_BLOB
65
+
66
+ /**
67
+ * Default and maximal TEXT and BLOB columns sizes we can support for different DB systems.
68
+ */
69
+ const DEFAULT_TEXT_SIZE = 1024;
70
+ const MAX_TEXT_SIZE = 2147483648;
71
+ const MAX_VARBINARY_SIZE = 2147483648;
72
+
73
+ /**
74
+ * Default values for timestampses - fill with current timestamp on inserting record, on changing and both cases
75
+ */
76
+ const TIMESTAMP_INIT_UPDATE = 'TIMESTAMP_INIT_UPDATE';
77
+ const TIMESTAMP_INIT = 'TIMESTAMP_INIT';
78
+ const TIMESTAMP_UPDATE = 'TIMESTAMP_UPDATE';
79
+
80
+ /**
81
+ * Actions used for foreign keys
82
+ */
83
+ const ACTION_CASCADE = 'CASCADE';
84
+ const ACTION_SET_NULL = 'SET NULL';
85
+ const ACTION_NO_ACTION = 'NO ACTION';
86
+ const ACTION_RESTRICT = 'RESTRICT';
87
+ const ACTION_SET_DEFAULT = 'SET DEFAULT';
88
+
89
+ /**
90
+ * Name of table
91
+ *
92
+ * @var string
93
+ */
94
+ protected $_tableName;
95
+
96
+ /**
97
+ * Schema name
98
+ *
99
+ * @var string
100
+ */
101
+ protected $_schemaName;
102
+
103
+ /**
104
+ * Comment for Table
105
+ *
106
+ * @var string
107
+ */
108
+ protected $_tableComment;
109
+
110
+ /**
111
+ * Column descriptions for a table
112
+ *
113
+ * Is an associative array keyed by the uppercase column name
114
+ * The value of each array element is an associative array
115
+ * with the following keys:
116
+ *
117
+ * COLUMN_NAME => string; column name
118
+ * COLUMN_POSITION => number; ordinal position of column in table
119
+ * DATA_TYPE => string; constant datatype of column
120
+ * DEFAULT => string; default expression of column, null if none
121
+ * NULLABLE => boolean; true if column can have nulls
122
+ * LENGTH => number; length of CHAR/VARCHAR/INT
123
+ * SCALE => number; scale of NUMERIC/DECIMAL
124
+ * PRECISION => number; precision of NUMERIC/DECIMAL
125
+ * UNSIGNED => boolean; unsigned property of an integer type
126
+ * PRIMARY => boolean; true if column is part of the primary key
127
+ * PRIMARY_POSITION => integer; position of column in primary key
128
+ * IDENTITY => integer; true if column is auto-generated with unique values
129
+ * COMMENT => string; column description
130
+ *
131
+ * @var array
132
+ */
133
+ protected $_columns = array();
134
+
135
+ /**
136
+ * Index descriptions for a table
137
+ *
138
+ * Is an associative array keyed by the uppercase index name
139
+ * The value of each array element is an associative array
140
+ * with the following keys:
141
+ *
142
+ * INDEX_NAME => string; index name
143
+ * COLUMNS => array; array of index columns
144
+ * TYPE => string; Optional special index type
145
+ *
146
+ * COLUMNS is an associative array keyed by the uppercase column name
147
+ * The value of each COLUMNS array element is an associative array
148
+ * with the following keys:
149
+ *
150
+ * NAME => string; The column name
151
+ * SIZE => int|null; Length of index column (always null if index is unique)
152
+ * POSITION => int; Position in index
153
+ *
154
+ * @var array
155
+ */
156
+ protected $_indexes = array();
157
+
158
+ /**
159
+ * Foreign key descriptions for a table
160
+ *
161
+ * Is an associative array keyed by the uppercase foreign key name
162
+ * The value of each array element is an associative array
163
+ * with the following keys:
164
+ *
165
+ * FK_NAME => string; The foreign key name
166
+ * COLUMN_NAME => string; The column name in table
167
+ * REF_TABLE_NAME => string; Reference table name
168
+ * REF_COLUMN_NAME => string; Reference table column name
169
+ * ON_DELETE => string; Integrity Actions, default NO ACTION
170
+ * ON_UPDATE => string; Integrity Actions, default NO ACTION
171
+ *
172
+ * Valid Integrity Actions:
173
+ * CASCADE | SET NULL | NONE | RESTRICT | SET DEFAULT
174
+ *
175
+ * @var array
176
+ */
177
+ protected $_foreignKeys = array();
178
+
179
+ /**
180
+ * Additional table options
181
+ *
182
+ * @var array
183
+ */
184
+ protected $_options = array(
185
+ 'type' => 'INNODB',
186
+ 'charset' => 'utf8',
187
+ 'collate' => 'utf8_general_ci',
188
+
189
+ );
190
+
191
+ /**
192
+ * Set table name
193
+ *
194
+ * @param string $name
195
+ * @return Varien_Db_Ddl_Table
196
+ */
197
+ public function setName($name)
198
+ {
199
+ $this->_tableName = $name;
200
+ if ($this->_tableComment === null) {
201
+ $this->_tableComment = $name;
202
+ }
203
+ return $this;
204
+ }
205
+
206
+ /**
207
+ * Set schema name
208
+ *
209
+ * @param string $name
210
+ * @return Varien_Db_Ddl_Table
211
+ */
212
+ public function setSchema($name)
213
+ {
214
+ $this->_schemaName = $name;
215
+ return $this;
216
+ }
217
+
218
+ /**
219
+ * Set comment for table
220
+ *
221
+ * @param string $comment
222
+ */
223
+ public function setComment($comment)
224
+ {
225
+ $this->_tableComment = $comment;
226
+ return $this;
227
+ }
228
+
229
+ /**
230
+ * Retrieve name of table
231
+ *
232
+ * @throws Zend_Db_Exception
233
+ * @return string
234
+ */
235
+ public function getName()
236
+ {
237
+ if (is_null($this->_tableName)) {
238
+ throw new Zend_Db_Exception('Table name is not defined');
239
+ }
240
+ return $this->_tableName;
241
+ }
242
+
243
+ /**
244
+ * Get schema name
245
+ *
246
+ * @return string|null
247
+ */
248
+ public function getSchema()
249
+ {
250
+ return $this->_schemaName;
251
+ }
252
+
253
+ /**
254
+ * Return comment for table
255
+ *
256
+ * @return string
257
+ */
258
+ public function getComment()
259
+ {
260
+ return $this->_tableComment;
261
+ }
262
+
263
+ /**
264
+ * Adds column to table.
265
+ *
266
+ * $options contains additional options for columns. Supported values are:
267
+ * - 'unsigned', for number types only. Default: FALSE.
268
+ * - 'precision', for numeric and decimal only. Default: taken from $size, if not set there then 0.
269
+ * - 'scale', for numeric and decimal only. Default: taken from $size, if not set there then 10.
270
+ * - 'default'. Default: not set.
271
+ * - 'nullable'. Default: TRUE.
272
+ * - 'primary', add column to primary index. Default: do not add.
273
+ * - 'primary_position', only for column in primary index. Default: count of primary columns + 1.
274
+ * - 'identity' or 'auto_increment'. Default: FALSE.
275
+ *
276
+ * @param string $name the column name
277
+ * @param string $type the column data type
278
+ * @param string|int|array $size the column length
279
+ * @param array $options array of additional options
280
+ * @param string $comment column description
281
+ * @throws Zend_Db_Exception
282
+ * @return Varien_Db_Ddl_Table
283
+ */
284
+ public function addColumn($name, $type, $size = null, $options = array(), $comment = null)
285
+ {
286
+ $position = count($this->_columns);
287
+ $default = false;
288
+ $nullable = true;
289
+ $length = null;
290
+ $scale = null;
291
+ $precision = null;
292
+ $unsigned = false;
293
+ $primary = false;
294
+ $primaryPosition = 0;
295
+ $identity = false;
296
+
297
+ // Convert deprecated types
298
+ switch ($type) {
299
+ case self::TYPE_CHAR:
300
+ case self::TYPE_VARCHAR:
301
+ case self::TYPE_LONGVARCHAR:
302
+ case self::TYPE_CLOB:
303
+ $type = self::TYPE_TEXT;
304
+ break;
305
+ case self::TYPE_TINYINT:
306
+ $type = self::TYPE_SMALLINT;
307
+ break;
308
+ case self::TYPE_DOUBLE:
309
+ case self::TYPE_REAL:
310
+ $type = self::TYPE_FLOAT;
311
+ break;
312
+ case self::TYPE_TIME:
313
+ $type = self::TYPE_TIMESTAMP;
314
+ break;
315
+ case self::TYPE_BINARY:
316
+ case self::TYPE_LONGVARBINARY:
317
+ $type = self::TYPE_BLOB;
318
+ break;
319
+ }
320
+
321
+ // Prepare different properties
322
+ switch ($type) {
323
+ case self::TYPE_BOOLEAN:
324
+ break;
325
+
326
+ case self::TYPE_SMALLINT:
327
+ case self::TYPE_INTEGER:
328
+ case self::TYPE_BIGINT:
329
+ if (!empty($options['unsigned'])) {
330
+ $unsigned = true;
331
+ }
332
+
333
+ break;
334
+
335
+ case self::TYPE_FLOAT:
336
+ if (!empty($options['unsigned'])) {
337
+ $unsigned = true;
338
+ }
339
+ break;
340
+
341
+ case self::TYPE_DECIMAL:
342
+ case self::TYPE_NUMERIC:
343
+ $match = array();
344
+ $scale = 10;
345
+ $precision = 0;
346
+ // parse size value
347
+ if (is_array($size)) {
348
+ if (count($size) == 2) {
349
+ $size = array_values($size);
350
+ $precision = $size[0];
351
+ $scale = $size[1];
352
+ }
353
+ } else if (preg_match('#^(\d+),(\d+)$#', $size, $match)) {
354
+ $precision = $match[1];
355
+ $scale = $match[2];
356
+ }
357
+ // check options
358
+ if (isset($options['precision'])) {
359
+ $precision = $options['precision'];
360
+ }
361
+
362
+ if (isset($options['scale'])) {
363
+ $scale = $options['scale'];
364
+ }
365
+
366
+ if (!empty($options['unsigned'])) {
367
+ $unsigned = true;
368
+ }
369
+ break;
370
+ case self::TYPE_DATE:
371
+ case self::TYPE_DATETIME:
372
+ case self::TYPE_TIMESTAMP:
373
+ break;
374
+ case self::TYPE_TEXT:
375
+ case self::TYPE_BLOB:
376
+ case self::TYPE_VARBINARY:
377
+ $length = $size;
378
+ break;
379
+ default:
380
+ throw new Zend_Db_Exception('Invalid column data type "' . $type . '"');
381
+ }
382
+
383
+ if (array_key_exists('default', $options)) {
384
+ $default = $options['default'];
385
+ }
386
+ if (array_key_exists('nullable', $options)) {
387
+ $nullable = (bool)$options['nullable'];
388
+ }
389
+ if (!empty($options['primary'])) {
390
+ $primary = true;
391
+ if (isset($options['primary_position'])) {
392
+ $primaryPosition = (int)$options['primary_position'];
393
+ } else {
394
+ $primaryPosition = 0;
395
+ foreach ($this->_columns as $v) {
396
+ if ($v['PRIMARY']) {
397
+ $primaryPosition ++;
398
+ }
399
+ }
400
+ }
401
+ }
402
+ if (!empty($options['identity']) || !empty($options['auto_increment'])) {
403
+ $identity = true;
404
+ }
405
+
406
+ if ($comment === null) {
407
+ $comment = ucfirst($name);
408
+ }
409
+
410
+ $upperName = strtoupper($name);
411
+ $this->_columns[$upperName] = array(
412
+ 'COLUMN_NAME' => $name,
413
+ 'COLUMN_TYPE' => $type,
414
+ 'COLUMN_POSITION' => $position,
415
+ 'DATA_TYPE' => $type,
416
+ 'DEFAULT' => $default,
417
+ 'NULLABLE' => $nullable,
418
+ 'LENGTH' => $length,
419
+ 'SCALE' => $scale,
420
+ 'PRECISION' => $precision,
421
+ 'UNSIGNED' => $unsigned,
422
+ 'PRIMARY' => $primary,
423
+ 'PRIMARY_POSITION' => $primaryPosition,
424
+ 'IDENTITY' => $identity,
425
+ 'COMMENT' => $comment
426
+ );
427
+
428
+ return $this;
429
+ }
430
+
431
+ /**
432
+ * Add Foreign Key to table
433
+ *
434
+ * @param string $fkName the foreign key name
435
+ * @param string $column the foreign key column name
436
+ * @param string $refTable the reference table name
437
+ * @param string $refColumn the reference table column name
438
+ * @param string $onDelete the action on delete row
439
+ * @param string $onUpdate the action on update
440
+ * @throws Zend_Db_Exception
441
+ * @return Varien_Db_Ddl_Table
442
+ */
443
+ public function addForeignKey($fkName, $column, $refTable, $refColumn, $onDelete = null, $onUpdate = null)
444
+ {
445
+ $upperName = strtoupper($fkName);
446
+
447
+ // validate column name
448
+ if (!isset($this->_columns[strtoupper($column)])) {
449
+ throw new Zend_Db_Exception('Undefined column "' . $column . '"');
450
+ }
451
+
452
+ switch ($onDelete) {
453
+ case self::ACTION_CASCADE:
454
+ case self::ACTION_RESTRICT:
455
+ case self::ACTION_SET_DEFAULT:
456
+ case self::ACTION_SET_NULL:
457
+ break;
458
+ default:
459
+ $onDelete = self::ACTION_NO_ACTION;
460
+ }
461
+
462
+ switch ($onUpdate) {
463
+ case self::ACTION_CASCADE:
464
+ case self::ACTION_RESTRICT:
465
+ case self::ACTION_SET_DEFAULT:
466
+ case self::ACTION_SET_NULL:
467
+ break;
468
+ default:
469
+ $onUpdate = self::ACTION_NO_ACTION;
470
+ }
471
+
472
+ $this->_foreignKeys[$upperName] = array(
473
+ 'FK_NAME' => $fkName,
474
+ 'COLUMN_NAME' => $column,
475
+ 'REF_TABLE_NAME' => $refTable,
476
+ 'REF_COLUMN_NAME' => $refColumn,
477
+ 'ON_DELETE' => $onDelete,
478
+ 'ON_UPDATE' => $onUpdate
479
+ );
480
+
481
+ return $this;
482
+ }
483
+
484
+ /**
485
+ * Add index to table
486
+ *
487
+ * @param string $indexName the index name
488
+ * @param array|string $columns array of columns or column string
489
+ * @param array $options array of additional options
490
+ * @return Varien_Db_Ddl_Table
491
+ */
492
+ public function addIndex($indexName, $fields, $options = array())
493
+ {
494
+ $idxType = Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX;
495
+ $position = 0;
496
+ $columns = array();
497
+ if (!is_array($fields)) {
498
+ $fields = array($fields);
499
+ }
500
+
501
+ foreach ($fields as $columnData) {
502
+ $columnSize = null;
503
+ $columnPos = $position;
504
+ if (is_string($columnData)) {
505
+ $columnName = $columnData;
506
+ } else if (is_array($columnData)) {
507
+ if (!isset($columnData['name'])) {
508
+ throw new Zend_Db_Exception('Invalid index column data');
509
+ }
510
+
511
+ $columnName = $columnData['name'];
512
+ if (!empty($columnData['size'])) {
513
+ $columnSize = (int)$columnData['size'];
514
+ }
515
+ if (!empty($columnData['position'])) {
516
+ $columnPos = (int)$columnData['position'];
517
+ }
518
+ } else {
519
+ continue;
520
+ }
521
+
522
+ $columns[strtoupper($columnName)] = array(
523
+ 'NAME' => $columnName,
524
+ 'SIZE' => $columnSize,
525
+ 'POSITION' => $columnPos
526
+ );
527
+
528
+ $position ++;
529
+ }
530
+
531
+ if (empty($columns)) {
532
+ throw new Zend_Db_Exception('Columns for index are not defined');
533
+ }
534
+
535
+ if (!empty($options['type'])) {
536
+ $idxType = $options['type'];
537
+ }
538
+
539
+ $this->_indexes[strtoupper($indexName)] = array(
540
+ 'INDEX_NAME' => $indexName,
541
+ 'COLUMNS' => $this->_normalizeIndexColumnPosition($columns),
542
+ 'TYPE' => $idxType
543
+ );
544
+
545
+ return $this;
546
+ }
547
+
548
+ /**
549
+ * Retrieve array of table columns
550
+ *
551
+ * @param bool $normalized
552
+ * @see $this->_columns
553
+ * @return array
554
+ */
555
+ public function getColumns($normalized = true)
556
+ {
557
+ if ($normalized) {
558
+ return $this->_normalizeColumnPosition($this->_columns);
559
+ }
560
+ return $this->_columns;
561
+ }
562
+
563
+ /**
564
+ * Set column, formatted according to DDL Table format, into columns structure
565
+ *
566
+ * @param array $column
567
+ * @see $this->_columns
568
+ * @return Varien_Db_Ddl_Table
569
+ */
570
+ public function setColumn($column)
571
+ {
572
+ $upperName = strtoupper($column['COLUMN_NAME']);
573
+ $this->_columns[$upperName] = $column;
574
+ return $this;
575
+ }
576
+
577
+ /**
578
+ * Retrieve array of table indexes
579
+ *
580
+ * @see $this->_indexes
581
+ * @return array
582
+ */
583
+ public function getIndexes()
584
+ {
585
+ return $this->_indexes;
586
+ }
587
+
588
+ /**
589
+ * Retrieve array of table foreign keys
590
+ *
591
+ * @see $this->_foreignKeys
592
+ * @return array
593
+ */
594
+ public function getForeignKeys()
595
+ {
596
+ return $this->_foreignKeys;
597
+ }
598
+
599
+ /**
600
+ * Set table option
601
+ *
602
+ * @param string $key
603
+ * @param string $value
604
+ * @return string
605
+ */
606
+ public function setOption($key, $value)
607
+ {
608
+ $this->_options[$key] = $value;
609
+ return $this;
610
+ }
611
+
612
+ /**
613
+ * Retrieve table option value by option name
614
+ * Return null if option does not exits
615
+ *
616
+ * @param string $key
617
+ * @return mixed
618
+ */
619
+ public function getOption($key)
620
+ {
621
+ if (!isset($this->_options[$key])) {
622
+ return null;
623
+ }
624
+ return $this->_options[$key];
625
+ }
626
+
627
+ /**
628
+ * Retrieve array of table options
629
+ *
630
+ * @return array
631
+ */
632
+ public function getOptions()
633
+ {
634
+ return $this->_options;
635
+ }
636
+
637
+ /**
638
+ * Index column position comparison function
639
+ *
640
+ * @param array $a
641
+ * @param array $b
642
+ * @return int
643
+ */
644
+ protected function _sortIndexColumnPosition($a, $b)
645
+ {
646
+ return $a['POSITION'] - $b['POSITION'];
647
+ }
648
+
649
+ /**
650
+ * table column position comparison function
651
+ *
652
+ * @param array $a
653
+ * @param array $b
654
+ * @return int
655
+ */
656
+ protected function _sortColumnPosition($a, $b)
657
+ {
658
+ return $a['COLUMN_POSITION'] - $b['COLUMN_POSITION'];
659
+ }
660
+
661
+ /**
662
+ * Normalize positon of index columns array
663
+ *
664
+ * @param array $columns
665
+ * @return array
666
+ */
667
+ protected function _normalizeIndexColumnPosition($columns)
668
+ {
669
+ uasort($columns, array($this, '_sortIndexColumnPosition'));
670
+ $position = 0;
671
+ foreach (array_keys($columns) as $columnId) {
672
+ $columns[$columnId]['POSITION'] = $position;
673
+ $position ++;
674
+ }
675
+ return $columns;
676
+ }
677
+
678
+ /**
679
+ * Normalize positon of table columns array
680
+ *
681
+ * @param array $columns
682
+ * @return array
683
+ */
684
+ protected function _normalizeColumnPosition($columns)
685
+ {
686
+ uasort($columns, array($this, '_sortColumnPosition'));
687
+ $position = 0;
688
+ foreach (array_keys($columns) as $columnId) {
689
+ $columns[$columnId]['COLUMN_POSITION'] = $position;
690
+ $position ++;
691
+ }
692
+ return $columns;
693
+ }
694
+ }
lib/Varien/Db/Exception.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ require_once 'Varien/Exception.php';
28
+ class Varien_Db_Exception extends Varien_Exception
29
+ {}
lib/Varien/Db/Helper.php ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2010 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Varien Db Helper
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Db
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Db_Helper
36
+ {
37
+ /**
38
+ * Dictionary for generate short name
39
+ *
40
+ * @var array
41
+ */
42
+ protected static $_translateMap = array(
43
+ 'address' => 'addr',
44
+ 'admin' => 'adm',
45
+ 'attribute' => 'attr',
46
+ 'enterprise' => 'ent',
47
+ 'catalog' => 'cat',
48
+ 'category' => 'ctgr',
49
+ 'customer' => 'cstr',
50
+ 'notification' => 'ntfc',
51
+ 'product' => 'prd',
52
+ 'session' => 'sess',
53
+ 'user' => 'usr',
54
+ 'entity' => 'entt',
55
+ 'datetime' => 'dtime',
56
+ 'decimal' => 'dec',
57
+ 'varchar' => 'vchr',
58
+ 'index' => 'idx',
59
+ 'compare' => 'cmp',
60
+ 'bundle' => 'bndl',
61
+ 'option' => 'opt',
62
+ 'gallery' => 'glr',
63
+ 'media' => 'mda',
64
+ 'value' => 'val',
65
+ 'link' => 'lnk',
66
+ 'title' => 'ttl',
67
+ 'super' => 'spr',
68
+ 'label' => 'lbl',
69
+ 'website' => 'ws',
70
+ 'aggregat' => 'aggr',
71
+ 'minimal' => 'min',
72
+ 'inventory' => 'inv',
73
+ 'status' => 'sts',
74
+ 'agreement' => 'agrt',
75
+ 'layout' => 'lyt',
76
+ 'resource' => 'res',
77
+ 'directory' => 'dir',
78
+ 'downloadable' => 'dl',
79
+ 'element' => 'elm',
80
+ 'fieldset' => 'fset',
81
+ 'checkout' => 'chkt',
82
+ 'newsletter' => 'nlttr',
83
+ 'shipping' => 'shpp',
84
+ 'calculation' => 'calc',
85
+ 'search' => 'srch',
86
+ 'query' => 'qr'
87
+ );
88
+
89
+ /**
90
+ * Convert name using dictionary
91
+ *
92
+ * @param string $name
93
+ * @return string
94
+ */
95
+ public static function shortName($name)
96
+ {
97
+ return strtr($name, self::$_translateMap);
98
+ }
99
+
100
+ /**
101
+ * Add or replace translate to dictionary
102
+ *
103
+ * @param string $from
104
+ * @param string $to
105
+ */
106
+ public static function addTranslate($from, $to)
107
+ {
108
+ self::$_translateMap[$from] = $to;
109
+ }
110
+ }
lib/Varien/Db/Select.php ADDED
@@ -0,0 +1,474 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Class for SQL SELECT generation and results.
29
+ *
30
+ * @method Varien_Db_Adapter_Interface|Zend_Db_Adapter_Abstract getAdapter()
31
+ * @property Varien_Db_Adapter_Interface|Zend_Db_Adapter_Abstract $_adapter
32
+ * @method Varien_Db_Select from($name, $cols, $schema = null)
33
+ * @method Varien_Db_Select join($name, $cond, $cols, $schema = null)
34
+ * @method Varien_Db_Select joinInner($name, $cond, $cols, $schema = null)
35
+ * @method Varien_Db_Select joinLeft($name, $cond, $cols = '*', $schema = null)
36
+ * @method Varien_Db_Select joinNatural($name, $cond, $cols, $schema = null)
37
+ * @method Varien_Db_Select joinFull($name, $cond, $cols, $schema = null)
38
+ * @method Varien_Db_Select joinRight($name, $cond, $cols, $schema = null)
39
+ * @method Varien_Db_Select joinCross($name, $cols, $schema = null)
40
+ * @method Varien_Db_Select orWhere($cond, $value = null, $type = null)
41
+ * @method Varien_Db_Select group($spec)
42
+ * @method Varien_Db_Select order($spec)
43
+ * @method Varien_Db_Select limitPage($page, $rowCount)
44
+ * @method Varien_Db_Select forUpdate($flag = true)
45
+ * @method Varien_Db_Select distinct($flag = true)
46
+ * @method Varien_Db_Select reset($part = null)
47
+ * @method Varien_Db_Select columns($cols, $correlationName = null)
48
+ *
49
+ * @category Varien
50
+ * @package Varien_Db
51
+ * @author Magento Core Team <core@magentocommerce.com>
52
+ */
53
+ class Varien_Db_Select extends Zend_Db_Select
54
+ {
55
+ const TYPE_CONDITION = 'TYPE_CONDITION';
56
+
57
+ const STRAIGHT_JOIN = 'straightjoin';
58
+
59
+ const SQL_STRAIGHT_JOIN = 'STRAIGHT_JOIN';
60
+
61
+ /**
62
+ * Class constructor
63
+ * Add straight join support
64
+ *
65
+ * @param Zend_Db_Adapter_Abstract $adapter
66
+ */
67
+ public function __construct(Zend_Db_Adapter_Abstract $adapter)
68
+ {
69
+ if (!isset(self::$_partsInit[self::STRAIGHT_JOIN])) {
70
+ self::$_partsInit = array(self::STRAIGHT_JOIN => false) + self::$_partsInit;
71
+ }
72
+
73
+ parent::__construct($adapter);
74
+ }
75
+
76
+ /**
77
+ * Adds a WHERE condition to the query by AND.
78
+ *
79
+ * If a value is passed as the second param, it will be quoted
80
+ * and replaced into the condition wherever a question-mark
81
+ * appears. Array values are quoted and comma-separated.
82
+ *
83
+ * <code>
84
+ * // simplest but non-secure
85
+ * $select->where("id = $id");
86
+ *
87
+ * // secure (ID is quoted but matched anyway)
88
+ * $select->where('id = ?', $id);
89
+ *
90
+ * // alternatively, with named binding
91
+ * $select->where('id = :id');
92
+ * </code>
93
+ *
94
+ * Note that it is more correct to use named bindings in your
95
+ * queries for values other than strings. When you use named
96
+ * bindings, don't forget to pass the values when actually
97
+ * making a query:
98
+ *
99
+ * <code>
100
+ * $db->fetchAll($select, array('id' => 5));
101
+ * </code>
102
+ *
103
+ * @param string $cond The WHERE condition.
104
+ * @param string $value OPTIONAL A single value to quote into the condition.
105
+ * @param constant $type OPTIONAL The type of the given value
106
+ * @return Varien_Db_Select This Zend_Db_Select object.
107
+ */
108
+ public function where($cond, $value = null, $type = null)
109
+ {
110
+ if (is_null($value) && is_null($type)) {
111
+ $value = '';
112
+ }
113
+ /**
114
+ * Additional internal type used for really null value
115
+ */
116
+ if ($type == self::TYPE_CONDITION) {
117
+ $type = null;
118
+ }
119
+ if (is_array($value)) {
120
+ $cond = $this->_adapter->quoteInto($cond, $value);
121
+ $value = null;
122
+ }
123
+ return parent::where($cond, $value, $type);
124
+ }
125
+
126
+ /**
127
+ * Reset unused LEFT JOIN(s)
128
+ *
129
+ * @return Varien_Db_Select
130
+ */
131
+ public function resetJoinLeft()
132
+ {
133
+ foreach ($this->_parts[self::FROM] as $tableId => $tableProp) {
134
+ if ($tableProp['joinType'] == self::LEFT_JOIN) {
135
+ $useJoin = false;
136
+ foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
137
+ list($correlationName, $column) = $columnEntry;
138
+ if ($column instanceof Zend_Db_Expr) {
139
+ if ($this->_findTableInCond($tableId, $column)
140
+ || $this->_findTableInCond($tableProp['tableName'], $column)) {
141
+ $useJoin = true;
142
+ }
143
+ } else {
144
+ if ($correlationName == $tableId) {
145
+ $useJoin = true;
146
+ }
147
+ }
148
+ }
149
+ foreach ($this->_parts[self::WHERE] as $where) {
150
+ if ($this->_findTableInCond($tableId, $where)
151
+ || $this->_findTableInCond($tableProp['tableName'], $where)) {
152
+ $useJoin = true;
153
+ }
154
+ }
155
+
156
+ $joinUseInCond = $useJoin;
157
+ $joinInTables = array();
158
+
159
+ foreach ($this->_parts[self::FROM] as $tableCorrelationName => $table) {
160
+ if ($tableCorrelationName == $tableId) {
161
+ continue;
162
+ }
163
+ if (!empty($table['joinCondition'])) {
164
+ if ($this->_findTableInCond($tableId, $table['joinCondition'])
165
+ || $this->_findTableInCond($tableProp['tableName'], $table['joinCondition'])) {
166
+ $useJoin = true;
167
+ $joinInTables[] = $tableCorrelationName;
168
+ }
169
+ }
170
+ }
171
+
172
+ if (!$useJoin) {
173
+ unset($this->_parts[self::FROM][$tableId]);
174
+ } else {
175
+ $this->_parts[self::FROM][$tableId]['useInCond'] = $joinUseInCond;
176
+ $this->_parts[self::FROM][$tableId]['joinInTables'] = $joinInTables;
177
+ }
178
+ }
179
+ }
180
+
181
+ $this->_resetJoinLeft();
182
+
183
+ return $this;
184
+ }
185
+
186
+ /**
187
+ * Validate LEFT joins, and remove it if not exists
188
+ *
189
+ * @return Varien_Db_Select
190
+ */
191
+ protected function _resetJoinLeft()
192
+ {
193
+ foreach ($this->_parts[self::FROM] as $tableId => $tableProp) {
194
+ if ($tableProp['joinType'] == self::LEFT_JOIN) {
195
+ if ($tableProp['useInCond']) {
196
+ continue;
197
+ }
198
+
199
+ $used = false;
200
+ foreach ($tableProp['joinInTables'] as $table) {
201
+ if (isset($this->_parts[self::FROM][$table])) {
202
+ $used = true;
203
+ break;
204
+ }
205
+ }
206
+
207
+ if (!$used) {
208
+ unset($this->_parts[self::FROM][$tableId]);
209
+ return $this->_resetJoinLeft();
210
+ }
211
+ }
212
+ }
213
+
214
+ return $this;
215
+ }
216
+
217
+ /**
218
+ * Find table name in condition (where, column)
219
+ *
220
+ * @param string $table
221
+ * @param string $cond
222
+ * @return bool
223
+ */
224
+ protected function _findTableInCond($table, $cond)
225
+ {
226
+ $quote = $this->_adapter->getQuoteIdentifierSymbol();
227
+
228
+ if (strpos($cond, $quote . $table . $quote . '.') !== false) {
229
+ return true;
230
+ }
231
+
232
+ $position = 0;
233
+ $result = 0;
234
+ $needle = array();
235
+ while (is_integer($result)) {
236
+ $result = strpos($cond, $table . '.', $position);
237
+
238
+ if (is_integer($result)) {
239
+ $needle[] = $result;
240
+ $position = ($result + strlen($table) + 1);
241
+ }
242
+ }
243
+
244
+ if (!$needle) {
245
+ return false;
246
+ }
247
+
248
+ foreach ($needle as $position) {
249
+ if ($position == 0) {
250
+ return true;
251
+ }
252
+ if (!preg_match('#[a-z0-9_]#is', substr($cond, $position - 1, 1))) {
253
+ return true;
254
+ }
255
+ }
256
+
257
+ return false;
258
+ }
259
+
260
+ /**
261
+ * Populate the {@link $_parts} 'join' key
262
+ *
263
+ * Does the dirty work of populating the join key.
264
+ *
265
+ * The $name and $cols parameters follow the same logic
266
+ * as described in the from() method.
267
+ *
268
+ * @param null|string $type Type of join; inner, left, and null are currently supported
269
+ * @param array|string|Zend_Db_Expr $name Table name
270
+ * @param string $cond Join on this condition
271
+ * @param array|string $cols The columns to select from the joined table
272
+ * @param string $schema The database name to specify, if any.
273
+ * @return Zend_Db_Select This Zend_Db_Select object
274
+ * @throws Zend_Db_Select_Exception
275
+ */
276
+ protected function _join($type, $name, $cond, $cols, $schema = null)
277
+ {
278
+ if ($type == self::INNER_JOIN && empty($cond)) {
279
+ $type = self::CROSS_JOIN;
280
+ }
281
+ return parent::_join($type, $name, $cond, $cols, $schema);
282
+ }
283
+
284
+ /**
285
+ * Sets a limit count and offset to the query.
286
+ *
287
+ * @param int $count OPTIONAL The number of rows to return.
288
+ * @param int $offset OPTIONAL Start returning after this many rows.
289
+ * @return Zend_Db_Select This Zend_Db_Select object.
290
+ */
291
+ public function limit($count = null, $offset = null)
292
+ {
293
+ if ($count === null) {
294
+ $this->reset(self::LIMIT_COUNT);
295
+ } else {
296
+ $this->_parts[self::LIMIT_COUNT] = (int) $count;
297
+ }
298
+ if ($offset === null) {
299
+ $this->reset(self::LIMIT_OFFSET);
300
+ } else {
301
+ $this->_parts[self::LIMIT_OFFSET] = (int) $offset;
302
+ }
303
+ return $this;
304
+ }
305
+
306
+ /**
307
+ * Cross Table Update From Current select
308
+ *
309
+ * @param string|array $table
310
+ * @return string
311
+ */
312
+ public function crossUpdateFromSelect($table)
313
+ {
314
+ return $this->getAdapter()->updateFromSelect($this, $table);
315
+ }
316
+
317
+ /**
318
+ * Insert to table from current select
319
+ *
320
+ * @param string $tableName
321
+ * @param array $fields
322
+ * @param bool $onDuplicate
323
+ * @return string
324
+ */
325
+ public function insertFromSelect($tableName, $fields = array(), $onDuplicate = true)
326
+ {
327
+ $mode = $onDuplicate ? Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE : false;
328
+ return $this->getAdapter()->insertFromSelect($this, $tableName, $fields, $mode);
329
+ }
330
+
331
+ /**
332
+ * Generate INSERT IGNORE query to the table from current select
333
+ *
334
+ * @param string $tableName
335
+ * @param array $fields
336
+ * @return string
337
+ */
338
+ public function insertIgnoreFromSelect($tableName, $fields = array())
339
+ {
340
+ return $this->getAdapter()
341
+ ->insertFromSelect($this, $tableName, $fields, Varien_Db_Adapter_Interface::INSERT_IGNORE);
342
+ }
343
+
344
+ /**
345
+ * Retrieve DELETE query from select
346
+ *
347
+ * @param string $table The table name or alias
348
+ * @return string
349
+ */
350
+ public function deleteFromSelect($table)
351
+ {
352
+ return $this->getAdapter()->deleteFromSelect($this, $table);
353
+ }
354
+
355
+ /**
356
+ * Modify (hack) part of the structured information for the currect query
357
+ *
358
+ * @param string $part
359
+ * @param mixed $value
360
+ * @return Varien_Db_Select
361
+ * @throws Zend_Db_Select_Exception
362
+ */
363
+ public function setPart($part, $value)
364
+ {
365
+ $part = strtolower($part);
366
+ if (!array_key_exists($part, $this->_parts)) {
367
+ throw new Zend_Db_Select_Exception("Invalid Select part '{$part}'");
368
+ }
369
+ $this->_parts[$part] = $value;
370
+ return $this;
371
+ }
372
+
373
+ /**
374
+ * Use a STRAIGHT_JOIN for the SQL Select
375
+ *
376
+ * @param bool $flag Whether or not the SELECT use STRAIGHT_JOIN (default true).
377
+ * @return Zend_Db_Select This Zend_Db_Select object.
378
+ */
379
+ public function useStraightJoin($flag = true)
380
+ {
381
+ $this->_parts[self::STRAIGHT_JOIN] = (bool) $flag;
382
+ return $this;
383
+ }
384
+
385
+ /**
386
+ * Render STRAIGHT_JOIN clause
387
+ *
388
+ * @param string $sql SQL query
389
+ * @return string
390
+ */
391
+ protected function _renderStraightjoin($sql)
392
+ {
393
+ if ($this->_adapter->supportStraightJoin() && !empty($this->_parts[self::STRAIGHT_JOIN])) {
394
+ $sql .= ' ' . self::SQL_STRAIGHT_JOIN;
395
+ }
396
+
397
+ return $sql;
398
+ }
399
+
400
+ /**
401
+ * Adds to the internal table-to-column mapping array.
402
+ *
403
+ * @param string $tbl The table/join the columns come from.
404
+ * @param array|string $cols The list of columns; preferably as
405
+ * an array, but possibly as a string containing one column.
406
+ * @param bool|string True if it should be prepended, a correlation name if it should be inserted
407
+ * @return void
408
+ */
409
+ protected function _tableCols($correlationName, $cols, $afterCorrelationName = null)
410
+ {
411
+ if (!is_array($cols)) {
412
+ $cols = array($cols);
413
+ }
414
+
415
+ foreach ($cols as $k => $v) {
416
+ if ($v instanceof Varien_Db_Select) {
417
+ $cols[$k] = new Zend_Db_Expr(sprintf('(%s)', $v->assemble()));
418
+ }
419
+ }
420
+
421
+ return parent::_tableCols($correlationName, $cols, $afterCorrelationName);
422
+ }
423
+
424
+ /**
425
+ * Adds the random order to query
426
+ *
427
+ * @param string $field integer field name
428
+ * @return Varien_Db_Select
429
+ */
430
+ public function orderRand($field = null)
431
+ {
432
+ $this->_adapter->orderRand($this, $field);
433
+ return $this;
434
+ }
435
+
436
+ /**
437
+ * Render FOR UPDATE clause
438
+ *
439
+ * @param string $sql SQL query
440
+ * @return string
441
+ */
442
+ protected function _renderForupdate($sql)
443
+ {
444
+ if ($this->_parts[self::FOR_UPDATE]) {
445
+ $sql = $this->_adapter->forUpdate($sql);
446
+ }
447
+
448
+ return $sql;
449
+ }
450
+ /**
451
+ * Add EXISTS clause
452
+ *
453
+ * @param Varien_Db_Select $select
454
+ * @param string $joinCondition
455
+ * @param bool $isExists
456
+ * @return Varien_Db_Select
457
+ */
458
+ public function exists($select, $joinCondition, $isExists = true)
459
+ {
460
+ if ($isExists) {
461
+ $exists = 'EXISTS (%s)';
462
+ } else {
463
+ $exists = 'NOT EXISTS (%s)';
464
+ }
465
+ $select->reset(self::COLUMNS)
466
+ ->columns(array(new Zend_Db_Expr('1')))
467
+ ->where($joinCondition);
468
+
469
+ $exists = sprintf($exists, $select->assemble());
470
+
471
+ $this->where($exists);
472
+ return $this;
473
+ }
474
+ }
lib/Varien/Db/Statement/Parameter.php ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2010 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Varien DB Statement Parameter
30
+ *
31
+ * Used to transmit specific information about parameter value binding to be bound the right
32
+ * way to the query.
33
+ * Most used properties and methods are defined in interface. Specific things for concrete DB adapter can be
34
+ * transmitted using 'addtional' property (Varien_Object) as a container.
35
+ *
36
+ * @category Varien
37
+ * @package Varien_Db
38
+ * @author Magento Core Team <core@magentocommerce.com>
39
+ */
40
+ class Varien_Db_Statement_Parameter
41
+ {
42
+ /**
43
+ * Actual parameter value
44
+ */
45
+ protected $_value = null;
46
+
47
+ /**
48
+ * Value is a BLOB.
49
+ *
50
+ * A shortcut setting to notify DB adapter, that value must be bound in a default way, as adapter binds
51
+ * BLOB data to query placeholders. If FALSE, then specific settings from $_dataType, $_length,
52
+ * $_driverOptions will be used.
53
+ */
54
+ protected $_isBlob = false;
55
+
56
+ /*
57
+ * Data type to set to DB driver during parameter bind
58
+ */
59
+ protected $_dataType = null;
60
+
61
+ /*
62
+ * Length to set to DB driver during parameter bind
63
+ */
64
+ protected $_length = null;
65
+
66
+ /*
67
+ * Specific driver options to set to DB driver during parameter bind
68
+ */
69
+ protected $_driverOptions = null;
70
+
71
+ /*
72
+ * Additional information to be used by DB adapter internally
73
+ */
74
+ protected $_additional = null;
75
+
76
+ /**
77
+ * Inits instance
78
+ *
79
+ * @param mixed $value
80
+ * @return Varien_Db_Statement_Parameter
81
+ */
82
+ public function __construct($value)
83
+ {
84
+ $this->_value = $value;
85
+ $this->_additional = new Varien_Object();
86
+ return $this;
87
+ }
88
+
89
+ /**
90
+ * Sets parameter value.
91
+ *
92
+ * @param mixed $value
93
+ * @return Varien_Db_Statement_Parameter
94
+ */
95
+ public function setValue($value)
96
+ {
97
+ $this->_value = $value;
98
+ return $this;
99
+ }
100
+
101
+ /**
102
+ * Gets parameter value.
103
+ *
104
+ * @return mixed
105
+ */
106
+ public function getValue()
107
+ {
108
+ return $this->_value;
109
+ }
110
+
111
+ /**
112
+ * Sets, whether parameter is a BLOB.
113
+ *
114
+ * FALSE (default) means, that concrete binding options come in dataType, length and driverOptions properties.
115
+ * TRUE means that DB adapter must ignore other options and use adapter's default options to bind this parameter
116
+ * as a BLOB value.
117
+ *
118
+ * @param bool $isBlob
119
+ * @return Varien_Db_Statement_Parameter
120
+ */
121
+ public function setIsBlob($isBlob)
122
+ {
123
+ $this->_isBlob = $isBlob;
124
+ return $this;
125
+ }
126
+
127
+ /**
128
+ * Gets, whether parameter is a BLOB.
129
+ * See setIsBlob() for returned value explanation.
130
+ *
131
+ * @return bool
132
+ *
133
+ * @see setIsBlob
134
+ */
135
+ public function getIsBlob()
136
+ {
137
+ return $this->_isBlob;
138
+ }
139
+
140
+ /**
141
+ * Sets data type option to be used during binding parameter value.
142
+ *
143
+ * @param mixed $dataType
144
+ * @return Varien_Db_Statement_Parameter
145
+ */
146
+ public function setDataType($dataType)
147
+ {
148
+ $this->_dataType = $dataType;
149
+ return $this;
150
+ }
151
+
152
+ /**
153
+ * Gets data type option to be used during binding parameter value.
154
+ *
155
+ * @return mixed
156
+ */
157
+ public function getDataType()
158
+ {
159
+ return $this->_dataType;
160
+ }
161
+
162
+ /**
163
+ * Sets length option to be used during binding parameter value.
164
+ *
165
+ * @param mixed $length
166
+ * @return Varien_Db_Statement_Parameter
167
+ */
168
+ public function setLength($length)
169
+ {
170
+ $this->_length = $length;
171
+ return $this;
172
+ }
173
+
174
+ /**
175
+ * Gets length option to be used during binding parameter value.
176
+ *
177
+ * @return mixed
178
+ */
179
+ public function getLength()
180
+ {
181
+ return $this->_length;
182
+ }
183
+
184
+ /**
185
+ * Sets specific driver options to be used during binding parameter value.
186
+ *
187
+ * @param mixed $driverOptions
188
+ * @return Varien_Db_Statement_Parameter
189
+ */
190
+ public function setDriverOptions($driverOptions)
191
+ {
192
+ $this->_driverOptions = $driverOptions;
193
+ return $this;
194
+ }
195
+
196
+ /**
197
+ * Gets driver options to be used during binding parameter value.
198
+ *
199
+ * @return mixed
200
+ */
201
+ public function getDriverOptions()
202
+ {
203
+ return $this->_driverOptions;
204
+ }
205
+
206
+ /**
207
+ * Sets additional information for concrete DB adapter.
208
+ * Set there any data you want to pass along with query parameter.
209
+ *
210
+ * @param Varien_Object $additional
211
+ * @return Varien_Db_Statement_Parameter
212
+ */
213
+ public function setAdditional($additional)
214
+ {
215
+ $this->_additional = $additional;
216
+ return $this;
217
+ }
218
+
219
+ /**
220
+ * Gets additional information for concrete DB adapter.
221
+ *
222
+ * @return Varien_Object
223
+ */
224
+ public function getAdditional()
225
+ {
226
+ return $this->_additional;
227
+ }
228
+
229
+ /**
230
+ * Returns representation of a object to be used in string contexts
231
+ *
232
+ * @return string
233
+ */
234
+ public function __toString()
235
+ {
236
+ return (string) $this->_value;
237
+ }
238
+
239
+ /**
240
+ * Returns representation of a object to be used in string contexts
241
+ *
242
+ * @return string
243
+ */
244
+ public function toString()
245
+ {
246
+ return $this->__toString();
247
+ }
248
+ }
lib/Varien/Db/Statement/Pdo/Mysql.php ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2010 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Mysql DB Statement
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Db
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Db_Statement_Pdo_Mysql extends Zend_Db_Statement_Pdo
36
+ {
37
+ /**
38
+ * Executes statement with binding values to it.
39
+ * Allows transferring specific options to DB driver.
40
+ *
41
+ * @param array $params Array of values to bind to parameter placeholders.
42
+ * @return bool
43
+ * @throws Zend_Db_Statement_Exception
44
+ */
45
+ public function _executeWithBinding(array $params)
46
+ {
47
+ // Check whether we deal with named bind
48
+ $isPositionalBind = true;
49
+ foreach ($params as $k => $v) {
50
+ if (!is_int($k)) {
51
+ $isPositionalBind = false;
52
+ break;
53
+ }
54
+ }
55
+
56
+ /* @var $statement PDOStatement */
57
+ $statement = $this->_stmt;
58
+ $bindValues = array(); // Separate array with values, as they are bound by reference
59
+ foreach ($params as $name => $param) {
60
+ $dataType = PDO::PARAM_STR;
61
+ $length = null;
62
+ $driverOptions = null;
63
+
64
+ if ($param instanceof Varien_Db_Statement_Parameter) {
65
+ if ($param->getIsBlob()) {
66
+ // Nothing to do there - default options are fine for MySQL driver
67
+ } else {
68
+ $dataType = $param->getDataType();
69
+ $length = $param->getLength();
70
+ $driverOptions = $param->getDriverOptions();
71
+ }
72
+ $bindValues[$name] = $param->getValue();
73
+ } else {
74
+ $bindValues[$name] = $param;
75
+ }
76
+
77
+ $paramName = $isPositionalBind ? ($name + 1) : $name;
78
+ $statement->bindParam($paramName, $bindValues[$name], $dataType, $length, $driverOptions);
79
+ }
80
+
81
+ try {
82
+ return $statement->execute();
83
+ } catch (PDOException $e) {
84
+ throw new Zend_Db_Statement_Exception($e->getMessage(), (int) $e->getCode(), $e);
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Executes a prepared statement.
90
+ *
91
+ * @param array $params OPTIONAL Values to bind to parameter placeholders.
92
+ * @return bool
93
+ * @throws Zend_Db_Statement_Exception
94
+ */
95
+ public function _execute(array $params = null)
96
+ {
97
+ $specialExecute = false;
98
+ if ($params) {
99
+ foreach ($params as $param) {
100
+ if ($param instanceof Varien_Db_Statement_Parameter) {
101
+ $specialExecute = true;
102
+ break;
103
+ }
104
+ }
105
+ }
106
+
107
+ if ($specialExecute) {
108
+ return $this->_executeWithBinding($params);
109
+ } else {
110
+ return parent::_execute($params);
111
+ }
112
+ }
113
+ }
lib/Varien/Db/Tree.php ADDED
@@ -0,0 +1,561 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien Library
29
+ *
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Db
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+
36
+
37
+ require_once 'Varien/Db/Tree/Exception.php';
38
+ Zend_Loader::loadClass('Zend_Db_Select');
39
+ Zend_Loader::loadClass('Varien_Db_Tree_Node');
40
+ Zend_Loader::loadClass('Varien_Db_Tree_NodeSet');
41
+
42
+ class Varien_Db_Tree
43
+ {
44
+
45
+ private $_id;
46
+ private $_left;
47
+ private $_right;
48
+ private $_level;
49
+ private $_pid;
50
+ private $_nodesInfo = array();
51
+
52
+ /**
53
+ * Array of additional tables
54
+ *
55
+ * array(
56
+ * [$tableName] => array(
57
+ * ['joinCondition']
58
+ * ['fields']
59
+ * )
60
+ * )
61
+ *
62
+ * @var array
63
+ */
64
+ private $_extTables = array();
65
+
66
+ /**
67
+ * Zend_Db_Adapter
68
+ *
69
+ * @var Zend_Db_Adapter_Abstract
70
+ */
71
+ private $_db;
72
+
73
+ private $_table;
74
+
75
+ function __construct($config = array())
76
+ {
77
+ // set a Zend_Db_Adapter connection
78
+ if (! empty($config['db'])) {
79
+
80
+ // convenience variable
81
+ $db = $config['db'];
82
+
83
+ // use an object from the registry?
84
+ if (is_string($db)) {
85
+ $db = Zend::registry($db);
86
+ }
87
+
88
+ // make sure it's a Zend_Db_Adapter
89
+ if (! $db instanceof Zend_Db_Adapter_Abstract) {
90
+ throw new Varien_Db_Tree_Exception('db object does not implement Zend_Db_Adapter_Abstract');
91
+ }
92
+
93
+ // save the connection
94
+ $this->_db = $db;
95
+ $conn = $this->_db->getConnection();
96
+ if ($conn instanceof PDO) {
97
+ $conn->setAttribute (PDO::ATTR_EMULATE_PREPARES, true);
98
+ } elseif ($conn instanceof mysqli) {
99
+ //TODO: ???
100
+ }
101
+ } else {
102
+ throw new Varien_Db_Tree_Exception('db object is not set in config');
103
+ }
104
+
105
+
106
+ if (!empty($config['table'])) {
107
+ $this->setTable($config['table']);
108
+ }
109
+
110
+ if (!empty($config['id'])) {
111
+ $this->setIdField($config['id']);
112
+ } else {
113
+ $this->setIdField('id');
114
+ }
115
+
116
+ if (!empty($config['left'])) {
117
+ $this->setLeftField($config['left']);
118
+ } else {
119
+ $this->setLeftField('left_key');
120
+ }
121
+
122
+ if (!empty($config['right'])) {
123
+ $this->setRightField($config['right']);
124
+ } else {
125
+ $this->setRightField('right_key');
126
+ }
127
+
128
+ if (!empty($config['level'])) {
129
+ $this->setLevelField($config['level']);
130
+ } else {
131
+ $this->setLevelField('level');
132
+ }
133
+
134
+
135
+ if (!empty($config['pid'])) {
136
+ $this->setPidField($config['pid']);
137
+ } else {
138
+ $this->setPidField('parent_id');
139
+ }
140
+
141
+ }
142
+
143
+ /**
144
+ * set name of id field
145
+ *
146
+ * @param string $name
147
+ * @return Varien_Db_Tree
148
+ */
149
+ public function setIdField($name) {
150
+ $this->_id = $name;
151
+ return $this;
152
+ }
153
+
154
+ /**
155
+ * set name of left field
156
+ *
157
+ * @param string $name
158
+ * @return Varien_Db_Tree
159
+ */
160
+ public function setLeftField($name) {
161
+ $this->_left = $name;
162
+ return $this;
163
+ }
164
+
165
+ /**
166
+ * set name of right field
167
+ *
168
+ * @param string $name
169
+ * @return Varien_Db_Tree
170
+ */
171
+ public function setRightField($name) {
172
+ $this->_right = $name;
173
+ return $this;
174
+ }
175
+
176
+ /**
177
+ * set name of level field
178
+ *
179
+ * @param string $name
180
+ * @return Varien_Db_Tree
181
+ */
182
+ public function setLevelField($name) {
183
+ $this->_level = $name;
184
+ return $this;
185
+ }
186
+
187
+ /**
188
+ * set name of pid Field
189
+ *
190
+ * @param string $name
191
+ * @return Varien_Db_Tree
192
+ */
193
+ public function setPidField($name) {
194
+ $this->_pid = $name;
195
+ return $this;
196
+ }
197
+
198
+ /**
199
+ * set table name
200
+ *
201
+ * @param string $name
202
+ * @return Varien_Db_Tree
203
+ */
204
+ public function setTable($name) {
205
+ $this->_table = $name;
206
+ return $this;
207
+ }
208
+
209
+ public function getKeys() {
210
+ $keys = array();
211
+ $keys['id'] = $this->_id;
212
+ $keys['left'] = $this->_left;
213
+ $keys['right'] = $this->_right;
214
+ $keys['pid'] = $this->_pid;
215
+ $keys['level'] = $this->_level;
216
+ return $keys;
217
+ }
218
+
219
+ /**
220
+ * Cleare table and add root element
221
+ *
222
+ */
223
+ public function clear($data = array())
224
+ {
225
+ // clearing table
226
+ $this->_db->query('TRUNCATE '. $this->_table);
227
+ //$this->_db->delete($this->_table,'');
228
+
229
+ // prepare data for root element
230
+ $data[$this->_pid] = 0;
231
+ $data[$this->_left] = 1;
232
+ $data[$this->_right] = 2;
233
+ $data[$this->_level] = 0;
234
+
235
+ try {
236
+ $this->_db->insert($this->_table, $data);
237
+ } catch (PDOException $e) {
238
+ echo $e->getMessage();
239
+ }
240
+ return $this->_db->lastInsertId();
241
+ }
242
+
243
+ public function getNodeInfo($ID) {
244
+ if (empty($this->_nodesInfo[$ID])) {
245
+ $sql = 'SELECT * FROM '.$this->_table.' WHERE '.$this->_id.'=:id';
246
+ $res = $this->_db->query($sql, array('id' => $ID));
247
+ $data = $res->fetch();
248
+ $this->_nodesInfo[$ID] = $data;
249
+ } else {
250
+ $data = $this->_nodesInfo[$ID];
251
+ }
252
+ return $data;
253
+ }
254
+
255
+ public function appendChild($ID, $data) {
256
+
257
+ if (!$info = $this->getNodeInfo($ID)) {
258
+ return false;
259
+ }
260
+
261
+ $data[$this->_left] = $info[$this->_right];
262
+ $data[$this->_right] = $info[$this->_right] + 1;
263
+ $data[$this->_level] = $info[$this->_level] + 1;
264
+ $data[$this->_pid] = $ID;
265
+
266
+ // creating a place for the record being inserted
267
+ if($ID) {
268
+ $this->_db->beginTransaction();
269
+ try {
270
+ $sql = 'UPDATE '.$this->_table.' SET'
271
+ . ' `'.$this->_left.'` = IF( `'.$this->_left.'` > :left, `'.$this->_left.'`+2, `'.$this->_left.'`),'
272
+ . ' `'.$this->_right.'` = IF( `'.$this->_right.'`>= :right, `'.$this->_right.'`+2, `'.$this->_right.'`)'
273
+ . ' WHERE `'.$this->_right.'` >= :right';
274
+
275
+ $this->_db->query($sql, array('left'=>$info[$this->_left], 'right'=>$info[$this->_right]));
276
+
277
+ $this->_db->insert($this->_table, $data);
278
+ $this->_db->commit();
279
+ } catch (PDOException $p) {
280
+ $this->_db->rollBack();
281
+ echo $p->getMessage();
282
+ exit();
283
+ } catch (Exception $e) {
284
+ $this->_db->rollBack();
285
+ echo $e->getMessage();
286
+ echo $sql;
287
+ var_dump($data);
288
+ exit();
289
+ }
290
+ // TODO: change to ZEND LIBRARY
291
+ $res = $this->_db->fetchOne('select last_insert_id()');
292
+ return $res;
293
+ //return $this->_db->fetchOne('select last_insert_id()');
294
+ //return $this->_db->lastInsertId();
295
+ }
296
+ return false;
297
+ }
298
+
299
+ public function checkNodes() {
300
+ $sql = $this->_db->select();
301
+
302
+ $sql->from(array('t1'=>$this->_table), array('t1.'.$this->_id, new Zend_Db_Expr('COUNT(t1.'.$this->_id.') AS rep')))
303
+ ->from(array('t2'=>$this->_table))
304
+ ->from(array('t3'=>$this->_table), new Zend_Db_Expr('MAX(t3.'.$this->_right.') AS max_right'));
305
+
306
+
307
+ $sql->where('t1.'.$this->_left.' <> t2.'.$this->_left)
308
+ ->where('t1.'.$this->_left.' <> t2.'.$this->_right)
309
+ ->where('t1.'.$this->_right.' <> t2.'.$this->_right);
310
+
311
+ $sql->group('t1.'.$this->_id);
312
+ $sql->having('max_right <> SQRT(4 * rep + 1) + 1');
313
+
314
+
315
+ return $this->_db->fetchAll($sql);
316
+ }
317
+
318
+ public function insertBefore($ID, $data) {
319
+
320
+ }
321
+
322
+ public function removeNode($ID) {
323
+
324
+ if (!$info = $this->getNodeInfo($ID)) {
325
+ return false;
326
+ }
327
+
328
+ if($ID) {
329
+ $this->_db->beginTransaction();
330
+ try {
331
+ // DELETE FROM my_tree WHERE left_key >= $left_key AND right_key <= $right_key
332
+ $this->_db->delete($this->_table, $this->_left.' >= '.$info[$this->_left].' AND '.$this->_right.' <= '.$info[$this->_right]);
333
+
334
+ // UPDATE my_tree SET left_key = IF(left_key > $left_key, left_key – ($right_key - $left_key + 1), left_key), right_key = right_key – ($right_key - $left_key + 1) WHERE right_key > $right_key
335
+ $sql = 'UPDATE '.$this->_table.'
336
+ SET
337
+ '.$this->_left.' = IF('.$this->_left.' > '.$info[$this->_left].', '.$this->_left.' - '.($info[$this->_right] - $info[$this->_left] + 1).', '.$this->_left.'),
338
+ '.$this->_right.' = '.$this->_right.' - '.($info[$this->_right] - $info[$this->_left] + 1).'
339
+ WHERE
340
+ '.$this->_right.' > '.$info[$this->_right];
341
+ $this->_db->query($sql);
342
+ $this->_db->commit();
343
+ return new Varien_Db_Tree_Node($info, $this->getKeys());;
344
+ } catch (Exception $e) {
345
+ $this->_db->rollBack();
346
+ echo $e->getMessage();
347
+ }
348
+ }
349
+ }
350
+
351
+
352
+ public function moveNode($eId, $pId, $aId = 0) {
353
+
354
+ $eInfo = $this->getNodeInfo($eId);
355
+ $pInfo = $this->getNodeInfo($pId);
356
+
357
+
358
+ $leftId = $eInfo[$this->_left];
359
+ $rightId = $eInfo[$this->_right];
360
+ $level = $eInfo[$this->_level];
361
+
362
+ $leftIdP = $pInfo[$this->_left];
363
+ $rightIdP = $pInfo[$this->_right];
364
+ $levelP = $pInfo[$this->_level];
365
+
366
+ if ($eId == $pId || $leftId == $leftIdP || ($leftIdP >= $leftId && $leftIdP <= $rightId) || ($level == $levelP+1 && $leftId > $leftIdP && $rightId < $rightIdP)) {
367
+ echo "alert('cant_move_tree');";
368
+ return FALSE;
369
+ }
370
+
371
+ if ($leftIdP < $leftId && $rightIdP > $rightId && $levelP < $level - 1) {
372
+ $sql = 'UPDATE '.$this->_table.' SET '
373
+ . $this->_level . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_level.sprintf('%+d', -($level-1)+$levelP) . ' ELSE ' . $this->_level . ' END, '
374
+ . $this->_right . ' = CASE WHEN ' . $this->_right . ' BETWEEN ' . ($rightId+1) . ' AND ' . ($rightIdP-1) . ' THEN ' . $this->_right . '-' . ($rightId-$leftId+1) . ' '
375
+ . 'WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_right . '+' . ((($rightIdP-$rightId-$level+$levelP)/2)*2+$level-$levelP-1) . ' ELSE ' . $this->_right . ' END, '
376
+ . $this->_left . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . ($rightId+1) . ' AND ' . ($rightIdP-1) . ' THEN ' . $this->_left . '-' . ($rightId-$leftId+1) . ' '
377
+ . 'WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_left . '+' . ((($rightIdP-$rightId-$level+$levelP)/2)*2+$level-$levelP-1) . ' ELSE ' . $this->_left . ' END '
378
+ . 'WHERE ' . $this->_left . ' BETWEEN ' . ($leftIdP+1) . ' AND ' . ($rightIdP-1);
379
+ } elseif ($leftIdP < $leftId) {
380
+ $sql = 'UPDATE ' . $this->_table . ' SET '
381
+ . $this->_level . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_level.sprintf('%+d', -($level-1)+$levelP) . ' ELSE ' . $this->_level . ' END, '
382
+ . $this->_left . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . $rightIdP . ' AND ' . ($leftId-1) . ' THEN ' . $this->_left . '+' . ($rightId-$leftId+1) . ' '
383
+ . 'WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_left . '-' . ($leftId-$rightIdP) . ' ELSE ' . $this->_left . ' END, '
384
+ . $this->_right . ' = CASE WHEN ' . $this->_right . ' BETWEEN ' . $rightIdP . ' AND ' . $leftId . ' THEN ' . $this->_right . '+' . ($rightId-$leftId+1) . ' '
385
+ . 'WHEN ' . $this->_right . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_right . '-' . ($leftId-$rightIdP) . ' ELSE ' . $this->_right . ' END '
386
+ . 'WHERE (' . $this->_left . ' BETWEEN ' . $leftIdP . ' AND ' . $rightId. ' '
387
+ . 'OR ' . $this->_right . ' BETWEEN ' . $leftIdP . ' AND ' . $rightId . ')';
388
+ } else {
389
+ $sql = 'UPDATE ' . $this->_table . ' SET '
390
+ . $this->_level . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_level.sprintf('%+d', -($level-1)+$levelP) . ' ELSE ' . $this->_level . ' END, '
391
+ . $this->_left . ' = CASE WHEN ' . $this->_left . ' BETWEEN ' . $rightId . ' AND ' . $rightIdP . ' THEN ' . $this->_left . '-' . ($rightId-$leftId+1) . ' '
392
+ . 'WHEN ' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_left . '+' . ($rightIdP-1-$rightId) . ' ELSE ' . $this->_left . ' END, '
393
+ . $this->_right . ' = CASE WHEN ' . $this->_right . ' BETWEEN ' . ($rightId+1) . ' AND ' . ($rightIdP-1) . ' THEN ' . $this->_right . '-' . ($rightId-$leftId+1) . ' '
394
+ . 'WHEN ' . $this->_right . ' BETWEEN ' . $leftId . ' AND ' . $rightId . ' THEN ' . $this->_right . '+' . ($rightIdP-1-$rightId) . ' ELSE ' . $this->_right . ' END '
395
+ . 'WHERE (' . $this->_left . ' BETWEEN ' . $leftId . ' AND ' . $rightIdP . ' '
396
+ . 'OR ' . $this->_right . ' BETWEEN ' . $leftId . ' AND ' . $rightIdP . ')';
397
+ }
398
+
399
+ $this->_db->beginTransaction();
400
+ try {
401
+ $this->_db->query($sql);
402
+ $this->_db->commit();
403
+ echo "alert('node moved');";
404
+ return true;
405
+ } catch (Exception $e) {
406
+ $this->_db->rollBack();
407
+ echo "alert('node not moved: fatal error');";
408
+ echo $e->getMessage();
409
+ echo "<br>\r\n";
410
+ echo $sql;
411
+ echo "<br>\r\n";
412
+ exit();
413
+ }
414
+ }
415
+
416
+
417
+ public function __moveNode($eId, $pId, $aId = 0) {
418
+
419
+ $eInfo = $this->getNodeInfo($eId);
420
+ if ($pId != 0) {
421
+ $pInfo = $this->getNodeInfo($pId);
422
+ }
423
+ if ($aId != 0) {
424
+ $aInfo = $this->getNodeInfo($aId);
425
+ }
426
+
427
+ $level = $eInfo[$this->_level];
428
+ $left_key = $eInfo[$this->_left];
429
+ $right_key = $eInfo[$this->_right];
430
+ if ($pId == 0) {
431
+ $level_up = 0;
432
+ } else {
433
+ $level_up = $pInfo[$this->_level];
434
+ }
435
+
436
+ $right_key_near = 0;
437
+ $left_key_near = 0;
438
+
439
+ if ($pId == 0) { //move to root
440
+ $right_key_near = $this->_db->fetchOne('SELECT MAX('.$this->_right.') FROM '.$this->_table);
441
+ } elseif ($aId != 0 && $pID == $eInfo[$this->_pid]) { // if we have after ID
442
+ $right_key_near = $aInfo[$this->_right];
443
+ $left_key_near = $aInfo[$this->_left];
444
+ } elseif ($aId == 0 && $pId == $eInfo[$this->_pid]) { // if we do not have after ID
445
+ $right_key_near = $pInfo[$this->_left];
446
+ } elseif ($pId != $eInfo[$this->_pid]) {
447
+ $right_key_near = $pInfo[$this->_right] - 1;
448
+ }
449
+
450
+
451
+ $skew_level = $pInfo[$this->_level] - $eInfo[$this->_level] + 1;
452
+ $skew_tree = $eInfo[$this->_right] - $eInfo[$this->_left] + 1;
453
+
454
+ echo "alert('".$right_key_near."');";
455
+
456
+ if ($right_key_near > $right_key) { // up
457
+ echo "alert('move up');";
458
+ $skew_edit = $right_key_near - $left_key + 1;
459
+ $sql = 'UPDATE '.$this->_table.'
460
+ SET
461
+ '.$this->_right.' = IF('.$this->_left.' >= '.$eInfo[$this->_left].', '.$this->_right.' + '.$skew_edit.', IF('.$this->_right.' < '.$eInfo[$this->_left].', '.$this->_right.' + '.$skew_tree.', '.$this->_right.')),
462
+ '.$this->_level.' = IF('.$this->_left.' >= '.$eInfo[$this->_left].', '.$this->_level.' + '.$skew_level.', '.$this->_level.'),
463
+ '.$this->_left.' = IF('.$this->_left.' >= '.$eInfo[$this->_left].', '.$this->_left.' + '.$skew_edit.', IF('.$this->_left.' > '.$right_key_near.', '.$this->_left.' + '.$skew_tree.', '.$this->_left.'))
464
+ WHERE '.$this->_right.' > '.$right_key_near.' AND '.$this->_left.' < '.$eInfo[$this->_right];
465
+ } elseif ($right_key_near < $right_key) { // down
466
+ echo "alert('move down');";
467
+ $skew_edit = $right_key_near - $left_key + 1 - $skew_tree;
468
+ $sql = 'UPDATE '.$this->_table.'
469
+ SET
470
+ '.$this->_left.' = IF('.$this->_right.' <= '.$right_key.', '.$this->_left.' + '.$skew_edit.', IF('.$this->_left.' > '.$right_key.', '.$this->_left.' - '.$skew_tree.', '.$this->_left.')),
471
+ '.$this->_level.' = IF('.$this->_right.' <= '.$right_key.', '.$this->_level.' + '.$skew_level.', '.$this->_level.'),
472
+ '.$this->_right.' = IF('.$this->_right.' <= '.$right_key.', '.$this->_right.' + '.$skew_edit.', IF('.$this->_right.' <= '.$right_key_near.', '.$this->_right.' - '.$skew_tree.', '.$this->_right.'))
473
+ WHERE
474
+ '.$this->_right.' > '.$left_key.' AND '.$this->_left.' <= '.$right_key_near;
475
+ }
476
+
477
+
478
+ $this->_db->beginTransaction();
479
+ try {
480
+ $this->_db->query($sql);
481
+ //$afrows = $this->_db->get
482
+ $this->_db->commit();
483
+
484
+ } catch (Exception $e) {
485
+ $this->_db->rollBack();
486
+ echo $e->getMessage();
487
+ echo "<br>\r\n";
488
+ echo $sql;
489
+ echo "<br>\r\n";
490
+ exit();
491
+ }
492
+ echo "alert('node added')";
493
+ }
494
+
495
+ public function addTable($tableName, $joinCondition, $fields='*')
496
+ {
497
+ $this->_extTables[$tableName] = array(
498
+ 'joinCondition' => $joinCondition,
499
+ 'fields' => $fields
500
+ );
501
+ }
502
+
503
+ protected function _addExtTablesToSelect(Zend_Db_Select &$select)
504
+ {
505
+ foreach ($this->_extTables as $tableName=>$info) {
506
+ $select->joinInner($tableName, $info['joinCondition'], $info['fields']);
507
+ }
508
+ }
509
+
510
+ public function getChildren($ID, $start_level = 0, $end_level = 0)
511
+ {
512
+ try {
513
+ $info = $this->getNodeInfo($ID);
514
+ } catch (Exception $e) {
515
+ echo $e->getMessage();
516
+ exit;
517
+ }
518
+
519
+ $dbSelect = new Zend_Db_Select($this->_db);
520
+ $dbSelect->from($this->_table)
521
+ ->where($this->_left . ' >= :left')
522
+ ->where($this->_right . ' <= :right')
523
+ ->order($this->_left);
524
+
525
+ $this->_addExtTablesToSelect($dbSelect);
526
+
527
+ $data = array();
528
+ $data['left'] = $info[$this->_left];
529
+ $data['right'] = $info[$this->_right];
530
+
531
+ if (!empty($start_level) && empty($end_level)) {
532
+ $dbSelect->where($this->_level . ' = :minLevel');
533
+ $data['minLevel'] = $info[$this->_level] + $start_level;
534
+ }
535
+
536
+ //echo $dbSelect->__toString();
537
+ $data = $this->_db->fetchAll($dbSelect, $data);
538
+
539
+ $nodeSet = new Varien_Db_Tree_NodeSet();
540
+ foreach ($data as $node) {
541
+ $nodeSet->addNode(new Varien_Db_Tree_Node($node, $this->getKeys()));
542
+ }
543
+ return $nodeSet;
544
+ }
545
+
546
+ public function getNode($nodeId)
547
+ {
548
+ $dbSelect = new Zend_Db_Select($this->_db);
549
+ $dbSelect->from($this->_table)
550
+ ->where($this->_table.'.'.$this->_id . ' >= :id');
551
+
552
+ $this->_addExtTablesToSelect($dbSelect);
553
+
554
+ $data = array();
555
+ $data['id'] = $nodeId;
556
+
557
+ $data = $this->_db->fetchRow($dbSelect, $data);
558
+
559
+ return new Varien_Db_Tree_Node($data, $this->getKeys());
560
+ }
561
+ }
lib/Varien/Db/Tree/Exception.php ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ require_once 'Varien/Db/Exception.php';
29
+
30
+ class Varien_Db_Tree_Exception extends Varien_Db_Exception
31
+ {}
lib/Varien/Db/Tree/Node.php ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ require_once 'Varien/Db/Tree/Node/Exception.php';
29
+
30
+ class Varien_Db_Tree_Node {
31
+
32
+ private $left;
33
+ private $right;
34
+ private $id;
35
+ private $pid;
36
+ private $level;
37
+ private $title;
38
+ private $data;
39
+
40
+
41
+ public $hasChild = false;
42
+ public $numChild = 0;
43
+
44
+
45
+ function __construct($nodeData = array(), $keys) {
46
+ if (empty($nodeData)) {
47
+ throw new Varien_Db_Tree_Node_Exception('Empty array of node information');
48
+ }
49
+ if (empty($keys)) {
50
+ throw new Varien_Db_Tree_Node_Exception('Empty keys array');
51
+ }
52
+
53
+ $this->id = $nodeData[$keys['id']];
54
+ $this->pid = $nodeData[$keys['pid']];
55
+ $this->left = $nodeData[$keys['left']];
56
+ $this->right = $nodeData[$keys['right']];
57
+ $this->level = $nodeData[$keys['level']];
58
+
59
+ $this->data = $nodeData;
60
+ $a = $this->right - $this->left;
61
+ if ($a > 1) {
62
+ $this->hasChild = true;
63
+ $this->numChild = ($a - 1) / 2;
64
+ }
65
+ return $this;
66
+ }
67
+
68
+ function getData($name) {
69
+ if (isset($this->data[$name])) {
70
+ return $this->data[$name];
71
+ } else {
72
+ return null;
73
+ }
74
+ }
75
+
76
+ function getLevel() {
77
+ return $this->level;
78
+ }
79
+
80
+ function getLeft() {
81
+ return $this->left;
82
+ }
83
+
84
+ function getRight() {
85
+ return $this->right;
86
+ }
87
+
88
+ function getPid() {
89
+ return $this->pid;
90
+ }
91
+
92
+ function getId() {
93
+ return $this->id;
94
+ }
95
+
96
+ /**
97
+ * Return true if node have chield
98
+ *
99
+ * @return boolean
100
+ */
101
+ function isParent() {
102
+ if ($this->right - $this->left > 1) {
103
+ return true;
104
+ } else {
105
+ return false;
106
+ }
107
+ }
108
+ }
lib/Varien/Db/Tree/Node/Exception.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Db_Tree_Node_Exception extends Varien_Db_Tree_Exception
29
+ {}
lib/Varien/Db/Tree/NodeSet.php ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * TODO implements iterators
30
+ *
31
+ */
32
+ class Varien_Db_Tree_NodeSet implements Iterator
33
+ {
34
+ private $_nodes = array();
35
+ private $_currentNode = 0;
36
+ private $_current = 0;
37
+
38
+
39
+ function __construct() {
40
+ $this->_nodes = array();
41
+ $this->_current = 0;
42
+ $this->_currentNode = 0;
43
+ $this->count = 0;
44
+ }
45
+
46
+
47
+
48
+ function addNode(Varien_Db_Tree_Node $node) {
49
+ $this->_nodes[$this->_currentNode] = $node;
50
+ $this->count++;
51
+ return ++$this->_currentNode;
52
+ }
53
+
54
+ function count() {
55
+ return $this->count;
56
+ }
57
+
58
+
59
+ function valid() {
60
+ return isset($this->_nodes[$this->_current]);
61
+ }
62
+
63
+ function next() {
64
+ if ($this->_current > $this->_currentNode) {
65
+ return false;
66
+ } else {
67
+ return $this->_current++;
68
+ }
69
+ }
70
+
71
+ function key() {
72
+ return $this->_current;
73
+ }
74
+
75
+
76
+ function current() {
77
+ return $this->_nodes[$this->_current];
78
+ }
79
+
80
+ function rewind() {
81
+ $this->_current = 0;
82
+ }
83
+ }
lib/Varien/Db/Tree/NodeSet/Exception.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Db
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Db_Tree_NodeSet_Exception extends Varien_Db_Tree_Exception
29
+ {}
lib/Varien/Debug.php ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Debug
23
+ * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien Debug methods
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Debug
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Debug
35
+ {
36
+ public static $argLength = 16;
37
+ /**
38
+ * Magento Root path
39
+ *
40
+ * @var string
41
+ */
42
+ protected static $_filePath;
43
+
44
+ /**
45
+ * Retrieve real root path with last directory separator
46
+ *
47
+ * @return string
48
+ */
49
+ public static function getRootPath()
50
+ {
51
+ if (is_null(self::$_filePath)) {
52
+ if (defined('BP')) {
53
+ self::$_filePath = BP;
54
+ } else {
55
+ self::$_filePath = dirname(dirname(__FILE__));
56
+ }
57
+ }
58
+ return self::$_filePath;
59
+ }
60
+
61
+ /**
62
+ * Prints or return a backtrace
63
+ *
64
+ * @param bool $return return or print
65
+ * @param bool $html output in HTML format
66
+ * @param bool $withArgs add short argumets of methods
67
+ * @return string|bool
68
+ */
69
+ public static function backtrace($return = false, $html = true, $withArgs = true)
70
+ {
71
+ $trace = debug_backtrace();
72
+ return self::trace($trace, $return, $html, $withArgs);
73
+ }
74
+
75
+ /**
76
+ * Prints or return a trace
77
+ *
78
+ * @param array $trace trace array
79
+ * @param bool $return return or print
80
+ * @param bool $html output in HTML format
81
+ * @param bool $withArgs add short argumets of methods
82
+ * @return string|bool
83
+ */
84
+ public static function trace(array $trace, $return = false, $html = true, $withArgs = true)
85
+ {
86
+ $out = '';
87
+ if ($html) {
88
+ $out .= '<pre>';
89
+ }
90
+
91
+ foreach ($trace as $i => $data) {
92
+ // skip self
93
+ if ($i == 0) {
94
+ continue;
95
+ }
96
+
97
+ // prepare method argments
98
+ $args = array();
99
+ if (isset($data['args']) && $withArgs) {
100
+ foreach ($data['args'] as $arg) {
101
+ $args[] = self::_formatCalledArgument($arg);
102
+ }
103
+ }
104
+
105
+ // prepare method's name
106
+ if (isset($data['class']) && isset($data['function'])) {
107
+ if (isset($data['object']) && get_class($data['object']) != $data['class']) {
108
+ $className = get_class($data['object']) . '[' . $data['class'] . ']';
109
+ } else {
110
+ $className = $data['class'];
111
+ }
112
+ if (isset($data['object'])) {
113
+ $className .= sprintf('#%s#', spl_object_hash($data['object']));
114
+ }
115
+
116
+ $methodName = sprintf('%s%s%s(%s)',
117
+ $className,
118
+ isset($data['type']) ? $data['type'] : '->',
119
+ $data['function'],
120
+ join(', ', $args)
121
+ );
122
+ } else if (isset($data['function'])) {
123
+ $methodName = sprintf('%s(%s)', $data['function'], join(', ', $args));
124
+ }
125
+
126
+ if (isset($data['file'])) {
127
+ $pos = strpos($data['file'], self::getRootPath());
128
+ if ($pos !== false) {
129
+ $data['file'] = substr($data['file'], strlen(self::getRootPath()) + 1);
130
+ }
131
+ $fileName = sprintf('%s:%d', $data['file'], $data['line']);
132
+ } else {
133
+ $fileName = false;
134
+ }
135
+
136
+ if ($fileName) {
137
+ $out .= sprintf('#%d %s called at [%s]', $i, $methodName, $fileName);
138
+ } else {
139
+ $out .= sprintf('#%d %s', $i, $methodName);
140
+ }
141
+
142
+ $out .= "\n";
143
+ }
144
+
145
+ if ($html) {
146
+ $out .= '</pre>';
147
+ }
148
+
149
+ if ($return) {
150
+ return $out;
151
+ } else {
152
+ echo $out;
153
+ return true;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Format argument in called method
159
+ *
160
+ * @param mixed $arg
161
+ */
162
+ protected static function _formatCalledArgument($arg)
163
+ {
164
+ $out = '';
165
+ if (is_object($arg)) {
166
+ $out .= sprintf("&%s#%s#", get_class($arg), spl_object_hash($arg));
167
+
168
+ } else if (is_resource($arg)) {
169
+ $out .= '#[' . get_resource_type($arg) . ']';
170
+ } else if (is_array($arg)) {
171
+ $isAssociative = false;
172
+ $args = array();
173
+ foreach ($arg as $k => $v) {
174
+ if (!is_numeric($k)) {
175
+ $isAssociative = true;
176
+ }
177
+ $args[$k] = self::_formatCalledArgument($v);
178
+ }
179
+ if ($isAssociative) {
180
+ $arr = array();
181
+ foreach ($args as $k => $v) {
182
+ $arr[] = self::_formatCalledArgument($k) . ' => ' . $v;
183
+ }
184
+ $out .= 'array(' . join(', ', $arr) . ')';
185
+ } else {
186
+ $out .= 'array(' . join(', ', $args) . ')';
187
+ }
188
+ } else if (is_null($arg)) {
189
+ $out .= 'NULL';
190
+ } else if (is_numeric($arg) || is_float($arg)) {
191
+ $out .= $arg;
192
+ } else if (is_string($arg)) {
193
+ if (strlen($arg) > self::$argLength) {
194
+ $arg = substr($arg, 0, self::$argLength) . "...";
195
+ }
196
+ $arg = strtr($arg, array("\t" => '\t', "\r" => '\r', "\n" => '\n', "'" => '\\\''));
197
+ $out .= "'" . $arg . "'";
198
+ } else if (is_bool($arg)) {
199
+ $out .= $arg === true ? 'true' : 'false';
200
+ }
201
+
202
+ return $out;
203
+ }
204
+ }
lib/Varien/Directory/Collection.php ADDED
@@ -0,0 +1,461 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Directory
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien Directory Collection
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Directory
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ require_once('Varien/Data/Collection.php');
36
+ require_once('Varien/Directory/Factory.php');
37
+ require_once('Varien/Directory/IFactory.php');
38
+
39
+ class Varien_Directory_Collection extends Varien_Data_Collection implements IFactory{
40
+ protected $_path='';
41
+ protected $_dirName='';
42
+ protected $_recursionLevel=0;
43
+ protected $_isRecursion;
44
+ protected $_filters = array();
45
+ /**
46
+ * Constructor
47
+ *
48
+ * @param string $path - path to directory
49
+ * @param bool $is_recursion - use or not recursion
50
+ * @return none
51
+ */
52
+ public function __construct($path,$isRecursion=true,$recursionLevel = 0)
53
+ {
54
+ parent::__construct();
55
+ $this->setPath($path);
56
+ $this->_dirName = $this->lastDir();
57
+ $this->setRecursion($isRecursion);
58
+ $this->setRecursionLevel($recursionLevel);
59
+ if($this->getRecursion() || $this->getRecursionLevel()==0){
60
+ $this->parseDir();
61
+ }
62
+ }
63
+ /**
64
+ * Get name of this directory
65
+ *
66
+ * @return string - name of this directory
67
+ */
68
+ public function getDirName()
69
+ {
70
+ return $this->_dirName;
71
+ }
72
+ /**
73
+ * Get recursion
74
+ *
75
+ * @return bool - is or not recursion
76
+ */
77
+ public function getRecursion()
78
+ {
79
+ return $this->_isRecursion;
80
+ }
81
+ /**
82
+ * Get recursion level
83
+ *
84
+ * @return int - recursion level
85
+ */
86
+ public function getRecursionLevel()
87
+ {
88
+ return $this->_recursionLevel;
89
+ }
90
+ /**
91
+ * Get path
92
+ *
93
+ * @return string - path to this directory
94
+ */
95
+ public function getPath()
96
+ {
97
+ return $this->_path;
98
+ }
99
+ /**
100
+ * Set path to this directory
101
+ * @param string $path - path to this directory
102
+ * @param bool $isRecursion - use or not recursion
103
+ * @return none
104
+ */
105
+ public function setPath($path, $isRecursion='')
106
+ {
107
+ if(is_dir($path)){
108
+ if(isset($this->_path) && $this->_path!=$path && $this->_path!=''){
109
+ $this->_path = $path;
110
+ if($isRecursion!='')$this->_isRecursion = $isRecursion;
111
+ $this->parseDir();
112
+ } else {
113
+ $this->_path = $path;
114
+ }
115
+ } else {
116
+ throw new Exception($path. 'is not dir.');
117
+ }
118
+ }
119
+ /**
120
+ * Set recursion
121
+ *
122
+ * @param bool $isRecursion - use or not recursion
123
+ * @return none
124
+ */
125
+ public function setRecursion($isRecursion)
126
+ {
127
+ $this->_isRecursion = $isRecursion;
128
+ }
129
+ /**
130
+ * Set level of recursion
131
+ *
132
+ * @param int $recursionLevel - level of recursion
133
+ * @return none
134
+ */
135
+ public function setRecursionLevel($recursionLevel)
136
+ {
137
+ $this->_recursionLevel = $recursionLevel;
138
+ }
139
+ /**
140
+ * get latest dir in the path
141
+ *
142
+ * @param string $path - path to directory
143
+ * @return string - latest dir in the path
144
+ */
145
+ public function lastDir()
146
+ {
147
+ return self::getLastDir($this->getPath());
148
+ }
149
+ /**
150
+ * get latest dir in the path
151
+ *
152
+ * @param string $path - path to directory
153
+ * @return string - latest dir in the path
154
+ */
155
+ static public function getLastDir($path){
156
+ if($path=='') $path = $this->getPath();
157
+ $last = strrpos($path, "/");
158
+ return substr($path,$last+1);
159
+ }
160
+ /**
161
+ * add item to collection
162
+ *
163
+ * @param IFactory $item - item of collection
164
+ * @return none
165
+ */
166
+ public function addItem(IFactory $item)
167
+ {
168
+ $this->_items[] = $item;
169
+ }
170
+ /**
171
+ * parse this directory
172
+ *
173
+ * @return none
174
+ */
175
+ protected function parseDir()
176
+ {
177
+ $this->clear();
178
+ $iter = new RecursiveDirectoryIterator($this->getPath());
179
+ while ($iter->valid()) {
180
+ $curr = (string)$iter->getSubPathname();
181
+ if (!$iter->isDot() && $curr[0]!='.'){
182
+ $this->addItem(Varien_Directory_Factory::getFactory($iter->current(),$this->getRecursion(),$this->getRecursionLevel()));
183
+ }
184
+ $iter->next();
185
+ }
186
+ }
187
+ /**
188
+ * set filter using
189
+ *
190
+ * @param bool $useFilter - filter using
191
+ * @return none
192
+ */
193
+ public function useFilter($useFilter)
194
+ {
195
+ $this->_renderFilters();
196
+ $this->walk('useFilter', array($useFilter));
197
+ }
198
+ /**
199
+ * get files names of current collection
200
+ *
201
+ * @return array - files names of current collection
202
+ */
203
+ public function filesName()
204
+ {
205
+ $files = array();
206
+ $this->getFilesName($files);
207
+ return $files;
208
+
209
+ }
210
+ /**
211
+ * get files names of current collection
212
+ *
213
+ * @param array $files - array of files names
214
+ * @return none
215
+ */
216
+ public function getFilesName(&$files)
217
+ {
218
+ $this->walk('getFilesName', array(&$files));
219
+ }
220
+ /**
221
+ * get files paths of current collection
222
+ *
223
+ * @return array - files paths of current collection
224
+ */
225
+ public function filesPaths()
226
+ {
227
+ $paths = array();
228
+ $this->getFilesPaths($paths);
229
+ return $paths;
230
+ }
231
+ /**
232
+ * get files paths of current collection
233
+ *
234
+ * @param array $files - array of files paths
235
+ * @return none
236
+ */
237
+ public function getFilesPaths(&$paths)
238
+ {
239
+ $this->walk('getFilesPaths', array(&$paths));
240
+ }
241
+ /**
242
+ * get SplFileObject objects of files of current collection
243
+ *
244
+ * @return array - array of SplFileObject objects
245
+ */
246
+ public function filesObj()
247
+ {
248
+ $objs = array();
249
+ $this->getFilesObj($objs);
250
+ return $objs;
251
+ }
252
+ /**
253
+ * get SplFileObject objects of files of current collection
254
+ *
255
+ * @param array $objs - array of SplFileObject objects
256
+ * @return none
257
+ */
258
+ public function getFilesObj(&$objs)
259
+ {
260
+ $this->walk('getFilesObj', array(&$objs));
261
+ }
262
+ /**
263
+ * get names of dirs of current collection
264
+ *
265
+ * @return array - array of names of dirs
266
+ */
267
+ public function dirsName()
268
+ {
269
+ $dir = array();
270
+ $this->getDirsName($dir);
271
+ return $dir;
272
+ }
273
+ /**
274
+ * get names of dirs of current collection
275
+ *
276
+ * @param array $dirs - array of names of dirs
277
+ * @return none
278
+ */
279
+ public function getDirsName(&$dirs)
280
+ {
281
+ $this->walk('getDirsName', array(&$dirs));
282
+ if($this->getRecursionLevel()>0)
283
+ $dirs[] = $this->getDirName();
284
+ }
285
+ /**
286
+ * set filters for files
287
+ *
288
+ * @param array $filter - array of filters
289
+ * @return none
290
+ */
291
+ protected function setFilesFilter($filter)
292
+ {
293
+ $this->walk('setFilesFilter', array($filter));
294
+ }
295
+ /**
296
+ * display this collection as array
297
+ *
298
+ * @return array
299
+ */
300
+ public function __toArray()
301
+ {
302
+ $arr = array();
303
+ $this->toArray($arr);
304
+ return $arr;
305
+ }
306
+ /**
307
+ * display this collection as array
308
+ * @param array &$arr - this collection array
309
+ * @return none
310
+ */
311
+ public function toArray(&$arr)
312
+ {
313
+ if($this->getRecursionLevel()>0){
314
+ $arr[$this->getDirName()] = array();
315
+ $this->walk('toArray', array(&$arr[$this->getDirName()]));
316
+ } else {
317
+ $this->walk('toArray', array(&$arr));
318
+ }
319
+ }
320
+ /**
321
+ * get this collection as xml
322
+ * @param bool $addOpenTag - add or not header of xml
323
+ * @param string $rootName - root element name
324
+ * @return none
325
+ */
326
+ public function __toXml($addOpenTag=true,$rootName='Struct')
327
+ {
328
+ $xml='';
329
+ $this->toXml($xml,$addOpenTag,$rootName);
330
+ return $xml;
331
+ }
332
+ /**
333
+ * get this collection as xml
334
+ * @param string &$xml - xml
335
+ * @param bool $addOpenTag - add or not header of xml
336
+ * @param string $rootName - root element name
337
+ * @return none
338
+ */
339
+ public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
340
+ {
341
+ if($recursionLevel==0 ){
342
+ $xml = '';
343
+ if($addOpenTag)
344
+ $xml.= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
345
+ $xml.= '<'.$rootName.'>'."\n";
346
+ }
347
+ $recursionLevel = $this->getRecursionLevel();
348
+ $xml.= str_repeat("\t",$recursionLevel+1)."<$this->_dirName>\n";
349
+ $this->walk('toXml', array(&$xml,$recursionLevel,$addOpenTag,$rootName));
350
+ $xml.= str_repeat("\t",$recursionLevel+1)."</$this->_dirName>"."\n";
351
+ if($recursionLevel==0 ){
352
+ $xml.= '</'.$rootName.'>'."\n";
353
+ }
354
+ }
355
+ /**
356
+ * apply filters
357
+ * @return none
358
+ */
359
+ protected function _renderFilters()
360
+ {
361
+ $exts = array();
362
+ $names = array();
363
+ $regName = array();
364
+ foreach ($this->_filters as $filter){
365
+ switch ($filter['field']){
366
+ case 'extension':
367
+ if(is_array($filter['value'])){
368
+ foreach ($filter['value'] as $value){
369
+ $exts[] = $value;
370
+ }
371
+ } else {
372
+ $exts[] = $filter['value'];
373
+ }
374
+ break;
375
+ case 'name':
376
+ if(is_array($filter['value'])){
377
+ foreach ($filter['value'] as $value){
378
+ $names[] = $filter['value'];
379
+ }
380
+ } else {
381
+ $names[] = $filter['value'];
382
+ }
383
+ break;
384
+ case 'regName':
385
+ if(is_array($filter['value'])){
386
+ foreach ($filter['value'] as $value){
387
+ $regName[] = $filter['value'];
388
+ }
389
+ } else {
390
+ $regName[] = $filter['value'];
391
+ }
392
+ break;
393
+ }
394
+ }
395
+ $filter = array();
396
+ if(count($exts)>0) {
397
+ $filter['extension'] = $exts;
398
+ } else {
399
+ $filter['extension'] = null;
400
+ }
401
+ if(count($names)>0) {
402
+ $filter['name']=$names;
403
+ } else {
404
+ $filter['name']=null;
405
+ }
406
+ if(count($regName)>0) {
407
+
408
+ $filter['regName']=$regName;
409
+ } else {
410
+ $filter['regName']=null;
411
+ }
412
+ $this->setFilesFilter($filter);
413
+ }
414
+ /**
415
+ * add filter
416
+ * @return none
417
+ */
418
+ public function addFilter($field, $value)
419
+ {
420
+ $filter = array();
421
+ $filter['field'] = $field;
422
+ $filter['value'] = $value;
423
+ $this->_filters[] = $filter;
424
+ $this->_isFiltersRendered = false;
425
+ $this->walk('addFilter',array($field, $value));
426
+ return $this;
427
+ }
428
+ }
429
+
430
+
431
+ /* Example */
432
+ /*
433
+ $a = new Varien_Directory_Collection('/usr/home/vasily/dev/magento/lib',false);
434
+
435
+ $a->addFilter("extension","php");
436
+
437
+ $a->useFilter(true);
438
+
439
+ print "-----------------------\n";
440
+ print_r($a->filesName());
441
+
442
+ $a->setPath('/usr/home/vasily/dev/magento/lib/Varien/Image',true);
443
+ $a->useFilter(true);
444
+
445
+ print "-----------------------\n";
446
+ print_r($a->filesName());
447
+
448
+ print "-----------------------\n";
449
+ $filesObj = $a->filesObj();
450
+ print $filesObj[0]->fgets();
451
+ print $filesObj[0]->fgets();
452
+ print $filesObj[0]->fgets();
453
+ print $filesObj[0]->fgets();
454
+ print $filesObj[0]->fgets();
455
+ print $filesObj[0]->fgets();
456
+
457
+ */
458
+
459
+
460
+
461
+ ?>
lib/Varien/Directory/Factory.php ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Directory
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Directory Factory
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Directory
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ require_once("Varien/Directory/Collection.php");
36
+ require_once("Varien/File/Object.php");
37
+
38
+ class Varien_Directory_Factory{
39
+ /**
40
+ * return or Varien_Directory_Collection or Varien_File_Object object
41
+ *
42
+ * @param array $path - path to direcctory
43
+ * @param bool $is_recursion - use or not recursion
44
+ * @param int $recurse_level - recurse level
45
+ * @return IFactor - Varien_Directory_Collection or Varien_File_Object object
46
+ */
47
+ static public function getFactory($path,$is_recursion = true,$recurse_level=0)
48
+ {
49
+ if(is_dir($path)){
50
+ $obj = new Varien_Directory_Collection($path,$is_recursion,$recurse_level+1);
51
+ return $obj;
52
+ } else {
53
+ return new Varien_File_Object($path);
54
+ }
55
+ }
56
+
57
+ }
58
+ ?>
lib/Varien/Directory/IFactory.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Directory
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Factory Interface for Directory
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Directory
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ interface IFactory{
36
+
37
+ public function getFilesName(&$files);
38
+ public function getFilesPaths(&$paths);
39
+ public function getFilesObj(&$objs);
40
+ public function useFilter($useFilter);
41
+ public function getDirsName(&$dirs);
42
+ public function toArray(&$arr);
43
+ public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct');
44
+ }
lib/Varien/Directory/a.txt ADDED
@@ -0,0 +1,1352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Struct>
3
+ <Zend>
4
+ <Log>
5
+ <Formatter>
6
+ </Formatter>
7
+ <Filter>
8
+ </Filter>
9
+ <Writer>
10
+ </Writer>
11
+ </Log>
12
+ <Mail>
13
+ <Protocol>
14
+ <Smtp>
15
+ <Auth>
16
+ </Auth>
17
+ </Smtp>
18
+ </Protocol>
19
+ <Storage>
20
+ <Writable>
21
+ </Writable>
22
+ <Folder>
23
+ </Folder>
24
+ </Storage>
25
+ <Transport>
26
+ </Transport>
27
+ </Mail>
28
+ <Session>
29
+ <Validator>
30
+ </Validator>
31
+ <SaveHandler>
32
+ </SaveHandler>
33
+ </Session>
34
+ <Filter>
35
+ </Filter>
36
+ <Mime>
37
+ </Mime>
38
+ <Request>
39
+ </Request>
40
+ <Service>
41
+ <Yahoo>
42
+ </Yahoo>
43
+ <Delicious>
44
+ </Delicious>
45
+ <Amazon>
46
+ </Amazon>
47
+ <StrikeIron>
48
+ </StrikeIron>
49
+ <Simpy>
50
+ </Simpy>
51
+ <Flickr>
52
+ </Flickr>
53
+ </Service>
54
+ <Measure>
55
+ <Cooking>
56
+ </Cooking>
57
+ <Viscosity>
58
+ </Viscosity>
59
+ <Flow>
60
+ </Flow>
61
+ <fileName>Current.php</fileName>
62
+ </Measure>
63
+ <Console>
64
+ <Getopt>
65
+ </Getopt>
66
+ </Console>
67
+ <Feed>
68
+ <Entry>
69
+ </Entry>
70
+ <Builder>
71
+ <Header>
72
+ </Header>
73
+ </Builder>
74
+ </Feed>
75
+ <Config>
76
+ </Config>
77
+ <XmlRpc>
78
+ <Server>
79
+ </Server>
80
+ <Request>
81
+ </Request>
82
+ <Value>
83
+ </Value>
84
+ <Client>
85
+ </Client>
86
+ <Response>
87
+ </Response>
88
+ <fileName>Value.php</fileName>
89
+ </XmlRpc>
90
+ <Pdf>
91
+ <Cmap>
92
+ <ByteEncoding>
93
+ </ByteEncoding>
94
+ </Cmap>
95
+ <ElementFactory>
96
+ </ElementFactory>
97
+ <Filter>
98
+ <Compression>
99
+ </Compression>
100
+ </Filter>
101
+ <Resource>
102
+ <Image>
103
+ </Image>
104
+ <Font>
105
+ <Standard>
106
+ </Standard>
107
+ <OpenType>
108
+ </OpenType>
109
+ </Font>
110
+ </Resource>
111
+ <Element>
112
+ <Object>
113
+ </Object>
114
+ <String>
115
+ </String>
116
+ <Reference>
117
+ </Reference>
118
+ </Element>
119
+ <FileParser>
120
+ <Image>
121
+ </Image>
122
+ <Font>
123
+ <OpenType>
124
+ </OpenType>
125
+ </Font>
126
+ </FileParser>
127
+ <Parser>
128
+ </Parser>
129
+ <Trailer>
130
+ </Trailer>
131
+ <FileParserDataSource>
132
+ </FileParserDataSource>
133
+ <Color>
134
+ </Color>
135
+ </Pdf>
136
+ <Http>
137
+ <Client>
138
+ <Adapter>
139
+ </Adapter>
140
+ </Client>
141
+ </Http>
142
+ <Gdata>
143
+ <Media>
144
+ <Extension>
145
+ </Extension>
146
+ </Media>
147
+ <App>
148
+ <Extension>
149
+ </Extension>
150
+ </App>
151
+ <Gbase>
152
+ <Extension>
153
+ </Extension>
154
+ </Gbase>
155
+ <Kind>
156
+ </Kind>
157
+ <Spreadsheets>
158
+ <Extension>
159
+ </Extension>
160
+ </Spreadsheets>
161
+ <YouTube>
162
+ <Extension>
163
+ </Extension>
164
+ </YouTube>
165
+ <Calendar>
166
+ <Extension>
167
+ </Extension>
168
+ </Calendar>
169
+ <Gapps>
170
+ <Extension>
171
+ </Extension>
172
+ </Gapps>
173
+ <Extension>
174
+ </Extension>
175
+ </Gdata>
176
+ <Controller>
177
+ <Router>
178
+ <Route>
179
+ </Route>
180
+ </Router>
181
+ <Dispatcher>
182
+ </Dispatcher>
183
+ <Request>
184
+ </Request>
185
+ <Action>
186
+ <Helper>
187
+ </Helper>
188
+ </Action>
189
+ <Plugin>
190
+ </Plugin>
191
+ <Response>
192
+ </Response>
193
+ </Controller>
194
+ <Db>
195
+ <Select>
196
+ </Select>
197
+ <Adapter>
198
+ <Pdo>
199
+ <Ibm>
200
+ </Ibm>
201
+ </Pdo>
202
+ <Oracle>
203
+ </Oracle>
204
+ <Db2>
205
+ </Db2>
206
+ <Mysqli>
207
+ </Mysqli>
208
+ </Adapter>
209
+ <Profiler>
210
+ </Profiler>
211
+ <Statement>
212
+ <Pdo>
213
+ </Pdo>
214
+ <Oracle>
215
+ </Oracle>
216
+ <Db2>
217
+ </Db2>
218
+ <Mysqli>
219
+ </Mysqli>
220
+ </Statement>
221
+ <Table>
222
+ <Rowset>
223
+ </Rowset>
224
+ <Row>
225
+ </Row>
226
+ </Table>
227
+ </Db>
228
+ <Validate>
229
+ <Hostname>
230
+ </Hostname>
231
+ </Validate>
232
+ <Currency>
233
+ </Currency>
234
+ <Translate>
235
+ <Adapter>
236
+ </Adapter>
237
+ </Translate>
238
+ <Locale>
239
+ <Data>
240
+ </Data>
241
+ <Math>
242
+ </Math>
243
+ </Locale>
244
+ <Acl>
245
+ <Assert>
246
+ </Assert>
247
+ <Role>
248
+ <Registry>
249
+ </Registry>
250
+ </Role>
251
+ <Resource>
252
+ </Resource>
253
+ </Acl>
254
+ <Uri>
255
+ </Uri>
256
+ <Auth>
257
+ <Adapter>
258
+ <Http>
259
+ <Resolver>
260
+ </Resolver>
261
+ </Http>
262
+ </Adapter>
263
+ <Storage>
264
+ </Storage>
265
+ </Auth>
266
+ <Cache>
267
+ <Frontend>
268
+ </Frontend>
269
+ <Backend>
270
+ </Backend>
271
+ </Cache>
272
+ <Search>
273
+ <Lucene>
274
+ <Analysis>
275
+ <Analyzer>
276
+ <Common>
277
+ <Text>
278
+ </Text>
279
+ <TextNum>
280
+ </TextNum>
281
+ </Common>
282
+ </Analyzer>
283
+ <TokenFilter>
284
+ </TokenFilter>
285
+ </Analysis>
286
+ <Storage>
287
+ <File>
288
+ </File>
289
+ <Directory>
290
+ </Directory>
291
+ </Storage>
292
+ <Search>
293
+ <Query>
294
+ </Query>
295
+ <Similarity>
296
+ </Similarity>
297
+ <QueryEntry>
298
+ </QueryEntry>
299
+ <Weight>
300
+ </Weight>
301
+ </Search>
302
+ <Index>
303
+ <SegmentWriter>
304
+ </SegmentWriter>
305
+ </Index>
306
+ <Document>
307
+ </Document>
308
+ </Lucene>
309
+ </Search>
310
+ <Server>
311
+ <Reflection>
312
+ <Function>
313
+ </Function>
314
+ </Reflection>
315
+ </Server>
316
+ <Memory>
317
+ <Container>
318
+ </Container>
319
+ <fileName>Value.php</fileName>
320
+ </Memory>
321
+ <Json>
322
+ </Json>
323
+ <View>
324
+ <Helper>
325
+ </Helper>
326
+ <Filter>
327
+ </Filter>
328
+ </View>
329
+ <Date>
330
+ </Date>
331
+ <Rest>
332
+ <Server>
333
+ </Server>
334
+ <Client>
335
+ </Client>
336
+ </Rest>
337
+ <fileName>Mail.php</fileName>
338
+ </Zend>
339
+ </lib>
340
+ </Struct>
341
+ Array
342
+ (
343
+ [tmp] => Array
344
+ (
345
+ )
346
+
347
+ [Varien] => Array
348
+ (
349
+ [Http] => Array
350
+ (
351
+ [Adapter] => Array
352
+ (
353
+ )
354
+
355
+ )
356
+
357
+ [File] => Array
358
+ (
359
+ [Uploader] => Array
360
+ (
361
+ )
362
+
363
+ )
364
+
365
+ [Convert] => Array
366
+ (
367
+ [Adapter] => Array
368
+ (
369
+ [Io] => Array
370
+ (
371
+ )
372
+
373
+ )
374
+
375
+ [Container] => Array
376
+ (
377
+ [Grid] => Array
378
+ (
379
+ )
380
+
381
+ )
382
+
383
+ [Mapper] => Array
384
+ (
385
+ )
386
+
387
+ [Validator] => Array
388
+ (
389
+ )
390
+
391
+ )
392
+
393
+ [Event] => Array
394
+ (
395
+ [Observer] => Array
396
+ (
397
+ )
398
+
399
+ )
400
+
401
+ [Image] => Array
402
+ (
403
+ [Adapter] => Array
404
+ (
405
+ )
406
+
407
+ )
408
+
409
+ [Controller] => Array
410
+ (
411
+ [Router] => Array
412
+ (
413
+ [Route] => Array
414
+ (
415
+ )
416
+
417
+ )
418
+
419
+ [Dispatcher] => Array
420
+ (
421
+ )
422
+
423
+ [Plugin] => Array
424
+ (
425
+ )
426
+
427
+ )
428
+
429
+ [Filter] => Array
430
+ (
431
+ [Object] => Array
432
+ (
433
+ )
434
+
435
+ [Template] => Array
436
+ (
437
+ [Tokenizer] => Array
438
+ (
439
+ )
440
+
441
+ )
442
+
443
+ [Array] => Array
444
+ (
445
+ )
446
+
447
+ )
448
+
449
+ [Db] => Array
450
+ (
451
+ [Tree] => Array
452
+ (
453
+ [NodeSet] => Array
454
+ (
455
+ )
456
+
457
+ [Node] => Array
458
+ (
459
+ )
460
+
461
+ )
462
+
463
+ [Adapter] => Array
464
+ (
465
+ [Pdo] => Array
466
+ (
467
+ )
468
+
469
+ )
470
+
471
+ )
472
+
473
+ [Crypt] => Array
474
+ (
475
+ )
476
+
477
+ [Io] => Array
478
+ (
479
+ )
480
+
481
+ [Data] => Array
482
+ (
483
+ [Tree] => Array
484
+ (
485
+ [Node] => Array
486
+ (
487
+ )
488
+
489
+ )
490
+
491
+ [Form] => Array
492
+ (
493
+ [Element] => Array
494
+ (
495
+ [Renderer] => Array
496
+ (
497
+ )
498
+
499
+ )
500
+
501
+ )
502
+
503
+ [Collection] => Array
504
+ (
505
+ )
506
+
507
+ )
508
+
509
+ [Simplexml] => Array
510
+ (
511
+ [Config] => Array
512
+ (
513
+ [Cache] => Array
514
+ (
515
+ )
516
+
517
+ )
518
+
519
+ )
520
+
521
+ [Directory] => Array
522
+ (
523
+ )
524
+
525
+ )
526
+
527
+ [PHPUnit] => Array
528
+ (
529
+ [Extensions] => Array
530
+ (
531
+ )
532
+
533
+ [Tests] => Array
534
+ (
535
+ [Extensions] => Array
536
+ (
537
+ )
538
+
539
+ [_files] => Array
540
+ (
541
+ )
542
+
543
+ [Runner] => Array
544
+ (
545
+ )
546
+
547
+ [Framework] => Array
548
+ (
549
+ )
550
+
551
+ [Util] => Array
552
+ (
553
+ [TestDox] => Array
554
+ (
555
+ )
556
+
557
+ )
558
+
559
+ )
560
+
561
+ [TextUI] => Array
562
+ (
563
+ )
564
+
565
+ [Samples] => Array
566
+ (
567
+ [Money] => Array
568
+ (
569
+ )
570
+
571
+ [BankAccount] => Array
572
+ (
573
+ )
574
+
575
+ )
576
+
577
+ [Runner] => Array
578
+ (
579
+ )
580
+
581
+ [Framework] => Array
582
+ (
583
+ [MockObject] => Array
584
+ (
585
+ [Matcher] => Array
586
+ (
587
+ )
588
+
589
+ [Builder] => Array
590
+ (
591
+ )
592
+
593
+ [Stub] => Array
594
+ (
595
+ )
596
+
597
+ )
598
+
599
+ [ComparisonFailure] => Array
600
+ (
601
+ )
602
+
603
+ [Constraint] => Array
604
+ (
605
+ )
606
+
607
+ )
608
+
609
+ [Util] => Array
610
+ (
611
+ [Log] => Array
612
+ (
613
+ )
614
+
615
+ [Skeleton] => Array
616
+ (
617
+ )
618
+
619
+ [TestDox] => Array
620
+ (
621
+ [ResultPrinter] => Array
622
+ (
623
+ )
624
+
625
+ )
626
+
627
+ [Database] => Array
628
+ (
629
+ )
630
+
631
+ [Report] => Array
632
+ (
633
+ [Test] => Array
634
+ (
635
+ [Node] => Array
636
+ (
637
+ )
638
+
639
+ )
640
+
641
+ [Coverage] => Array
642
+ (
643
+ [Node] => Array
644
+ (
645
+ )
646
+
647
+ )
648
+
649
+ [Template] => Array
650
+ (
651
+ )
652
+
653
+ )
654
+
655
+ )
656
+
657
+ )
658
+
659
+ [Zend] => Array
660
+ (
661
+ [Log] => Array
662
+ (
663
+ [Formatter] => Array
664
+ (
665
+ )
666
+
667
+ [Filter] => Array
668
+ (
669
+ )
670
+
671
+ [Writer] => Array
672
+ (
673
+ )
674
+
675
+ )
676
+
677
+ [Mail] => Array
678
+ (
679
+ [Protocol] => Array
680
+ (
681
+ [Smtp] => Array
682
+ (
683
+ [Auth] => Array
684
+ (
685
+ )
686
+
687
+ )
688
+
689
+ )
690
+
691
+ [Storage] => Array
692
+ (
693
+ [Writable] => Array
694
+ (
695
+ )
696
+
697
+ [Folder] => Array
698
+ (
699
+ )
700
+
701
+ )
702
+
703
+ [Transport] => Array
704
+ (
705
+ )
706
+
707
+ )
708
+
709
+ [Session] => Array
710
+ (
711
+ [Validator] => Array
712
+ (
713
+ )
714
+
715
+ [SaveHandler] => Array
716
+ (
717
+ )
718
+
719
+ )
720
+
721
+ [Filter] => Array
722
+ (
723
+ )
724
+
725
+ [Mime] => Array
726
+ (
727
+ )
728
+
729
+ [Request] => Array
730
+ (
731
+ )
732
+
733
+ [Service] => Array
734
+ (
735
+ [Yahoo] => Array
736
+ (
737
+ )
738
+
739
+ [Delicious] => Array
740
+ (
741
+ )
742
+
743
+ [Amazon] => Array
744
+ (
745
+ )
746
+
747
+ [StrikeIron] => Array
748
+ (
749
+ )
750
+
751
+ [Simpy] => Array
752
+ (
753
+ )
754
+
755
+ [Flickr] => Array
756
+ (
757
+ )
758
+
759
+ )
760
+
761
+ [Measure] => Array
762
+ (
763
+ [Cooking] => Array
764
+ (
765
+ )
766
+
767
+ [Viscosity] => Array
768
+ (
769
+ )
770
+
771
+ [Flow] => Array
772
+ (
773
+ )
774
+
775
+ [files_in_dirs] => Array
776
+ (
777
+ [0] => Current.php
778
+ )
779
+
780
+ )
781
+
782
+ [Console] => Array
783
+ (
784
+ [Getopt] => Array
785
+ (
786
+ )
787
+
788
+ )
789
+
790
+ [Feed] => Array
791
+ (
792
+ [Entry] => Array
793
+ (
794
+ )
795
+
796
+ [Builder] => Array
797
+ (
798
+ [Header] => Array
799
+ (
800
+ )
801
+
802
+ )
803
+
804
+ )
805
+
806
+ [Config] => Array
807
+ (
808
+ )
809
+
810
+ [XmlRpc] => Array
811
+ (
812
+ [Server] => Array
813
+ (
814
+ )
815
+
816
+ [Request] => Array
817
+ (
818
+ )
819
+
820
+ [Value] => Array
821
+ (
822
+ )
823
+
824
+ [Client] => Array
825
+ (
826
+ )
827
+
828
+ [Response] => Array
829
+ (
830
+ )
831
+
832
+ [files_in_dirs] => Array
833
+ (
834
+ [0] => Value.php
835
+ )
836
+
837
+ )
838
+
839
+ [Pdf] => Array
840
+ (
841
+ [Cmap] => Array
842
+ (
843
+ [ByteEncoding] => Array
844
+ (
845
+ )
846
+
847
+ )
848
+
849
+ [ElementFactory] => Array
850
+ (
851
+ )
852
+
853
+ [Filter] => Array
854
+ (
855
+ [Compression] => Array
856
+ (
857
+ )
858
+
859
+ )
860
+
861
+ [Resource] => Array
862
+ (
863
+ [Image] => Array
864
+ (
865
+ )
866
+
867
+ [Font] => Array
868
+ (
869
+ [Standard] => Array
870
+ (
871
+ )
872
+
873
+ [OpenType] => Array
874
+ (
875
+ )
876
+
877
+ )
878
+
879
+ )
880
+
881
+ [Element] => Array
882
+ (
883
+ [Object] => Array
884
+ (
885
+ )
886
+
887
+ [String] => Array
888
+ (
889
+ )
890
+
891
+ [Reference] => Array
892
+ (
893
+ )
894
+
895
+ )
896
+
897
+ [FileParser] => Array
898
+ (
899
+ [Image] => Array
900
+ (
901
+ )
902
+
903
+ [Font] => Array
904
+ (
905
+ [OpenType] => Array
906
+ (
907
+ )
908
+
909
+ )
910
+
911
+ )
912
+
913
+ [Parser] => Array
914
+ (
915
+ )
916
+
917
+ [Trailer] => Array
918
+ (
919
+ )
920
+
921
+ [FileParserDataSource] => Array
922
+ (
923
+ )
924
+
925
+ [Color] => Array
926
+ (
927
+ )
928
+
929
+ )
930
+
931
+ [Http] => Array
932
+ (
933
+ [Client] => Array
934
+ (
935
+ [Adapter] => Array
936
+ (
937
+ )
938
+
939
+ )
940
+
941
+ )
942
+
943
+ [Gdata] => Array
944
+ (
945
+ [Media] => Array
946
+ (
947
+ [Extension] => Array
948
+ (
949
+ )
950
+
951
+ )
952
+
953
+ [App] => Array
954
+ (
955
+ [Extension] => Array
956
+ (
957
+ )
958
+
959
+ )
960
+
961
+ [Gbase] => Array
962
+ (
963
+ [Extension] => Array
964
+ (
965
+ )
966
+
967
+ )
968
+
969
+ [Kind] => Array
970
+ (
971
+ )
972
+
973
+ [Spreadsheets] => Array
974
+ (
975
+ [Extension] => Array
976
+ (
977
+ )
978
+
979
+ )
980
+
981
+ [YouTube] => Array
982
+ (
983
+ [Extension] => Array
984
+ (
985
+ )
986
+
987
+ )
988
+
989
+ [Calendar] => Array
990
+ (
991
+ [Extension] => Array
992
+ (
993
+ )
994
+
995
+ )
996
+
997
+ [Gapps] => Array
998
+ (
999
+ [Extension] => Array
1000
+ (
1001
+ )
1002
+
1003
+ )
1004
+
1005
+ [Extension] => Array
1006
+ (
1007
+ )
1008
+
1009
+ )
1010
+
1011
+ [Controller] => Array
1012
+ (
1013
+ [Router] => Array
1014
+ (
1015
+ [Route] => Array
1016
+ (
1017
+ )
1018
+
1019
+ )
1020
+
1021
+ [Dispatcher] => Array
1022
+ (
1023
+ )
1024
+
1025
+ [Request] => Array
1026
+ (
1027
+ )
1028
+
1029
+ [Action] => Array
1030
+ (
1031
+ [Helper] => Array
1032
+ (
1033
+ )
1034
+
1035
+ )
1036
+
1037
+ [Plugin] => Array
1038
+ (
1039
+ )
1040
+
1041
+ [Response] => Array
1042
+ (
1043
+ )
1044
+
1045
+ )
1046
+
1047
+ [Db] => Array
1048
+ (
1049
+ [Select] => Array
1050
+ (
1051
+ )
1052
+
1053
+ [Adapter] => Array
1054
+ (
1055
+ [Pdo] => Array
1056
+ (
1057
+ [Ibm] => Array
1058
+ (
1059
+ )
1060
+
1061
+ )
1062
+
1063
+ [Oracle] => Array
1064
+ (
1065
+ )
1066
+
1067
+ [Db2] => Array
1068
+ (
1069
+ )
1070
+
1071
+ [Mysqli] => Array
1072
+ (
1073
+ )
1074
+
1075
+ )
1076
+
1077
+ [Profiler] => Array
1078
+ (
1079
+ )
1080
+
1081
+ [Statement] => Array
1082
+ (
1083
+ [Pdo] => Array
1084
+ (
1085
+ )
1086
+
1087
+ [Oracle] => Array
1088
+ (
1089
+ )
1090
+
1091
+ [Db2] => Array
1092
+ (
1093
+ )
1094
+
1095
+ [Mysqli] => Array
1096
+ (
1097
+ )
1098
+
1099
+ )
1100
+
1101
+ [Table] => Array
1102
+ (
1103
+ [Rowset] => Array
1104
+ (
1105
+ )
1106
+
1107
+ [Row] => Array
1108
+ (
1109
+ )
1110
+
1111
+ )
1112
+
1113
+ )
1114
+
1115
+ [Validate] => Array
1116
+ (
1117
+ [Hostname] => Array
1118
+ (
1119
+ )
1120
+
1121
+ )
1122
+
1123
+ [Currency] => Array
1124
+ (
1125
+ )
1126
+
1127
+ [Translate] => Array
1128
+ (
1129
+ [Adapter] => Array
1130
+ (
1131
+ )
1132
+
1133
+ )
1134
+
1135
+ [Locale] => Array
1136
+ (
1137
+ [Data] => Array
1138
+ (
1139
+ )
1140
+
1141
+ [Math] => Array
1142
+ (
1143
+ )
1144
+
1145
+ )
1146
+
1147
+ [Acl] => Array
1148
+ (
1149
+ [Assert] => Array
1150
+ (
1151
+ )
1152
+
1153
+ [Role] => Array
1154
+ (
1155
+ [Registry] => Array
1156
+ (
1157
+ )
1158
+
1159
+ )
1160
+
1161
+ [Resource] => Array
1162
+ (
1163
+ )
1164
+
1165
+ )
1166
+
1167
+ [Uri] => Array
1168
+ (
1169
+ )
1170
+
1171
+ [Auth] => Array
1172
+ (
1173
+ [Adapter] => Array
1174
+ (
1175
+ [Http] => Array
1176
+ (
1177
+ [Resolver] => Array
1178
+ (
1179
+ )
1180
+
1181
+ )
1182
+
1183
+ )
1184
+
1185
+ [Storage] => Array
1186
+ (
1187
+ )
1188
+
1189
+ )
1190
+
1191
+ [Cache] => Array
1192
+ (
1193
+ [Frontend] => Array
1194
+ (
1195
+ )
1196
+
1197
+ [Backend] => Array
1198
+ (
1199
+ )
1200
+
1201
+ )
1202
+
1203
+ [Search] => Array
1204
+ (
1205
+ [Lucene] => Array
1206
+ (
1207
+ [Analysis] => Array
1208
+ (
1209
+ [Analyzer] => Array
1210
+ (
1211
+ [Common] => Array
1212
+ (
1213
+ [Text] => Array
1214
+ (
1215
+ )
1216
+
1217
+ [TextNum] => Array
1218
+ (
1219
+ )
1220
+
1221
+ )
1222
+
1223
+ )
1224
+
1225
+ [TokenFilter] => Array
1226
+ (
1227
+ )
1228
+
1229
+ )
1230
+
1231
+ [Storage] => Array
1232
+ (
1233
+ [File] => Array
1234
+ (
1235
+ )
1236
+
1237
+ [Directory] => Array
1238
+ (
1239
+ )
1240
+
1241
+ )
1242
+
1243
+ [Search] => Array
1244
+ (
1245
+ [Query] => Array
1246
+ (
1247
+ )
1248
+
1249
+ [Similarity] => Array
1250
+ (
1251
+ )
1252
+
1253
+ [QueryEntry] => Array
1254
+ (
1255
+ )
1256
+
1257
+ [Weight] => Array
1258
+ (
1259
+ )
1260
+
1261
+ )
1262
+
1263
+ [Index] => Array
1264
+ (
1265
+ [SegmentWriter] => Array
1266
+ (
1267
+ )
1268
+
1269
+ )
1270
+
1271
+ [Document] => Array
1272
+ (
1273
+ )
1274
+
1275
+ )
1276
+
1277
+ )
1278
+
1279
+ [Server] => Array
1280
+ (
1281
+ [Reflection] => Array
1282
+ (
1283
+ [Function] => Array
1284
+ (
1285
+ )
1286
+
1287
+ )
1288
+
1289
+ )
1290
+
1291
+ [Memory] => Array
1292
+ (
1293
+ [Container] => Array
1294
+ (
1295
+ )
1296
+
1297
+ [files_in_dirs] => Array
1298
+ (
1299
+ [0] => Value.php
1300
+ )
1301
+
1302
+ )
1303
+
1304
+ [Json] => Array
1305
+ (
1306
+ )
1307
+
1308
+ [View] => Array
1309
+ (
1310
+ [Helper] => Array
1311
+ (
1312
+ )
1313
+
1314
+ [Filter] => Array
1315
+ (
1316
+ )
1317
+
1318
+ )
1319
+
1320
+ [Date] => Array
1321
+ (
1322
+ )
1323
+
1324
+ [Rest] => Array
1325
+ (
1326
+ [Server] => Array
1327
+ (
1328
+ )
1329
+
1330
+ [Client] => Array
1331
+ (
1332
+ )
1333
+
1334
+ )
1335
+
1336
+ [files_in_dirs] => Array
1337
+ (
1338
+ [0] => Mail.php
1339
+ )
1340
+
1341
+ )
1342
+
1343
+ )
1344
+
1345
+ 4254
1346
+ <?php
1347
+ 13730
1348
+ <?php
1349
+ 4056
1350
+ <?php
1351
+ 16349
1352
+ <?php
lib/Varien/Event.php ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Event object and dispatcher
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event extends Varien_Object
36
+ {
37
+ /**
38
+ * Observers collection
39
+ *
40
+ * @var Varien_Event_Observer_Collection
41
+ */
42
+ protected $_observers;
43
+
44
+ /**
45
+ * Constructor
46
+ *
47
+ * Initializes observers collection
48
+ *
49
+ * @param array $data
50
+ */
51
+ public function __construct(array $data=array())
52
+ {
53
+ $this->_observers = new Varien_Event_Observer_Collection();
54
+ parent::__construct($data);
55
+ }
56
+
57
+ /**
58
+ * Returns all the registered observers for the event
59
+ *
60
+ * @return Varien_Event_Observer_Collection
61
+ */
62
+ public function getObservers()
63
+ {
64
+ return $this->_observers;
65
+ }
66
+
67
+ /**
68
+ * Register an observer for the event
69
+ *
70
+ * @param Varien_Event_Observer $observer
71
+ * @return Varien_Event
72
+ */
73
+ public function addObserver(Varien_Event_Observer $observer)
74
+ {
75
+ $this->getObservers()->addObserver($observer);
76
+ return $this;
77
+ }
78
+
79
+ /**
80
+ * Removes an observer by its name
81
+ *
82
+ * @param string $observerName
83
+ * @return Varien_Event
84
+ */
85
+ public function removeObserverByName($observerName)
86
+ {
87
+ $this->getObservers()->removeObserverByName($observerName);
88
+ return $this;
89
+ }
90
+
91
+ /**
92
+ * Dispatches the event to registered observers
93
+ *
94
+ * @return Varien_Event
95
+ */
96
+ public function dispatch()
97
+ {
98
+ $this->getObservers()->dispatch($this);
99
+ return $this;
100
+ }
101
+
102
+ /**
103
+ * Retrieve event name
104
+ *
105
+ * @return string
106
+ */
107
+ public function getName()
108
+ {
109
+ return isset($this->_data['name']) ? $this->_data['name'] : null;
110
+ }
111
+
112
+ public function setName($data)
113
+ {
114
+ $this->_data['name'] = $data;
115
+ return $this;
116
+ }
117
+
118
+ public function getBlock()
119
+ {
120
+ return $this->_getData('block');
121
+ }
122
+ }
lib/Varien/Event/Collection.php ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Collection of events
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event_Collection
36
+ {
37
+ /**
38
+ * Array of events in the collection
39
+ *
40
+ * @var array
41
+ */
42
+ protected $_events;
43
+
44
+ /**
45
+ * Global observers
46
+ *
47
+ * For example regex observers will watch all events that
48
+ *
49
+ * @var Varien_Event_Observer_Collection
50
+ */
51
+ protected $_observers;
52
+
53
+ /**
54
+ * Initializes global observers collection
55
+ *
56
+ */
57
+ public function __construct()
58
+ {
59
+ $this->_events = array();
60
+ $this->_globalObservers = new Varien_Event_Observer_Collection();
61
+ }
62
+
63
+ /**
64
+ * Returns all registered events in collection
65
+ *
66
+ * @return array
67
+ */
68
+ public function getAllEvents()
69
+ {
70
+ return $this->_events;
71
+ }
72
+
73
+ /**
74
+ * Returns all registered global observers for the collection of events
75
+ *
76
+ * @return Varien_Event_Observer_Collection
77
+ */
78
+ public function getGlobalObservers()
79
+ {
80
+ return $this->_globalObservers;
81
+ }
82
+
83
+ /**
84
+ * Returns event by its name
85
+ *
86
+ * If event doesn't exist creates new one and returns it
87
+ *
88
+ * @param string $eventName
89
+ * @return Varien_Event
90
+ */
91
+ public function getEventByName($eventName)
92
+ {
93
+ if (!isset($this->_events[$eventName])) {
94
+ $this->addEvent(new Varien_Event(array('name'=>$eventName)));
95
+ }
96
+ return $this->_events[$eventName];
97
+ }
98
+
99
+ /**
100
+ * Register an event for this collection
101
+ *
102
+ * @param Varien_Event $event
103
+ * @return Varien_Event_Collection
104
+ */
105
+ public function addEvent(Varien_Event $event)
106
+ {
107
+ $this->_events[$event->getName()] = $event;
108
+ return $this;
109
+ }
110
+
111
+ /**
112
+ * Register an observer
113
+ *
114
+ * If observer has event_name property it will be regitered for this specific event.
115
+ * If not it will be registered as global observer
116
+ *
117
+ * @param Varien_Event_Observer $observer
118
+ * @return Varien_Event_Collection
119
+ */
120
+ public function addObserver(Varien_Event_Observer $observer)
121
+ {
122
+ $eventName = $observer->getEventName();
123
+ if ($eventName) {
124
+ $this->getEventByName($eventName)->addObserver($observer);
125
+ } else {
126
+ $this->getGlobalObservers()->addObserver($observer);
127
+ }
128
+ return $this;
129
+ }
130
+
131
+ /**
132
+ * Dispatch event name with optional data
133
+ *
134
+ * Will dispatch specific event and will try all global observers
135
+ *
136
+ * @param string $eventName
137
+ * @param array $data
138
+ * @return Varien_Event_Collection
139
+ */
140
+ public function dispatch($eventName, array $data=array())
141
+ {
142
+ $event = $this->getEventByName($eventName);
143
+ $event->addData($data)->dispatch();
144
+ $this->getGlobalObservers()->dispatch($event);
145
+ return $this;
146
+ }
147
+ }
lib/Varien/Event/Observer.php ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Event observer object
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event_Observer extends Varien_Object
36
+ {
37
+ /**
38
+ * Checkes the observer's event_regex against event's name
39
+ *
40
+ * @param Varien_Event $event
41
+ * @return boolean
42
+ */
43
+ public function isValidFor(Varien_Event $event)
44
+ {
45
+ return $this->getEventName()===$event->getName();
46
+ }
47
+
48
+ /**
49
+ * Dispatches an event to observer's callback
50
+ *
51
+ * @param Varien_Event $event
52
+ * @return Varien_Event_Observer
53
+ */
54
+ public function dispatch(Varien_Event $event)
55
+ {
56
+ if (!$this->isValidFor($event)) {
57
+ return $this;
58
+ }
59
+
60
+ $callback = $this->getCallback();
61
+ $this->setEvent($event);
62
+
63
+ $_profilerKey = 'OBSERVER: '.(is_object($callback[0]) ? get_class($callback[0]) : (string)$callback[0]).' -> '.$callback[1];
64
+ Varien_Profiler::start($_profilerKey);
65
+ call_user_func($callback, $this);
66
+ Varien_Profiler::stop($_profilerKey);
67
+
68
+ return $this;
69
+ }
70
+
71
+ public function getName()
72
+ {
73
+ return $this->getData('name');
74
+ }
75
+
76
+ public function setName($data)
77
+ {
78
+ return $this->setData('name', $data);
79
+ }
80
+
81
+ public function getEventName()
82
+ {
83
+ return $this->getData('event_name');
84
+ }
85
+
86
+ public function setEventName($data)
87
+ {
88
+ return $this->setData('event_name', $data);
89
+ }
90
+
91
+ public function getCallback()
92
+ {
93
+ return $this->getData('callback');
94
+ }
95
+
96
+ public function setCallback($data)
97
+ {
98
+ return $this->setData('callback', $data);
99
+ }
100
+
101
+ /**
102
+ * Get observer event object
103
+ *
104
+ * @return Varien_Event
105
+ */
106
+ public function getEvent()
107
+ {
108
+ return $this->getData('event');
109
+ }
110
+
111
+ public function setEvent($data)
112
+ {
113
+ return $this->setData('event', $data);
114
+ }
115
+ }
lib/Varien/Event/Observer/Collection.php ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Event observer collection
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event_Observer_Collection
36
+ {
37
+ /**
38
+ * Array of observers
39
+ *
40
+ * @var array
41
+ */
42
+ protected $_observers;
43
+
44
+ /**
45
+ * Initializes observers
46
+ *
47
+ */
48
+ public function __construct()
49
+ {
50
+ $this->_observers = array();
51
+ }
52
+
53
+ /**
54
+ * Returns all observers in the collection
55
+ *
56
+ * @return array
57
+ */
58
+ public function getAllObservers()
59
+ {
60
+ return $this->_observers;
61
+ }
62
+
63
+ /**
64
+ * Returns observer by its name
65
+ *
66
+ * @param string $observerName
67
+ * @return Varien_Event_Observer
68
+ */
69
+ public function getObserverByName($observerName)
70
+ {
71
+ return $this->_observers[$observerName];
72
+ }
73
+
74
+ /**
75
+ * Adds an observer to the collection
76
+ *
77
+ * @param Varien_Event_Observer $observer
78
+ * @return Varien_Event_Observer_Collection
79
+ */
80
+ public function addObserver(Varien_Event_Observer $observer)
81
+ {
82
+ $this->_observers[$observer->getName()] = $observer;
83
+ return $this;
84
+ }
85
+
86
+ /**
87
+ * Removes an observer from the collection by its name
88
+ *
89
+ * @param string $observerName
90
+ * @return Varien_Event_Observer_Collection
91
+ */
92
+ public function removeObserverByName($observerName)
93
+ {
94
+ unset($this->_observers[$observerName]);
95
+ return $this;
96
+ }
97
+
98
+ /**
99
+ * Dispatches an event to all observers in the collection
100
+ *
101
+ * @param Varien_Event $event
102
+ * @return Varien_Event_Observer_Collection
103
+ */
104
+ public function dispatch(Varien_Event $event)
105
+ {
106
+ foreach ($this->_observers as $observer) {
107
+ $observer->dispatch($event);
108
+ }
109
+ return $this;
110
+ }
111
+ }
lib/Varien/Event/Observer/Cron.php ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Event cron observer object
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event_Observer_Cron extends Varien_Event_Observer
36
+ {
37
+ /**
38
+ * Checkes the observer's cron string against event's name
39
+ *
40
+ * Supports $this->setCronExpr('* 0-5,10-59/5 2-10,15-25 january-june/2 mon-fri')
41
+ *
42
+ * @param Varien_Event $event
43
+ * @return boolean
44
+ */
45
+ public function isValidFor(Varien_Event $event)
46
+ {
47
+ $e = preg_split('#\s+#', $this->getCronExpr(), null, PREG_SPLIT_NO_EMPTY);
48
+ if (sizeof($e)!==5) {
49
+ return false;
50
+ }
51
+
52
+ $d = getdate($this->getNow());
53
+
54
+ return $this->matchCronExpression($e[0], $d['minutes'])
55
+ && $this->matchCronExpression($e[1], $d['hours'])
56
+ && $this->matchCronExpression($e[2], $d['mday'])
57
+ && $this->matchCronExpression($e[3], $d['mon'])
58
+ && $this->matchCronExpression($e[4], $d['wday']);
59
+ }
60
+
61
+ public function getNow()
62
+ {
63
+ if (!$this->hasNow()) {
64
+ $this->setNow(time());
65
+ }
66
+ return $this->getData('now');
67
+ }
68
+
69
+ public function matchCronExpression($expr, $num)
70
+ {
71
+ // handle ALL match
72
+ if ($expr==='*') {
73
+ return true;
74
+ }
75
+
76
+ // handle multiple options
77
+ if (strpos($expr,',')!==false) {
78
+ foreach (explode(',',$expr) as $e) {
79
+ if ($this->matchCronExpression($e, $num)) {
80
+ return true;
81
+ }
82
+ }
83
+ return false;
84
+ }
85
+
86
+ // handle modulus
87
+ if (strpos($expr,'/')!==false) {
88
+ $e = explode('/', $expr);
89
+ if (sizeof($e)!==2) {
90
+ return false;
91
+ }
92
+ $expr = $e[0];
93
+ $mod = $e[1];
94
+ if (!is_numeric($mod)) {
95
+ return false;
96
+ }
97
+ } else {
98
+ $mod = 1;
99
+ }
100
+
101
+ // handle range
102
+ if (strpos($expr,'-')!==false) {
103
+ $e = explode('-', $expr);
104
+ if (sizeof($e)!==2) {
105
+ return false;
106
+ }
107
+
108
+ $from = $this->getNumeric($e[0]);
109
+ $to = $this->getNumeric($e[1]);
110
+
111
+ return ($from!==false) && ($to!==false)
112
+ && ($num>=$from) && ($num<=$to) && ($num%$mod===0);
113
+ }
114
+
115
+ // handle regular token
116
+ $value = $this->getNumeric($expr);
117
+ return ($value!==false) && ($num==$value) && ($num%$mod===0);
118
+ }
119
+
120
+ public function getNumeric($value)
121
+ {
122
+ static $data = array(
123
+ 'jan'=>1,
124
+ 'feb'=>2,
125
+ 'mar'=>3,
126
+ 'apr'=>4,
127
+ 'may'=>5,
128
+ 'jun'=>6,
129
+ 'jul'=>7,
130
+ 'aug'=>8,
131
+ 'sep'=>9,
132
+ 'oct'=>10,
133
+ 'nov'=>11,
134
+ 'dec'=>12,
135
+
136
+ 'sun'=>0,
137
+ 'mon'=>1,
138
+ 'tue'=>2,
139
+ 'wed'=>3,
140
+ 'thu'=>4,
141
+ 'fri'=>5,
142
+ 'sat'=>6,
143
+ );
144
+
145
+ if (is_numeric($value)) {
146
+ return $value;
147
+ }
148
+
149
+ if (is_string($value)) {
150
+ $value = strtolower(substr($value,0,3));
151
+ if (isset($data[$value])) {
152
+ return $data[$value];
153
+ }
154
+ }
155
+
156
+ return false;
157
+ }
158
+ }
lib/Varien/Event/Observer/Regex.php ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Event
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Event regex observer object
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Event
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Event_Observer_Regex extends Varien_Event_Observer
36
+ {
37
+ /**
38
+ * Checkes the observer's event_regex against event's name
39
+ *
40
+ * @param Varien_Event $event
41
+ * @return boolean
42
+ */
43
+ public function isValidFor(Varien_Event $event)
44
+ {
45
+ return preg_match($this->getEventRegex(), $event->getName());
46
+ }
47
+ }
lib/Varien/Exception.php ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Exception
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Exception extends Exception
29
+ {
30
+ /**
31
+ * Check PCRE PREG error and throw exception
32
+ *
33
+ * @throws Varien_Exception
34
+ */
35
+ public static function processPcreError()
36
+ {
37
+ if (preg_last_error() != PREG_NO_ERROR) {
38
+ switch (preg_last_error()) {
39
+ case PREG_INTERNAL_ERROR:
40
+ throw new Varien_Exception('PCRE PREG internal error');
41
+ case PREG_BACKTRACK_LIMIT_ERROR:
42
+ throw new Varien_Exception('PCRE PREG Backtrack limit error');
43
+ case PREG_RECURSION_LIMIT_ERROR:
44
+ throw new Varien_Exception('PCRE PREG Recursion limit error');
45
+ case PREG_BAD_UTF8_ERROR:
46
+ throw new Varien_Exception('PCRE PREG Bad UTF-8 error');
47
+ case PREG_BAD_UTF8_OFFSET_ERROR:
48
+ throw new Varien_Exception('PCRE PREG Bad UTF-8 offset error');
49
+ }
50
+ }
51
+ }
52
+ }
lib/Varien/File/Csv.php ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Csv parse
29
+ *
30
+ * @author Magento Core Team <core@magentocommerce.com>
31
+ */
32
+ class Varien_File_Csv
33
+ {
34
+ protected $_lineLength= 0;
35
+ protected $_delimiter = ',';
36
+ protected $_enclosure = '"';
37
+
38
+ public function __construct()
39
+ {
40
+
41
+ }
42
+
43
+ /**
44
+ * Set max file line length
45
+ *
46
+ * @param int $length
47
+ * @return Varien_File_Csv
48
+ */
49
+ public function setLineLength($length)
50
+ {
51
+ $this->_lineLength = $length;
52
+ return $this;
53
+ }
54
+
55
+ /**
56
+ * Set CSV column delimiter
57
+ *
58
+ * @param string $delimiter
59
+ * @return Varien_File_Csv
60
+ */
61
+ public function setDelimiter($delimiter)
62
+ {
63
+ $this->_delimiter = $delimiter;
64
+ return $this;
65
+ }
66
+
67
+ /**
68
+ * Set CSV column value enclosure
69
+ *
70
+ * @param string $enclosure
71
+ * @return Varien_File_Csv
72
+ */
73
+ public function setEnclosure($enclosure)
74
+ {
75
+ $this->_enclosure = $enclosure;
76
+ return $this;
77
+ }
78
+
79
+ /**
80
+ * Retrieve CSV file data as array
81
+ *
82
+ * @param string $file
83
+ * @return array
84
+ */
85
+ public function getData($file)
86
+ {
87
+ $data = array();
88
+ if (!file_exists($file)) {
89
+ throw new Exception('File "'.$file.'" do not exists');
90
+ }
91
+
92
+ $fh = fopen($file, 'r');
93
+ while ($rowData = fgetcsv($fh, $this->_lineLength, $this->_delimiter, $this->_enclosure)) {
94
+ $data[] = $rowData;
95
+ }
96
+ fclose($fh);
97
+ return $data;
98
+ }
99
+
100
+ /**
101
+ * Retrieve CSV file data as pairs
102
+ *
103
+ * @param string $file
104
+ * @param int $keyIndex
105
+ * @param int $valueIndex
106
+ * @return array
107
+ */
108
+ public function getDataPairs($file, $keyIndex=0, $valueIndex=1)
109
+ {
110
+ $data = array();
111
+ $csvData = $this->getData($file);
112
+ foreach ($csvData as $rowData) {
113
+ if (isset($rowData[$keyIndex])) {
114
+ $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
115
+ }
116
+ }
117
+ return $data;
118
+ }
119
+
120
+ /**
121
+ * Saving data row array into file
122
+ *
123
+ * @param string $file
124
+ * @param array $data
125
+ * @return Varien_File_Csv
126
+ */
127
+ public function saveData($file, $data)
128
+ {
129
+ $fh = fopen($file, 'w');
130
+ foreach ($data as $dataRow) {
131
+ $this->fputcsv($fh, $dataRow, $this->_delimiter, $this->_enclosure);
132
+ }
133
+ fclose($fh);
134
+ return $this;
135
+ }
136
+
137
+ public function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') {
138
+ $str = '';
139
+ $escape_char = '\\';
140
+ foreach ($fields as $value) {
141
+ if (strpos($value, $delimiter) !== false ||
142
+ strpos($value, $enclosure) !== false ||
143
+ strpos($value, "\n") !== false ||
144
+ strpos($value, "\r") !== false ||
145
+ strpos($value, "\t") !== false ||
146
+ strpos($value, ' ') !== false) {
147
+ $str2 = $enclosure;
148
+ $escaped = 0;
149
+ $len = strlen($value);
150
+ for ($i=0;$i<$len;$i++) {
151
+ if ($value[$i] == $escape_char) {
152
+ $escaped = 1;
153
+ } else if (!$escaped && $value[$i] == $enclosure) {
154
+ $str2 .= $enclosure;
155
+ } else {
156
+ $escaped = 0;
157
+ }
158
+ $str2 .= $value[$i];
159
+ }
160
+ $str2 .= $enclosure;
161
+ $str .= $str2.$delimiter;
162
+ } else {
163
+ $str .= $enclosure.$value.$enclosure.$delimiter;
164
+ }
165
+ }
166
+ $str = substr($str,0,-1);
167
+ $str .= "\n";
168
+ return fwrite($handle, $str);
169
+ }
170
+
171
+ }
lib/Varien/File/CsvMulty.php ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Csv parse
29
+ *
30
+ * @author Magento Core Team <core@magentocommerce.com>
31
+ */
32
+
33
+ require_once 'Varien/File/Csv.php';
34
+
35
+ class Varien_File_Csv_Multy extends Varien_File_Csv {
36
+ /**
37
+ * Retrieve CSV file data as pairs with duplicates
38
+ *
39
+ * @param string $file
40
+ * @param int $keyIndex
41
+ * @param int $valueIndex
42
+ * @return array
43
+ */
44
+ public function getDataPairs($file, $keyIndex=0, $valueIndex=1)
45
+ {
46
+ $data = array();
47
+ $csvData = $this->getData($file);
48
+ $line_number = 0;
49
+ foreach ($csvData as $rowData) {
50
+ $line_number++;
51
+ if (isset($rowData[$keyIndex])) {
52
+ if(isset($data[$rowData[$keyIndex]])){
53
+ if(isset($data[$rowData[$keyIndex]]['duplicate'])){
54
+ #array_push($data[$rowData[$keyIndex]]['duplicate'],array('line' => $line_number,'value' => isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null));
55
+ $data[$rowData[$keyIndex]]['duplicate']['line'] .=', '.$line_number;
56
+ } else {
57
+ $tmp_value = $data[$rowData[$keyIndex]]['value'];
58
+ $tmp_line = $data[$rowData[$keyIndex]]['line'];
59
+ $data[$rowData[$keyIndex]]['duplicate'] = array();
60
+ #array_push($data[$rowData[$keyIndex]]['duplicate'],array('line' => $tmp_line.' ,'.$line_number,'value' => $tmp_value));
61
+ $data[$rowData[$keyIndex]]['duplicate']['line'] = $tmp_line.' ,'.$line_number;
62
+ $data[$rowData[$keyIndex]]['duplicate']['value'] = $tmp_value;
63
+ }
64
+ } else {
65
+ $data[$rowData[$keyIndex]] = array();
66
+ $data[$rowData[$keyIndex]]['line'] = $line_number;
67
+ $data[$rowData[$keyIndex]]['value'] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
68
+ }
69
+ }
70
+ }
71
+ return $data;
72
+ }
73
+
74
+ }
75
+ ?>
lib/Varien/File/Object.php ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * File Object
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_File
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ require_once("Varien/Object.php");
36
+ require_once('Varien/Directory/IFactory.php');
37
+
38
+ class Varien_File_Object extends SplFileObject implements IFactory {
39
+ protected $_filename;
40
+ protected $_path;
41
+ protected $_filter;
42
+ protected $_isCorrect=true; # - pass or not filter checking
43
+ protected $filtered;
44
+
45
+ /**
46
+ * Constructor
47
+ *
48
+ * @param string $path - path to directory
49
+ * @return none
50
+ */
51
+ public function __construct($path)
52
+ {
53
+ parent::__construct($path);
54
+ $this->_path=$path;
55
+ $this->_filename=basename($path);
56
+ }
57
+ /**
58
+ * add file name to array
59
+ *
60
+ * @param array &$files - array of files
61
+ * @return none
62
+ */
63
+ public function getFilesName(&$files)
64
+ {
65
+ $this->getFileName(&$files);
66
+ }
67
+ /**
68
+ * add file name to array
69
+ *
70
+ * @param array &$files - array of files
71
+ * @return none
72
+ */
73
+ public function getFileName(&$files=null)
74
+ {
75
+ if($this->_isCorrect){
76
+ if($files===null)
77
+ return $this->_filename;
78
+ $files[] = $this->_filename;
79
+ }
80
+ }
81
+ /**
82
+ * add file path to array
83
+ *
84
+ * @param array &$paths - array of paths
85
+ * @return none
86
+ */
87
+ public function getFilesPaths(&$paths)
88
+ {
89
+ if($this->_isCorrect){
90
+ $paths[] = (string)$this->_path;
91
+ }
92
+ }
93
+ /**
94
+ * add file path to array
95
+ *
96
+ * @param array &$paths - array of paths
97
+ * @return none
98
+ */
99
+ public function getFilePath(&$path=null)
100
+ {
101
+ if($this->_isCorrect){
102
+ if($path===null)
103
+ return $this->_path;
104
+ $paths[] = $this->_path;
105
+ }
106
+ }
107
+ /**
108
+ * use filter
109
+ *
110
+ * @param bool $useFilter - use or not filter
111
+ * @return none
112
+ */
113
+ public function useFilter($useFilter)
114
+ {
115
+ if($useFilter){
116
+ $this->renderFilter();
117
+ } else {
118
+ $this->_isCorrect = true;
119
+ $this->filtered = false;
120
+ }
121
+
122
+ }
123
+ /**
124
+ * add file object to array
125
+ *
126
+ * @param array &$objs - array of gile objects
127
+ * @return none
128
+ */
129
+ public function getFilesObj(&$objs)
130
+ {
131
+ if($this->_isCorrect){
132
+ $objs[] = $this;
133
+ }
134
+ }
135
+ /**
136
+ * nothing
137
+ *
138
+ * @param array &$dirs - array of dirs
139
+ * @return none
140
+ */
141
+ public function getDirsName(&$dirs)
142
+ {
143
+ return Varien_Directory_Collection::getLastDir($this->_path);
144
+ }
145
+ /**
146
+ * nothing
147
+ *
148
+ * @param array &$dirs - array of dirs
149
+ * @return none
150
+ */
151
+ public function getDirName()
152
+ {
153
+ return Varien_Directory_Collection::lastDir($this->_path);
154
+ }
155
+ /**
156
+ * set file filter
157
+ *
158
+ * @param array $filter - array of filter
159
+ * @return none
160
+ */
161
+ public function setFilesFilter($filter)
162
+ {
163
+ $this->addFilter($filter);
164
+ }
165
+ /**
166
+ * set file filter
167
+ *
168
+ * @param array $filter - array of filter
169
+ * @return none
170
+ */
171
+ public function addFilter($filter)
172
+ {
173
+ $this->_filter = $filter;
174
+ }
175
+ /**
176
+ * get extension of file
177
+ *
178
+ * @return string - extension of file
179
+ */
180
+ public function getExtension()
181
+ {
182
+ return self::getExt($this->_filename);
183
+ }
184
+ /**
185
+ * get extension of file
186
+ *
187
+ * @param string $fileName - name of file
188
+ * @return string - extension of file
189
+ */
190
+ static public function getExt($fileName)
191
+ {
192
+ if($fileName === ''){
193
+ $path_parts = pathinfo($this->_filename);
194
+ } else {
195
+ $path_parts = pathinfo($fileName);
196
+ }
197
+ if(isset($path_parts["extension"]))
198
+ return $path_parts["extension"];
199
+ else
200
+ return '';
201
+ }
202
+ /**
203
+ * get name of file
204
+ *
205
+ * @return string - name of file
206
+ */
207
+ public function getName()
208
+ {
209
+ return basename($this->_filename,'.'.$this->getExtension());
210
+ }
211
+ /**
212
+ * render filters
213
+ *
214
+ * @return none
215
+ */
216
+ public function renderFilter()
217
+ {
218
+ #print_r($this->_filter);
219
+ if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
220
+ $this->filtered = true;
221
+ if(isset($this->_filter['extension'])){
222
+ $filter = $this->_filter['extension'];
223
+ if($filter!=null){
224
+ if(is_array($filter)){
225
+ if(!in_array($this->getExtension(),$filter)){
226
+ $this->_isCorrect = false;
227
+ }
228
+ } else {
229
+ if($this->getExtension()!=$filter){
230
+ $this->_isCorrect = false;
231
+ }
232
+ }
233
+ }
234
+ }
235
+ if(isset($this->_filter['name'])){
236
+ $filter = $this->_filter['name'];
237
+ if($filter!=null){
238
+ if(is_array($filter)){
239
+ if(!in_array($this->getName(),$filter)){
240
+ $this->_isCorrect = false;
241
+ }
242
+ } else {
243
+ if($this->getName()!=$filter){
244
+ $this->_isCorrect = false;
245
+ }
246
+ }
247
+ }
248
+ }
249
+
250
+ if(isset($this->_filter['regName'])){
251
+ $filter = $this->_filter['regName'];
252
+
253
+ if($filter!=null){
254
+ foreach ($filter as $value) {
255
+ if(!preg_match($value,$this->getName())){
256
+ $this->_isCorrect = false;
257
+ }
258
+ }
259
+
260
+ }
261
+ }
262
+ }
263
+ }
264
+ /**
265
+ * add to array file name
266
+ *
267
+ * @param array &$arr -export array
268
+ * @return none
269
+ */
270
+ public function toArray(&$arr)
271
+ {
272
+ if($this->_isCorrect){
273
+ $arr['files_in_dirs'][] = $this->_filename;
274
+ }
275
+ }
276
+ /**
277
+ * add to xml file name
278
+ *
279
+ * @param array &$xml -export xml
280
+ * @param int $recursionLevel - level of recursion
281
+ * @param bool $addOpenTag - nothing
282
+ * @param string $rootName - nothing
283
+ * @return none
284
+ */
285
+ public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
286
+ {
287
+ if($this->_isCorrect){
288
+ $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
289
+ }
290
+ }
291
+
292
+ }
293
+
294
+ ?>
lib/Varien/File/Transfer/Adapter/Http.php ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Csv parse
29
+ *
30
+ * @author Magento Core Team <core@magentocommerce.com>
31
+ */
32
+ class Varien_File_Transfer_Adapter_Http
33
+ {
34
+ protected $_mimeTypes = array(
35
+ 'txt' => 'text/plain',
36
+ 'htm' => 'text/html',
37
+ 'html' => 'text/html',
38
+ 'php' => 'text/html',
39
+ 'css' => 'text/css',
40
+ 'js' => 'application/javascript',
41
+ 'json' => 'application/json',
42
+ 'xml' => 'application/xml',
43
+ 'swf' => 'application/x-shockwave-flash',
44
+ 'flv' => 'video/x-flv',
45
+
46
+ // images
47
+ 'png' => 'image/png',
48
+ 'jpe' => 'image/jpeg',
49
+ 'jpeg' => 'image/jpeg',
50
+ 'jpg' => 'image/jpeg',
51
+ 'gif' => 'image/gif',
52
+ 'bmp' => 'image/bmp',
53
+ 'ico' => 'image/vnd.microsoft.icon',
54
+ 'tiff' => 'image/tiff',
55
+ 'tif' => 'image/tiff',
56
+ 'svg' => 'image/svg+xml',
57
+ 'svgz' => 'image/svg+xml',
58
+
59
+ // archives
60
+ 'zip' => 'application/zip',
61
+ 'rar' => 'application/x-rar-compressed',
62
+ 'exe' => 'application/x-msdownload',
63
+ 'msi' => 'application/x-msdownload',
64
+ 'cab' => 'application/vnd.ms-cab-compressed',
65
+
66
+ // audio/video
67
+ 'mp3' => 'audio/mpeg',
68
+ 'qt' => 'video/quicktime',
69
+ 'mov' => 'video/quicktime',
70
+
71
+ // adobe
72
+ 'pdf' => 'application/pdf',
73
+ 'psd' => 'image/vnd.adobe.photoshop',
74
+ 'ai' => 'application/postscript',
75
+ 'eps' => 'application/postscript',
76
+ 'ps' => 'application/postscript'
77
+ );
78
+
79
+ /**
80
+ * Send the file to the client (Download)
81
+ *
82
+ * @param string|array $options Options for the file(s) to send
83
+ * @return void
84
+ */
85
+ public function send($options = null)
86
+ {
87
+ if (is_string($options)) {
88
+ $filepath = $options;
89
+ } else if (is_array($options)) {
90
+ $filepath = $options['filepath'];
91
+ } else {
92
+ throw new Exception("Filename is not set.");
93
+ }
94
+
95
+ if (!is_file($filepath) || !is_readable($filepath)) {
96
+ throw new Exception("File '{$filepath}' does not exists.");
97
+ }
98
+
99
+ $mimeType = $this->_detectMimeType(array('name' => $filepath));
100
+
101
+ $response = new Zend_Controller_Response_Http();
102
+
103
+ $response->setHeader('Content-length', filesize($filepath));
104
+ $response->setHeader('Content-Type', $mimeType);
105
+
106
+ $response->sendHeaders();
107
+
108
+ $handle = fopen($filepath, 'r');
109
+ if ($handle) {
110
+ while (($buffer = fgets($handle, 4096)) !== false) {
111
+ echo $buffer;
112
+ }
113
+ if (!feof($handle)) {
114
+ throw new Exception("Error: unexpected fgets() fail.");
115
+ }
116
+ fclose($handle);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Internal method to detect the mime type of a file
122
+ *
123
+ * @param array $value File infos
124
+ * @return string Mime type of given file
125
+ */
126
+ protected function _detectMimeType($value)
127
+ {
128
+ if (file_exists($value['name'])) {
129
+ $file = $value['name'];
130
+ } else if (file_exists($value['tmp_name'])) {
131
+ $file = $value['tmp_name'];
132
+ } else {
133
+ return null;
134
+ }
135
+
136
+ $parts = explode('.', $file);
137
+ $extention = strtolower(array_pop($parts));
138
+ if (isset($this->_mimeTypes[$extention])) {
139
+ $result = $this->_mimeTypes[$extention];
140
+ }
141
+
142
+ if (empty($result) && (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
143
+ $result = mime_content_type($file);
144
+ }
145
+
146
+ if (empty($result)) {
147
+ $result = 'application/octet-stream';
148
+ }
149
+
150
+ return $result;
151
+ }
152
+
153
+ }
lib/Varien/File/Uploader.php ADDED
@@ -0,0 +1,542 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * File upload class
29
+ *
30
+ * ATTENTION! This class must be used like abstract class and must added
31
+ * validation by protected file extension list to extended class
32
+ *
33
+ * @category Varien
34
+ * @package Varien_File
35
+ * @author Magento Core Team <core@magentocommerce.com>
36
+ */
37
+
38
+ class Varien_File_Uploader
39
+ {
40
+ /**
41
+ * Uploaded file handle (copy of $_FILES[] element)
42
+ *
43
+ * @var array
44
+ * @access protected
45
+ */
46
+ protected $_file;
47
+
48
+ /**
49
+ * Uploaded file mime type
50
+ *
51
+ * @var string
52
+ * @access protected
53
+ */
54
+ protected $_fileMimeType;
55
+
56
+ /**
57
+ * Upload type. Used to right handle $_FILES array.
58
+ *
59
+ * @var Varien_File_Uploader::SINGLE_STYLE|Varien_File_Uploader::MULTIPLE_STYLE
60
+ * @access protected
61
+ */
62
+ protected $_uploadType;
63
+
64
+ /**
65
+ * The name of uploaded file. By default it is original file name, but when
66
+ * we will change file name, this variable will be changed too.
67
+ *
68
+ * @var string
69
+ * @access protected
70
+ */
71
+ protected $_uploadedFileName;
72
+
73
+ /**
74
+ * The name of destination directory
75
+ *
76
+ * @var string
77
+ * @access protected
78
+ */
79
+ protected $_uploadedFileDir;
80
+
81
+ /**
82
+ * If this variable is set to TRUE, our library will be able to automaticaly create
83
+ * non-existant directories.
84
+ *
85
+ * @var bool
86
+ * @access protected
87
+ */
88
+ protected $_allowCreateFolders = true;
89
+
90
+ /**
91
+ * If this variable is set to TRUE, uploaded file name will be changed if some file with the same
92
+ * name already exists in the destination directory (if enabled).
93
+ *
94
+ * @var bool
95
+ * @access protected
96
+ */
97
+ protected $_allowRenameFiles = false;
98
+
99
+ /**
100
+ * If this variable is set to TRUE, files despersion will be supported.
101
+ *
102
+ * @var bool
103
+ * @access protected
104
+ */
105
+ protected $_enableFilesDispersion = false;
106
+
107
+ /**
108
+ * This variable is used both with $_enableFilesDispersion == true
109
+ * It helps to avoid problems after migrating from case-insensitive file system to case-insensitive
110
+ * (e.g. NTFS->ext or ext->NTFS)
111
+ *
112
+ * @var bool
113
+ * @access protected
114
+ */
115
+ protected $_caseInsensitiveFilenames = true;
116
+
117
+ /**
118
+ * @var string
119
+ * @access protected
120
+ */
121
+ protected $_dispretionPath = null;
122
+
123
+ protected $_fileExists = false;
124
+
125
+ protected $_allowedExtensions = null;
126
+
127
+ /**
128
+ * Validate callbacks storage
129
+ *
130
+ * @var array
131
+ * @access protected
132
+ */
133
+ protected $_validateCallbacks = array();
134
+
135
+ const SINGLE_STYLE = 0;
136
+ const MULTIPLE_STYLE = 1;
137
+ const TMP_NAME_EMPTY = 666;
138
+
139
+ /**
140
+ * Resulting of uploaded file
141
+ *
142
+ * @var array|bool Array with file info keys: path, file. Result is
143
+ * FALSE when file not uploaded
144
+ */
145
+ protected $_result;
146
+
147
+ function __construct($fileId)
148
+ {
149
+ $this->_setUploadFileId($fileId);
150
+ if( !file_exists($this->_file['tmp_name']) ) {
151
+ $code = empty($this->_file['tmp_name']) ? self::TMP_NAME_EMPTY : 0;
152
+ throw new Exception('File was not uploaded.', $code);
153
+ } else {
154
+ $this->_fileExists = true;
155
+ }
156
+ }
157
+
158
+ /**
159
+ * After save logic
160
+ *
161
+ * @param array $result
162
+ * @return Varien_File_Uploader
163
+ */
164
+ protected function _afterSave($result)
165
+ {
166
+ return $this;
167
+ }
168
+
169
+ /**
170
+ * Used to save uploaded file into destination folder with
171
+ * original or new file name (if specified)
172
+ *
173
+ * @param string $destinationFolder
174
+ * @param string $newFileName
175
+ * @access public
176
+ * @return void|bool
177
+ */
178
+ public function save($destinationFolder, $newFileName = null)
179
+ {
180
+ $this->_validateFile();
181
+
182
+ if ($this->_allowCreateFolders) {
183
+ $this->_createDestinationFolder($destinationFolder);
184
+ }
185
+
186
+ if (!is_writable($destinationFolder)) {
187
+ throw new Exception('Destination folder is not writable or does not exists.');
188
+ }
189
+
190
+ $this->_result = false;
191
+
192
+ $destinationFile = $destinationFolder;
193
+ $fileName = isset($newFileName) ? $newFileName : $this->_file['name'];
194
+ $fileName = self::getCorrectFileName($fileName);
195
+ if ($this->_enableFilesDispersion) {
196
+ $fileName = $this->correctFileNameCase($fileName);
197
+ $this->setAllowCreateFolders(true);
198
+ $this->_dispretionPath = self::getDispretionPath($fileName);
199
+ $destinationFile.= $this->_dispretionPath;
200
+ $this->_createDestinationFolder($destinationFile);
201
+ }
202
+
203
+ if ($this->_allowRenameFiles) {
204
+ $fileName = self::getNewFileName(self::_addDirSeparator($destinationFile) . $fileName);
205
+ }
206
+
207
+ $destinationFile = self::_addDirSeparator($destinationFile) . $fileName;
208
+
209
+ $this->_result = $this->_moveFile($this->_file['tmp_name'], $destinationFile);
210
+
211
+ if ($this->_result) {
212
+ chmod($destinationFile, 0777);
213
+ if ($this->_enableFilesDispersion) {
214
+ $fileName = str_replace(DIRECTORY_SEPARATOR, '/',
215
+ self::_addDirSeparator($this->_dispretionPath)) . $fileName;
216
+ }
217
+ $this->_uploadedFileName = $fileName;
218
+ $this->_uploadedFileDir = $destinationFolder;
219
+ $this->_result = $this->_file;
220
+ $this->_result['path'] = $destinationFolder;
221
+ $this->_result['file'] = $fileName;
222
+
223
+ $this->_afterSave($this->_result);
224
+ }
225
+
226
+ return $this->_result;
227
+ }
228
+
229
+ /**
230
+ * Move files from TMP folder into destination folder
231
+ *
232
+ * @param string $tmpPath
233
+ * @param string $destPath
234
+ * @return bool
235
+ */
236
+ protected function _moveFile($tmpPath, $destPath)
237
+ {
238
+ return move_uploaded_file($tmpPath, $destPath);
239
+ }
240
+
241
+ /**
242
+ * Validate file before save
243
+ *
244
+ * @access public
245
+ */
246
+ protected function _validateFile()
247
+ {
248
+ if( $this->_fileExists === false ) {
249
+ return;
250
+ }
251
+
252
+ $filePath = $this->_file['tmp_name'];
253
+ $fileName = $this->_file['name'];
254
+
255
+ //is file extension allowed
256
+ $fileExtension = substr($fileName, strrpos($fileName, '.')+1);
257
+ if (!$this->checkAllowedExtension($fileExtension)) {
258
+ throw new Exception('Disallowed file type.');
259
+ }
260
+ //run validate callbacks
261
+ foreach ($this->_validateCallbacks as $params) {
262
+ if (is_object($params['object']) && method_exists($params['object'], $params['method'])) {
263
+ $params['object']->$params['method']($filePath);
264
+ }
265
+ }
266
+ }
267
+
268
+ /**
269
+ * Add validation callback model for us in self::_validateFile()
270
+ *
271
+ * @param string $callbackName
272
+ * @param object $callbackObject
273
+ * @param string $callbackMethod Method name of $callbackObject. It must
274
+ * have interface (string $tmpFilePath)
275
+ * @return Varien_File_Uploader
276
+ */
277
+ public function addValidateCallback($callbackName, $callbackObject, $callbackMethod)
278
+ {
279
+ $this->_validateCallbacks[$callbackName] = array(
280
+ 'object' => $callbackObject,
281
+ 'method' => $callbackMethod
282
+ );
283
+ return $this;
284
+ }
285
+
286
+ /**
287
+ * Delete validation callback model for us in self::_validateFile()
288
+ *
289
+ * @param string $callbackName
290
+ * @access public
291
+ * @return Varien_File_Uploader
292
+ */
293
+ public function removeValidateCallback($callbackName)
294
+ {
295
+ if (isset($this->_validateCallbacks[$callbackName])) {
296
+ unset($this->_validateCallbacks[$callbackName]);
297
+ }
298
+ return $this;
299
+ }
300
+
301
+ /**
302
+ * Correct filename with special chars and spaces
303
+ *
304
+ * @param string $fileName
305
+ * @return string
306
+ */
307
+ static public function getCorrectFileName($fileName)
308
+ {
309
+ $fileName = preg_replace('/[^a-z0-9_\\-\\.]+/i', '_', $fileName);
310
+ $fileInfo = pathinfo($fileName);
311
+
312
+ if (preg_match('/^_+$/', $fileInfo['filename'])) {
313
+ $fileName = 'file.' . $fileInfo['extension'];
314
+ }
315
+ return $fileName;
316
+ }
317
+
318
+ /**
319
+ * Convert filename to lowercase in case of case-insensitive file names
320
+ *
321
+ * @param string
322
+ * @return string
323
+ */
324
+ public function correctFileNameCase($fileName)
325
+ {
326
+ if ($this->_caseInsensitiveFilenames) {
327
+ return strtolower($fileName);
328
+ }
329
+ return $fileName;
330
+ }
331
+
332
+ static protected function _addDirSeparator($dir)
333
+ {
334
+ if (substr($dir,-1) != DIRECTORY_SEPARATOR) {
335
+ $dir.= DIRECTORY_SEPARATOR;
336
+ }
337
+ return $dir;
338
+ }
339
+
340
+ /**
341
+ * Used to check if uploaded file mime type is valid or not
342
+ *
343
+ * @param array $validTypes
344
+ * @access public
345
+ * @return bool
346
+ */
347
+ public function checkMimeType($validTypes=Array())
348
+ {
349
+ if (count($validTypes) > 0) {
350
+ if (!in_array($this->_getMimeType(), $validTypes)) {
351
+ return false;
352
+ }
353
+ }
354
+ return true;
355
+ }
356
+
357
+ /**
358
+ * Returns a name of uploaded file
359
+ *
360
+ * @access public
361
+ * @return string
362
+ */
363
+ public function getUploadedFileName()
364
+ {
365
+ return $this->_uploadedFileName;
366
+ }
367
+
368
+ /**
369
+ * Used to set {@link _allowCreateFolders} value
370
+ *
371
+ * @param mixed $flag
372
+ * @access public
373
+ * @return Varien_File_Uploader
374
+ */
375
+ public function setAllowCreateFolders($flag)
376
+ {
377
+ $this->_allowCreateFolders = $flag;
378
+ return $this;
379
+ }
380
+
381
+ /**
382
+ * Used to set {@link _allowRenameFiles} value
383
+ *
384
+ * @param mixed $flag
385
+ * @access public
386
+ * @return Varien_File_Uploader
387
+ */
388
+ public function setAllowRenameFiles($flag)
389
+ {
390
+ $this->_allowRenameFiles = $flag;
391
+ return $this;
392
+ }
393
+
394
+ /**
395
+ * Used to set {@link _enableFilesDispersion} value
396
+ *
397
+ * @param mixed $flag
398
+ * @access public
399
+ * @return Varien_File_Uploader
400
+ */
401
+ public function setFilesDispersion($flag)
402
+ {
403
+ $this->_enableFilesDispersion = $flag;
404
+ return $this;
405
+ }
406
+
407
+ /**
408
+ * Filenames Case-sensitivity setter
409
+ *
410
+ * @param mixed $flag
411
+ * @return Varien_File_Uploader
412
+ */
413
+ public function setFilenamesCaseSensitivity($flag)
414
+ {
415
+ $this->_caseInsensitiveFilenames = $flag;
416
+ return $this;
417
+ }
418
+
419
+ public function setAllowedExtensions($extensions = array())
420
+ {
421
+ foreach ((array)$extensions as $extension) {
422
+ $this->_allowedExtensions[] = strtolower($extension);
423
+ }
424
+ return $this;
425
+ }
426
+
427
+ /**
428
+ * Check if specified extension is allowed
429
+ *
430
+ * @param string $extension
431
+ * @return boolean
432
+ */
433
+ public function checkAllowedExtension($extension)
434
+ {
435
+ if (!is_array($this->_allowedExtensions) || empty($this->_allowedExtensions)) {
436
+ return true;
437
+ }
438
+
439
+ return in_array(strtolower($extension), $this->_allowedExtensions);
440
+ }
441
+
442
+ /**
443
+ * @deprecated after 1.5.0.0-beta2
444
+ *
445
+ * @param string $extension
446
+ * @return boolean
447
+ */
448
+ public function chechAllowedExtension($extension)
449
+ {
450
+ return $this->checkAllowedExtension($extension);
451
+ }
452
+
453
+ private function _getMimeType()
454
+ {
455
+ return $this->_file['type'];
456
+ }
457
+
458
+ private function _setUploadFileId($fileId)
459
+ {
460
+ if (empty($_FILES)) {
461
+ throw new Exception('$_FILES array is empty');
462
+ }
463
+
464
+ if (is_array($fileId)) {
465
+ $this->_uploadType = self::MULTIPLE_STYLE;
466
+ $this->_file = $fileId;
467
+ } else {
468
+ preg_match("/^(.*?)\[(.*?)\]$/", $fileId, $file);
469
+
470
+ if (count($file) > 0 && (count($file[0]) > 0) && (count($file[1]) > 0)) {
471
+ array_shift($file);
472
+ $this->_uploadType = self::MULTIPLE_STYLE;
473
+
474
+ $fileAttributes = $_FILES[$file[0]];
475
+ $tmp_var = array();
476
+
477
+ foreach ($fileAttributes as $attributeName => $attributeValue) {
478
+ $tmp_var[$attributeName] = $attributeValue[$file[1]];
479
+ }
480
+
481
+ $fileAttributes = $tmp_var;
482
+ $this->_file = $fileAttributes;
483
+ } elseif( count($fileId) > 0 && isset($_FILES[$fileId])) {
484
+ $this->_uploadType = self::SINGLE_STYLE;
485
+ $this->_file = $_FILES[$fileId];
486
+ } elseif( $fileId == '' ) {
487
+ throw new Exception('Invalid parameter given. A valid $_FILES[] identifier is expected.');
488
+ }
489
+ }
490
+ }
491
+
492
+ private function _createDestinationFolder($destinationFolder)
493
+ {
494
+ if (!$destinationFolder) {
495
+ return $this;
496
+ }
497
+
498
+ if (substr($destinationFolder, -1) == DIRECTORY_SEPARATOR) {
499
+ $destinationFolder = substr($destinationFolder, 0, -1);
500
+ }
501
+
502
+ if (!(@is_dir($destinationFolder) || @mkdir($destinationFolder, 0777, true))) {
503
+ throw new Exception("Unable to create directory '{$destinationFolder}'.");
504
+ }
505
+ return $this;
506
+ }
507
+
508
+ static public function getNewFileName($destFile)
509
+ {
510
+ $fileInfo = pathinfo($destFile);
511
+ if (file_exists($destFile)) {
512
+ $index = 1;
513
+ $baseName = $fileInfo['filename'] . '.' . $fileInfo['extension'];
514
+ while( file_exists($fileInfo['dirname'] . DIRECTORY_SEPARATOR . $baseName) ) {
515
+ $baseName = $fileInfo['filename']. '_' . $index . '.' . $fileInfo['extension'];
516
+ $index ++;
517
+ }
518
+ $destFileName = $baseName;
519
+ } else {
520
+ return $fileInfo['basename'];
521
+ }
522
+
523
+ return $destFileName;
524
+ }
525
+
526
+ static public function getDispretionPath($fileName)
527
+ {
528
+ $char = 0;
529
+ $dispretionPath = '';
530
+ while (($char < 2) && ($char < strlen($fileName))) {
531
+ if (empty($dispretionPath)) {
532
+ $dispretionPath = DIRECTORY_SEPARATOR
533
+ . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
534
+ } else {
535
+ $dispretionPath = self::_addDirSeparator($dispretionPath)
536
+ . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
537
+ }
538
+ $char ++;
539
+ }
540
+ return $dispretionPath;
541
+ }
542
+ }
lib/Varien/File/Uploader/Image.php ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_File
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * This class is invalid. Avoid using it
29
+ *
30
+ * @deprecated after 1.4.0.0-rc1
31
+ * @file Image.php
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_File_Uploader_Image extends Varien_File_Uploader
36
+ {
37
+
38
+ function __construct($file=null)
39
+ {
40
+ $this->newUploader($file);
41
+ }
42
+
43
+ /**
44
+ * Resizes an image
45
+ * Set parameters to the wanted (or maximum/minimum) width for the processed image, in pixels
46
+ */
47
+ public function resize($width=null, $height=null)
48
+ {
49
+ $this->uploader->image_resize = true;
50
+
51
+ $this->uploader->image_ratio_x = ( $width == null ) ? true : false;
52
+ $this->uploader->image_ratio_y = ( $height == null ) ? true : false;
53
+
54
+ $this->uploader->image_x = $width;
55
+ $this->uploader->image_y = $height;
56
+ }
57
+
58
+ /**
59
+ * Rotates the image by increments of 45 degrees
60
+ *
61
+ * Value is either 90, 180 or 270
62
+ *
63
+ * Default value is NULL (no rotation)
64
+ *
65
+ */
66
+ public function rotate($degrees=null)
67
+ {
68
+ $this->uploader->image_rotate = intval($degrees);
69
+ }
70
+
71
+ /**
72
+ * Flips the image vertically or horizontally
73
+ *
74
+ * Value is either 'h' or 'v', as in horizontal and vertical
75
+ *
76
+ * Default value is h (flip horizontally)
77
+ *
78
+ * @access public
79
+ * @var string;
80
+ */
81
+ public function flip($type="h")
82
+ {
83
+ $this->uploader->image_flip = $type;
84
+ }
85
+
86
+ /**
87
+ * Crops an image
88
+ *
89
+ * $crop values are four dimensions, or two, or one (CSS style)
90
+ * They represent the amount cropped top, right, bottom and left.
91
+ * These values can either be in an array, or a space separated string.
92
+ * Each value can be in pixels (with or without 'px'), or percentage (of the source image)
93
+ *
94
+ * For instance, are valid:
95
+ * $foo->crop(20) OR array(20);
96
+ * $foo->crop('20px') OR array('20px');
97
+ * $foo->crop('20 40') OR array('20', 40);
98
+ * $foo->crop('-20 25%') OR array(-20, '25%');
99
+ * $foo->crop('20px 25%') OR array('20px', '25%');
100
+ * $foo->crop('20% 25%') OR array('20%', '25%');
101
+ * $foo->crop('20% 25% 10% 30%') OR array('20%', '25%', '10%', '30%');
102
+ * $foo->crop('20px 25px 2px 2px') OR array('20px', '25%px', '2px', '2px');
103
+ * $foo->crop('20 25% 40px 10%') OR array(20, '25%', '40px', '10%');
104
+ *
105
+ * If a value is negative, the image will be expanded, and the extra parts will be filled with black
106
+ *
107
+ * Default value is NULL (no cropping)
108
+ */
109
+ public function crop($crop=0)
110
+ {
111
+ $this->uploader->image_crop = $crop;
112
+ }
113
+
114
+ /**
115
+ * Coverts an image
116
+ *
117
+ * Possibles $color values are : ''; 'png'; 'jpeg'; 'gif'
118
+ *
119
+ * Default value is 'jpeg'
120
+ *
121
+ */
122
+ public function convert($format="jpeg")
123
+ {
124
+ $this->uploader->image_convert = $format;
125
+ }
126
+
127
+ /**
128
+ * Adds a watermark on the image
129
+ *
130
+ * $fileName is a local image filename, relative or absolute. GIF, JPG and PNG are supported, as well as PNG alpha.
131
+ * $position sets the watermarkposition within the image
132
+ *
133
+ * Value of positions is one or two out of 'TBLR' (top, bottom, left, right)
134
+ *
135
+ * The positions are as following: TL T TR
136
+ * L R
137
+ * BL B BR
138
+ *
139
+ * Default value is "BL" (bottom left)
140
+ *
141
+ * Note that is $absoluteX and $absoluteY are used, $position has no effect
142
+ *
143
+ * $absoluteX sets the watermark absolute X position within the image
144
+ *
145
+ * Value is in pixels, representing the distance between the top of the image and the watermark
146
+ * If a negative value is used, it will represent the distance between the bottom of the image and the watermark
147
+ *
148
+ * $absoluteY sets the twatermark absolute Y position within the image
149
+ *
150
+ * Value is in pixels, representing the distance between the left of the image and the watermark
151
+ * If a negative value is used, it will represent the distance between the right of the image and the watermark
152
+ *
153
+ */
154
+ public function addWatermark($fileName=null, $position="BL", $absoluteX=null, $absoluteY=null)
155
+ {
156
+ if( !isset($fileName) ) {
157
+ return;
158
+ }
159
+
160
+ $this->uploader->image_watermark = $fileName;
161
+ $this->uploader->image_watermark_position = $position;
162
+ $this->uploader->image_watermark_x = $absoluteX;
163
+ $this->uploader->image_watermark_y = $absoluteY;
164
+ }
165
+
166
+ /**
167
+ * $height sets the height of the reflection
168
+ *
169
+ * Value is an integer in pixels, or a string which format can be in pixels or percentage.
170
+ * For instance, values can be : 40, '40', '40px' or '40%'
171
+ *
172
+ * $space sets the space between the source image and its relection
173
+ *
174
+ * Value is an integer in pixels, which can be negative
175
+ *
176
+ * $color sets the color of the reflection background.
177
+ *
178
+ * Value is an hexadecimal color, such as #FFFFFF
179
+ *
180
+ * $opacity sets the initial opacity of the reflection
181
+ *
182
+ * Value is an integer between 0 (no opacity) and 100 (full opacity).
183
+ *
184
+ */
185
+ public function addReflection($height="10%", $space=0, $color="#FFFFFF", $opacity=60)
186
+ {
187
+ if( intval($height) == 0 ) {
188
+ return;
189
+ }
190
+
191
+ $this->uploader->image_reflection_height = $height;
192
+ $this->uploader->image_reflection_space = $space;
193
+ $this->uploader->image_reflection_color = $color;
194
+ $this->uploader->image_reflection_opacity = $opacity;
195
+ }
196
+
197
+ /**
198
+ * Adds a text label on the image
199
+ *
200
+ * Value is a string, any text. Text will not word-wrap, although you can use breaklines in your text "\n"
201
+ */
202
+ public function addText($string="")
203
+ {
204
+ if( trim($string) == "" ) {
205
+ return;
206
+ }
207
+
208
+ $this->uploader->image_text = $string;
209
+ }
210
+
211
+ public function setTextDirection($direction)
212
+ {
213
+ $this->uploader->image_text_direction = $direction;
214
+ }
215
+
216
+ public function setTextColor($color)
217
+ {
218
+ $this->uploader->image_text_color = $color;
219
+ }
220
+
221
+ public function setTextVisibilityPercent($percent)
222
+ {
223
+ $this->uploader->image_text_percent = $visibilityPercent;
224
+ }
225
+
226
+ public function setTextBackgroundColor($color)
227
+ {
228
+ $this->uploader->image_text_background = $color;
229
+ }
230
+
231
+ public function setTextBackgroundVisPercent($percent)
232
+ {
233
+ $this->uploader->image_text_background_percent = $percent;
234
+ }
235
+
236
+ public function setTextFont($font)
237
+ {
238
+ $this->uploader->image_text_font = $font;
239
+ }
240
+
241
+ public function setTextPosition($position="TR")
242
+ {
243
+ $this->uploader->image_text_position = $position;
244
+ }
245
+
246
+ public function setTextAbsoluteX($absoluteX)
247
+ {
248
+ $this->uploader->image_text_x = $absoluteX;
249
+ }
250
+
251
+ public function setTextAbsoluteY($absoluteY)
252
+ {
253
+ $this->uploader->image_text_y = $absoluteY;
254
+ }
255
+
256
+ public function setTextPadding($padding)
257
+ {
258
+ $this->uploader->image_text_padding = $padding;
259
+ }
260
+
261
+ public function setTextPaddingX($padding)
262
+ {
263
+ $this->uploader->image_text_padding_x = $padding;
264
+ }
265
+
266
+ public function setTextPaddingY($padding)
267
+ {
268
+ $this->uploader->image_text_padding_y = $padding;
269
+ }
270
+
271
+ public function setTextAlignment($alignment)
272
+ {
273
+ $this->uploader->image_text_alignment = $alignment;
274
+ }
275
+
276
+ public function setTextLineSpacing($lineSpacing)
277
+ {
278
+ $this->uploader->image_text_line_spacing = $lineSpacing;
279
+ }
280
+
281
+ /**
282
+ * Turns the image into greyscale
283
+ */
284
+ public function convertToGreyscale()
285
+ {
286
+ $this->uploader->image_greyscale = true;
287
+ }
288
+
289
+ /**
290
+ * Inverts the color of an image
291
+ *
292
+ */
293
+ public function colorInvert()
294
+ {
295
+ $this->uploader->image_negative = true;
296
+ }
297
+
298
+ /**
299
+ * Applies a colored overlay on the image
300
+ *
301
+ * $color value is an hexadecimal color, such as #FFFFFF
302
+ *
303
+ * $percent value is a percentage, as an integer between 0 and 100
304
+ *
305
+ */
306
+ public function colorOverlay($color="#FFFFFF", $percent=50)
307
+ {
308
+ $this->uploader->image_overlay_color = $color;
309
+ $this->uploader->image_overlay_percent = $percent;
310
+ }
311
+
312
+ /**
313
+ * Corrects the image contrast
314
+ *
315
+ * Value can range between -127 and 127
316
+ *
317
+ */
318
+ public function setContrast($value=0)
319
+ {
320
+ $this->uploader->image_contrast = $value;
321
+ }
322
+
323
+ /**
324
+ * Corrects the image brightness
325
+ *
326
+ * Value can range between -127 and 127
327
+ *
328
+ */
329
+ public function setBrightness($value=0)
330
+ {
331
+ $this->uploader->image_brightness = $value;
332
+ }
333
+
334
+ /**
335
+ * Quality of JPEG created/converted destination image
336
+ *
337
+ * Default value is 85
338
+ *
339
+ */
340
+ public function setJpegQuality($value=85)
341
+ {
342
+ $this->uploader->jpeg_quality = $value;
343
+ #
344
+ }
345
+
346
+ /**
347
+ * Default color of the image background
348
+ *
349
+ * Is generally used when cropping an image with negative margins
350
+ *
351
+ */
352
+ public function setBgColor($color="#000000")
353
+ {
354
+ $this->uploader->image_background_color = $color;
355
+ }
356
+
357
+ function __destruct()
358
+ {
359
+ $this->uploader->Clean();
360
+ }
361
+ }
362
+
363
+ // ft:php
364
+ // fileformat:unix
365
+ // tabstop:4
366
+ ?>
lib/Varien/Filter/Array.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Array extends Zend_Filter
29
+ {
30
+ protected $_columnFilters = array();
31
+
32
+ function addFilter(Zend_Filter_Interface $filter, $column='')
33
+ {
34
+ if (''===$column) {
35
+ parent::addFilter($filter);
36
+ } else {
37
+ if (!isset($this->_columnFilters[$column])) {
38
+ $this->_columnFilters[$column] = new Zend_Filter();
39
+ }
40
+ $this->_columnFilters[$column]->addFilter($filter);
41
+ }
42
+ }
43
+
44
+ function filter($array)
45
+ {
46
+ $out = array();
47
+ foreach ($array as $column=>$value) {
48
+ $value = parent::filter($value);
49
+ if (isset($this->_columnFilters[$column])) {
50
+ $value = $this->_columnFilters[$column]->filter($value);
51
+ }
52
+ $out[$column] = $value;
53
+ }
54
+ return $out;
55
+ }
56
+ }
lib/Varien/Filter/Array/Grid.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Array_Grid extends Varien_Filter_Array
29
+ {
30
+ function filter($grid)
31
+ {
32
+ $out = array();
33
+ foreach ($grid as $i=>$array) {
34
+ $out[$i] = parent::filter($array);
35
+ }
36
+ return $out;
37
+ }
38
+ }
lib/Varien/Filter/Email.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Email implements Zend_Filter_Interface
29
+ {
30
+ public function filter($value)
31
+ {
32
+ $atIndex = 0;
33
+ }
34
+ }
lib/Varien/Filter/Money.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Money implements Zend_Filter_Interface
29
+ {
30
+ protected $_format = null;
31
+
32
+ public function __construct($format)
33
+ {
34
+ $this->_format = $format;
35
+ }
36
+
37
+ public function filter($value)
38
+ {
39
+ return money_format($this->_format, $value);
40
+ }
41
+ }
lib/Varien/Filter/Object.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Object extends Zend_Filter
29
+ {
30
+ protected $_columnFilters = array();
31
+
32
+ function addFilter(Zend_Filter_Interface $filter, $column='')
33
+ {
34
+ if (''===$column) {
35
+ parent::addFilter($filter);
36
+ } else {
37
+ if (!isset($this->_columnFilters[$column])) {
38
+ $this->_columnFilters[$column] = new Zend_Filter();
39
+ }
40
+ $this->_columnFilters[$column]->addFilter($filter);
41
+ }
42
+ }
43
+
44
+ function filter($object)
45
+ {
46
+ if (!$object instanceof Varien_Object) {
47
+ throw new Exception('Expecting an instance of Varien_Object');
48
+ }
49
+ $class = get_class($object);
50
+ $out = new $class;
51
+ foreach ($object->getData() as $column=>$value) {
52
+ $value = parent::filter($value);
53
+ if (isset($this->_columnFilters[$column])) {
54
+ $value = $this->_columnFilters[$column]->filter($value);
55
+ }
56
+ $out->setData($column, $value);
57
+ }
58
+ return $out;
59
+ }
60
+ }
lib/Varien/Filter/Object/Grid.php ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Object_Grid extends Varien_Filter_Object
29
+ {
30
+ function filter($grid)
31
+ {
32
+ $out = array();
33
+ if (is_array($grid)) {
34
+ foreach ($grid as $i=>$array) {
35
+ $out[$i] = parent::filter($array);
36
+ }
37
+ }
38
+ return $out;
39
+ }
40
+ }
lib/Varien/Filter/Sprintf.php ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Sprintf implements Zend_Filter_Interface
29
+ {
30
+ protected $_format = null;
31
+ protected $_decimals = null;
32
+ protected $_decPoint = null;
33
+ protected $_thousandsSep = null;
34
+
35
+ public function __construct($format, $decimals=null, $decPoint='.', $thousandsSep=',')
36
+ {
37
+ $this->_format = $format;
38
+ $this->_decimals = $decimals;
39
+ $this->_decPoint = $decPoint;
40
+ $this->_thousandsSep = $thousandsSep;
41
+ }
42
+
43
+ public function filter($value)
44
+ {
45
+ if (!is_null($this->_decimals)) {
46
+ $value = number_format($value, $this->_decimals, $this->_decPoint, $this->_thousandsSep);
47
+ }
48
+ $value = sprintf($this->_format, $value);
49
+ return $value;
50
+ }
51
+ }
lib/Varien/Filter/Template.php ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Template constructions filter
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Filter
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_Filter_Template implements Zend_Filter_Interface
36
+ {
37
+ /**
38
+ * Cunstruction regular expression
39
+ */
40
+ const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}/si';
41
+
42
+ /**
43
+ * Cunstruction logic regular expression
44
+ */
45
+ const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
46
+ const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';
47
+
48
+ /**
49
+ * Assigned template variables
50
+ *
51
+ * @var array
52
+ */
53
+ protected $_templateVars = array();
54
+
55
+ /**
56
+ * Include processor
57
+ *
58
+ * @var array|string|null
59
+ */
60
+ protected $_includeProcessor = null;
61
+
62
+ /**
63
+ * Sets template variables that's can be called througth {var ...} statement
64
+ *
65
+ * @param array $variables
66
+ */
67
+ public function setVariables(array $variables)
68
+ {
69
+ foreach($variables as $name=>$value) {
70
+ $this->_templateVars[$name] = $value;
71
+ }
72
+ return $this;
73
+ }
74
+
75
+ /**
76
+ * Sets the proccessor of includes.
77
+ *
78
+ * @param array $callback it must return string
79
+ */
80
+ public function setIncludeProcessor(array $callback)
81
+ {
82
+ $this->_includeProcessor = $callback;
83
+ return $this;
84
+ }
85
+
86
+ /**
87
+ * Sets the proccessor of includes.
88
+ *
89
+ * @return array|null
90
+ */
91
+ public function getIncludeProcessor()
92
+ {
93
+ return is_callable($this->_includeProcessor) ? $this->_includeProcessor : null;
94
+ }
95
+
96
+ /**
97
+ * Filter the string as template.
98
+ *
99
+ * @param string $value
100
+ * @return string
101
+ */
102
+ public function filter($value)
103
+ {
104
+ // "depend" and "if" operands should be first
105
+ foreach (array(
106
+ self::CONSTRUCTION_DEPEND_PATTERN => 'dependDirective',
107
+ self::CONSTRUCTION_IF_PATTERN => 'ifDirective',
108
+ ) as $pattern => $directive) {
109
+ if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) {
110
+ foreach($constructions as $index => $construction) {
111
+ $replacedValue = '';
112
+ $callback = array($this, $directive);
113
+ if(!is_callable($callback)) {
114
+ continue;
115
+ }
116
+ try {
117
+ $replacedValue = call_user_func($callback, $construction);
118
+ } catch (Exception $e) {
119
+ throw $e;
120
+ }
121
+ $value = str_replace($construction[0], $replacedValue, $value);
122
+ }
123
+ }
124
+ }
125
+
126
+ if(preg_match_all(self::CONSTRUCTION_PATTERN, $value, $constructions, PREG_SET_ORDER)) {
127
+ foreach($constructions as $index=>$construction) {
128
+ $replacedValue = '';
129
+ $callback = array($this, $construction[1].'Directive');
130
+ if(!is_callable($callback)) {
131
+ continue;
132
+ }
133
+ try {
134
+ $replacedValue = call_user_func($callback, $construction);
135
+ } catch (Exception $e) {
136
+ throw $e;
137
+ }
138
+ $value = str_replace($construction[0], $replacedValue, $value);
139
+ }
140
+ }
141
+ return $value;
142
+ }
143
+
144
+
145
+ public function varDirective($construction)
146
+ {
147
+ if (count($this->_templateVars)==0) {
148
+ // If template preprocessing
149
+ return $construction[0];
150
+ }
151
+
152
+ $replacedValue = $this->_getVariable($construction[2], '');
153
+ return $replacedValue;
154
+ }
155
+
156
+ public function includeDirective($construction)
157
+ {
158
+ // Processing of {include template=... [...]} statement
159
+ $includeParameters = $this->_getIncludeParameters($construction[2]);
160
+ if(!isset($includeParameters['template']) or !$this->getIncludeProcessor()) {
161
+ // Not specified template or not seted include processor
162
+ $replacedValue = '{Error in include processing}';
163
+ } else {
164
+ // Including of template
165
+ $templateCode = $includeParameters['template'];
166
+ unset($includeParameters['template']);
167
+ $includeParameters = array_merge_recursive($includeParameters, $this->_templateVars);
168
+ $replacedValue = call_user_func($this->getIncludeProcessor(), $templateCode, $includeParameters);
169
+ }
170
+ return $replacedValue;
171
+ }
172
+
173
+ public function dependDirective($construction)
174
+ {
175
+ if (count($this->_templateVars)==0) {
176
+ // If template preprocessing
177
+ return $construction[0];
178
+ }
179
+
180
+ if($this->_getVariable($construction[1], '')=='') {
181
+ return '';
182
+ } else {
183
+ return $construction[2];
184
+ }
185
+ }
186
+
187
+ public function ifDirective($construction)
188
+ {
189
+ if (count($this->_templateVars) == 0) {
190
+ return $construction[0];
191
+ }
192
+
193
+ if($this->_getVariable($construction[1], '') == '') {
194
+ if (isset($construction[3]) && isset($construction[4])) {
195
+ return $construction[4];
196
+ }
197
+ return '';
198
+ } else {
199
+ return $construction[2];
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Return associative array of include construction.
205
+ *
206
+ * @param string $value raw parameters
207
+ * @return array
208
+ */
209
+ protected function _getIncludeParameters($value)
210
+ {
211
+ $tokenizer = new Varien_Filter_Template_Tokenizer_Parameter();
212
+ $tokenizer->setString($value);
213
+ $params = $tokenizer->tokenize();
214
+ foreach ($params as $key => $value) {
215
+ if (substr($value, 0, 1) === '$') {
216
+ $params[$key] = $this->_getVariable(substr($value, 1), null);
217
+ }
218
+ }
219
+ return $params;
220
+ }
221
+
222
+ /**
223
+ * Return variable value for var construction
224
+ *
225
+ * @param string $value raw parameters
226
+ * @param string $default default value
227
+ * @return string
228
+ */
229
+ protected function _getVariable($value, $default='{no_value_defined}')
230
+ {
231
+ Varien_Profiler::start("email_template_proccessing_variables");
232
+ $tokenizer = new Varien_Filter_Template_Tokenizer_Variable();
233
+ $tokenizer->setString($value);
234
+ $stackVars = $tokenizer->tokenize();
235
+ $result = $default;
236
+ $last = 0;
237
+ for($i = 0; $i < count($stackVars); $i ++) {
238
+ if ($i == 0 && isset($this->_templateVars[$stackVars[$i]['name']])) {
239
+ // Getting of template value
240
+ $stackVars[$i]['variable'] =& $this->_templateVars[$stackVars[$i]['name']];
241
+ } else if (isset($stackVars[$i-1]['variable'])
242
+ && $stackVars[$i-1]['variable'] instanceof Varien_Object) {
243
+ // If object calling methods or getting properties
244
+ if($stackVars[$i]['type'] == 'property') {
245
+ $caller = "get" . uc_words($stackVars[$i]['name'], '');
246
+ if(is_callable(array($stackVars[$i-1]['variable'], $caller))) {
247
+ // If specified getter for this property
248
+ $stackVars[$i]['variable'] = $stackVars[$i-1]['variable']->$caller();
249
+ } else {
250
+ $stackVars[$i]['variable'] = $stackVars[$i-1]['variable']
251
+ ->getData($stackVars[$i]['name']);
252
+ }
253
+ } else if ($stackVars[$i]['type'] == 'method') {
254
+ // Calling of object method
255
+ if (is_callable(array($stackVars[$i-1]['variable'], $stackVars[$i]['name'])) || substr($stackVars[$i]['name'],0,3) == 'get') {
256
+ $stackVars[$i]['variable'] = call_user_func_array(array($stackVars[$i-1]['variable'],
257
+ $stackVars[$i]['name']),
258
+ $stackVars[$i]['args']);
259
+ }
260
+ }
261
+ $last = $i;
262
+ }
263
+ }
264
+
265
+ if(isset($stackVars[$last]['variable'])) {
266
+ // If value for construction exists set it
267
+ $result = $stackVars[$last]['variable'];
268
+ }
269
+ Varien_Profiler::stop("email_template_proccessing_variables");
270
+ return $result;
271
+ }
272
+ }
lib/Varien/Filter/Template/Simple.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Filter_Template_Simple extends Varien_Object implements Zend_Filter_Interface
29
+ {
30
+ protected $_startTag = '{{';
31
+ protected $_endTag = '}}';
32
+
33
+ public function setTags($start, $end)
34
+ {
35
+ $this->_startTag = $start;
36
+ $this->_endTag = $end;
37
+ return $this;
38
+ }
39
+
40
+ public function filter($value)
41
+ {
42
+ return preg_replace('#'.$this->_startTag.'(.*?)'.$this->_endTag.'#e', '$this->getData("$1")', $value);
43
+ }
44
+ }
lib/Varien/Filter/Template/Tokenizer/Abstract.php ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Template constructions tokenizer
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Filter
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ abstract class Varien_Filter_Template_Tokenizer_Abstract
36
+ {
37
+ /**
38
+ * Current index in string
39
+ * @var int
40
+ */
41
+ protected $_currentIndex;
42
+
43
+ /**
44
+ * String for tokenize
45
+ */
46
+ protected $_string;
47
+
48
+ /**
49
+ * Move current index to next char.
50
+ *
51
+ * If index out of bounds returns false
52
+ *
53
+ * @return boolean
54
+ */
55
+ public function next()
56
+ {
57
+ if($this->_currentIndex + 1 >= strlen($this->_string)) {
58
+ return false;
59
+ }
60
+
61
+ $this->_currentIndex++;
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * Move current index to previus char.
67
+ *
68
+ * If index out of bounds returns false
69
+ *
70
+ * @return boolean
71
+ */
72
+ public function prev()
73
+ {
74
+ if($this->_currentIndex - 1 < 0) {
75
+ return false;
76
+ }
77
+
78
+ $this->_currentIndex--;
79
+ return true;
80
+ }
81
+
82
+ /**
83
+ * Return current char
84
+ *
85
+ * @return string
86
+ */
87
+ public function char()
88
+ {
89
+ return $this->_string{$this->_currentIndex};
90
+ }
91
+
92
+
93
+ /**
94
+ * Set string for tokenize
95
+ */
96
+ public function setString($value)
97
+ {
98
+ $this->_string = $value;
99
+ $this->reset();
100
+ }
101
+
102
+ /**
103
+ * Move char index to begin of string
104
+ */
105
+ public function reset()
106
+ {
107
+ $this->_currentIndex = 0;
108
+ }
109
+
110
+ /**
111
+ * Return true if current char is white-space
112
+ *
113
+ * @return boolean
114
+ */
115
+ public function isWhiteSpace() {
116
+ return trim($this->char()) != $this->char();
117
+ }
118
+
119
+ abstract public function tokenize();
120
+
121
+ }
lib/Varien/Filter/Template/Tokenizer/Parameter.php ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Template constructions parameters tokenizer
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Filter
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_Filter_Template_Tokenizer_Parameter extends Varien_Filter_Template_Tokenizer_Abstract
36
+ {
37
+
38
+ /**
39
+ * Tokenize string and return getted parameters
40
+ *
41
+ * @return array
42
+ */
43
+ public function tokenize()
44
+ {
45
+ $parameters = array();
46
+ $parameterName = '';
47
+ while($this->next()) {
48
+ if($this->isWhiteSpace()) {
49
+ continue;
50
+ } else if($this->char()!='=') {
51
+ $parameterName .= $this->char();
52
+ } else {
53
+ $parameters[$parameterName] = $this->getValue();
54
+ $parameterName = '';
55
+ }
56
+
57
+ }
58
+ return $parameters;
59
+ }
60
+
61
+ /**
62
+ * Get string value in parameters througth tokenize
63
+ *
64
+ * @return string
65
+ */
66
+ public function getValue() {
67
+ $this->next();
68
+ $value = '';
69
+ if($this->isWhiteSpace()) {
70
+ return $value;
71
+ }
72
+ $quoteStart = $this->char() == "'" || $this->char() == '"';
73
+
74
+
75
+ if($quoteStart) {
76
+ $breakSymbol = $this->char();
77
+ } else {
78
+ $breakSymbol = false;
79
+ $value .= $this->char();
80
+ }
81
+
82
+ while ($this->next()) {
83
+ if (!$breakSymbol && $this->isWhiteSpace()) {
84
+ break;
85
+ } else if ($breakSymbol && $this->char() == $breakSymbol) {
86
+ break;
87
+ } else if ($this->char() == '\\') {
88
+ $this->next();
89
+ $value .= $this->char();
90
+ } else {
91
+ $value .= $this->char();
92
+ }
93
+ }
94
+
95
+ return $value;
96
+ }
97
+
98
+ }
lib/Varien/Filter/Template/Tokenizer/Variable.php ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Filter
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Template constructions variables tokenizer
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Filter
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_Filter_Template_Tokenizer_Variable extends Varien_Filter_Template_Tokenizer_Abstract
36
+ {
37
+
38
+ /**
39
+ * Tokenize string and return getted variable stack path
40
+ *
41
+ * @return array
42
+ */
43
+ public function tokenize()
44
+ {
45
+ $actions = array();
46
+ $parameterName = '';
47
+ $variableSet = false;
48
+ do {
49
+ if($this->isWhiteSpace()) {
50
+ // Ignore white spaces
51
+ continue;
52
+ } else if($this->char()!='.' && $this->char()!='(') {
53
+ // Property or method name
54
+ $parameterName .= $this->char();
55
+ } else if($this->char()=='(') {
56
+ // Method declaration
57
+ $methodArgs = $this->getMethodArgs();
58
+ $actions[] = array('type'=>'method',
59
+ 'name'=>$parameterName,
60
+ 'args'=>$methodArgs);
61
+ $parameterName = '';
62
+ } else if($parameterName!='') {
63
+ // Property or variable declaration
64
+ if($variableSet) {
65
+ $actions[] = array('type'=>'property',
66
+ 'name'=>$parameterName);
67
+ } else {
68
+ $variableSet = true;
69
+ $actions[] = array('type'=>'variable',
70
+ 'name'=>$parameterName);
71
+ }
72
+ $parameterName = '';
73
+ }
74
+ } while ($this->next());
75
+
76
+ if($parameterName != '' ) {
77
+ if($variableSet) {
78
+ $actions[] = array('type'=>'property',
79
+ 'name'=>$parameterName);
80
+ } else {
81
+ $actions[] = array('type'=>'variable',
82
+ 'name'=>$parameterName);
83
+ }
84
+ }
85
+
86
+ return $actions;
87
+ }
88
+
89
+ /**
90
+ * Get string value for method args
91
+ *
92
+ * @return string
93
+ */
94
+ public function getString() {
95
+
96
+ $value = '';
97
+ if($this->isWhiteSpace()) {
98
+ return $value;
99
+ }
100
+ $qouteStart = $this->isQuote();
101
+
102
+ if($qouteStart) {
103
+ $breakSymbol = $this->char();
104
+ } else {
105
+ $breakSymbol = false;
106
+ $value .= $this->char();
107
+ }
108
+
109
+ while ($this->next()) {
110
+ if (!$breakSymbol && ($this->isWhiteSpace() || $this->char() == ',' || $this->char() == ')') ) {
111
+ $this->prev();
112
+ break;
113
+ } else if ($breakSymbol && $this->char() == $breakSymbol) {
114
+ break;
115
+ } else if ($this->char() == '\\') {
116
+ $this->next();
117
+ $value .= $this->char();
118
+ } else {
119
+ $value .= $this->char();
120
+
121
+ }
122
+ }
123
+
124
+ return $value;
125
+ }
126
+
127
+ /**
128
+ * Return true if current char is a number
129
+ *
130
+ * @return boolean
131
+ */
132
+ public function isNumeric() {
133
+ return $this->char() >= '0' && $this->char() <= '9';
134
+ }
135
+
136
+ /**
137
+ * Return true if current char is qoute or apostroph
138
+ *
139
+ * @return boolean
140
+ */
141
+ public function isQuote() {
142
+ return $this->char() == '"' || $this->char() == "'";
143
+ }
144
+
145
+ /**
146
+ * Return array of arguments for method.
147
+ *
148
+ * @return array
149
+ */
150
+ public function getMethodArgs() {
151
+ $value = array();
152
+ $numberStr = '';
153
+
154
+ while($this->next() && $this->char() != ')') {
155
+ if($this->isWhiteSpace() || $this->char() == ',') {
156
+ continue;
157
+ } else if($this->isNumeric()) {
158
+ $value[] = $this->getNumber();
159
+ } else {
160
+ $value[] = $this->getString();
161
+ }
162
+ }
163
+
164
+ return $value;
165
+ }
166
+
167
+ /**
168
+ * Return number value for method args
169
+ *
170
+ * @return float
171
+ */
172
+ public function getNumber() {
173
+ $value = $this->char();
174
+ while( ($this->isNumeric() || $this->char()=='.') && $this->next() ) {
175
+ $value.= $this->char();
176
+ }
177
+
178
+ if(!$this->isNumeric()) {
179
+ $this->prev();
180
+ }
181
+
182
+ return floatval($value);
183
+ }
184
+
185
+ }
lib/Varien/Http/Adapter/Curl.php ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Http
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * HTTP CURL Adapter
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Http
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Http_Adapter_Curl implements Zend_Http_Client_Adapter_Interface
35
+ {
36
+ /**
37
+ * Parameters array
38
+ *
39
+ * @var array
40
+ */
41
+ protected $_config = array();
42
+
43
+ protected $_resource;
44
+
45
+ /**
46
+ * Apply current configuration array to transport resource
47
+ */
48
+ protected function _applyConfig()
49
+ {
50
+ //curl_setopt();
51
+ if (isset($this->_config['timeout'])) {
52
+ curl_setopt($this->_getResource(), CURLOPT_TIMEOUT, $this->_config['timeout']);
53
+ }
54
+ if (isset($this->_config['maxredirects'])) {
55
+ curl_setopt($this->_getResource(), CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
56
+ }
57
+ if (isset($this->_config['proxy'])) {
58
+ curl_setopt ($this->_getResource(), CURLOPT_PROXY, $this->_config['proxy']);
59
+ }
60
+ if (isset($this->_config['ssl_cert'])) {
61
+ curl_setopt($this->_getResource(), CURLOPT_SSLCERT, $this->_config['ssl_cert']);
62
+ }
63
+
64
+ return $this;
65
+ }
66
+
67
+ /**
68
+ * Set the configuration array for the adapter
69
+ *
70
+ * @param array $config
71
+ */
72
+ public function setConfig($config = array())
73
+ {
74
+ $this->_config = $config;
75
+ return $this;
76
+ }
77
+
78
+ /**
79
+ * Connect to the remote server
80
+ *
81
+ * @param string $host
82
+ * @param int $port
83
+ * @param boolean $secure
84
+ * @deprecated since 1.4.0.0-rc1
85
+ */
86
+ public function connect($host, $port = 80, $secure = false)
87
+ {
88
+ //curl_setopt();
89
+ if (isset($this->_config['timeout'])) {
90
+ curl_setopt($this->_getResource(), CURLOPT_TIMEOUT, $this->_config['timeout']);
91
+ }
92
+ if (isset($this->_config['maxredirects'])) {
93
+ curl_setopt($this->_getResource(), CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
94
+ }
95
+ if (isset($this->_config['proxy'])) {
96
+ curl_setopt ($this->_getResource(), CURLOPT_PROXY, $this->_config['proxy']);
97
+ }
98
+
99
+ return $this;
100
+ }
101
+
102
+ /**
103
+ * Send request to the remote server
104
+ *
105
+ * @param string $method
106
+ * @param Zend_Uri_Http $url
107
+ * @param string $http_ver
108
+ * @param array $headers
109
+ * @param string $body
110
+ * @return string Request as text
111
+ */
112
+ public function write($method, $url, $http_ver = '1.1', $headers = array(), $body = '')
113
+ {
114
+ if ($url instanceof Zend_Uri_Http) {
115
+ $url = $url->getUri();
116
+ }
117
+ $this->_applyConfig();
118
+
119
+ // set url to post to
120
+ curl_setopt($this->_getResource(), CURLOPT_URL, $url);
121
+ curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true);
122
+ if ($method == Zend_Http_Client::POST) {
123
+ curl_setopt($this->_getResource(), CURLOPT_POST, true);
124
+ curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
125
+ }
126
+ elseif ($method == Zend_Http_Client::GET) {
127
+ curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true);
128
+ }
129
+
130
+ if( is_array($headers) ) {
131
+ curl_setopt($this->_getResource(), CURLOPT_HTTPHEADER, $headers);
132
+ }
133
+
134
+ curl_setopt($this->_getResource(), CURLOPT_HEADER, true);
135
+ curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYPEER, 0);
136
+ curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYHOST, 0);
137
+
138
+
139
+ return $body;
140
+ }
141
+
142
+ /**
143
+ * Read response from server
144
+ *
145
+ * @return string
146
+ */
147
+ public function read()
148
+ {
149
+ $response = curl_exec($this->_getResource());
150
+
151
+ // Remove 100 and 101 responses headers
152
+ if (Zend_Http_Response::extractCode($response) == 100 ||
153
+ Zend_Http_Response::extractCode($response) == 101) {
154
+ $response = preg_split('/^\r?$/m', $response, 2);
155
+ $response = trim($response[1]);
156
+ }
157
+
158
+ return $response;
159
+ }
160
+
161
+ /**
162
+ * Close the connection to the server
163
+ *
164
+ */
165
+ public function close()
166
+ {
167
+ curl_close($this->_getResource());
168
+ $this->_resource = null;
169
+ return $this;
170
+ }
171
+
172
+ protected function _getResource()
173
+ {
174
+ if (is_null($this->_resource)) {
175
+ $this->_resource = curl_init();
176
+ }
177
+ return $this->_resource;
178
+ }
179
+
180
+ public function getErrno()
181
+ {
182
+ return curl_errno($this->_getResource());
183
+ }
184
+
185
+ public function getError()
186
+ {
187
+ return curl_error($this->_getResource());
188
+ }
189
+
190
+ /**
191
+ * Get information regarding a specific transfer
192
+ *
193
+ * @param int $opt CURLINFO option
194
+ * @return mixed
195
+ */
196
+ public function getInfo($opt = 0)
197
+ {
198
+ return curl_getinfo($this->_getResource(), $opt);
199
+ }
200
+
201
+ /**
202
+ * curl_multi_* requests support
203
+ *
204
+ * @param array $urls
205
+ * @param array $options
206
+ * @return array
207
+ */
208
+ public function multiRequest($urls, $options = array())
209
+ {
210
+ $handles = array();
211
+ $result = array();
212
+
213
+ $multihandle = curl_multi_init();
214
+
215
+ foreach ($urls as $key => $url) {
216
+ $handles[$key] = curl_init();
217
+ curl_setopt($handles[$key], CURLOPT_URL, $url);
218
+ curl_setopt($handles[$key], CURLOPT_HEADER, 0);
219
+ curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
220
+ if (!empty($options)) {
221
+ curl_setopt_array($handles[$key], $options);
222
+ }
223
+ curl_multi_add_handle($multihandle, $handles[$key]);
224
+ }
225
+ $process = null;
226
+ do {
227
+ curl_multi_exec($multihandle, $process);
228
+ usleep(100);
229
+ } while ($process>0);
230
+
231
+ foreach ($handles as $key => $handle) {
232
+ $result[$key] = curl_multi_getcontent($handle);
233
+ curl_multi_remove_handle($multihandle, $handle);
234
+ }
235
+ curl_multi_close($multihandle);
236
+ return $result;
237
+ }
238
+ }
lib/Varien/Http/Client.php ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Http
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Varien HTTP Client
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Http
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Http_Client extends Zend_Http_Client
35
+ {
36
+ /**
37
+ * Internal flag to allow decoding of request body
38
+ *
39
+ * @var bool
40
+ */
41
+ protected $_urlEncodeBody = true;
42
+
43
+ public function __construct($uri = null, $config = null)
44
+ {
45
+ $this->config['useragent'] = 'Varien_Http_Client';
46
+
47
+ parent::__construct($uri, $config);
48
+ }
49
+
50
+ protected function _trySetCurlAdapter()
51
+ {
52
+ if (extension_loaded('curl')) {
53
+ $this->setAdapter(new Varien_Http_Adapter_Curl());
54
+ }
55
+ return $this;
56
+ }
57
+
58
+ public function request($method = null)
59
+ {
60
+ $this->_trySetCurlAdapter();
61
+ return parent::request($method);
62
+ }
63
+
64
+ /**
65
+ * Change value of internal flag to disable/enable custom prepare functionality
66
+ *
67
+ * @param bool $flag
68
+ * @return Varien_Http_Client
69
+ */
70
+ public function setUrlEncodeBody($flag)
71
+ {
72
+ $this->_urlEncodeBody = $flag;
73
+ return $this;
74
+ }
75
+
76
+ /**
77
+ * Adding custom functionality to decode data after
78
+ * standard prepare functionality
79
+ *
80
+ * @return string
81
+ */
82
+ protected function _prepareBody()
83
+ {
84
+ $body = parent::_prepareBody();
85
+
86
+ if (!$this->_urlEncodeBody && $body) {
87
+ $body = urldecode($body);
88
+ $this->setHeaders('Content-length', strlen($body));
89
+ }
90
+
91
+ return $body;
92
+ }
93
+ }
lib/Varien/Image.php ADDED
@@ -0,0 +1,318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Image
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Image handler library
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Image
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+ class Varien_Image
35
+ {
36
+ protected $_adapter;
37
+
38
+ protected $_fileName;
39
+
40
+ /**
41
+ * Constructor
42
+ *
43
+ * @param Varien_Image_Adapter $adapter. Default value is GD2
44
+ * @param string $fileName
45
+ * @return void
46
+ */
47
+ function __construct($fileName=null, $adapter=Varien_Image_Adapter::ADAPTER_GD2)
48
+ {
49
+ $this->_getAdapter($adapter);
50
+ $this->_fileName = $fileName;
51
+ if( isset($fileName) ) {
52
+ $this->open();
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Opens an image and creates image handle
58
+ *
59
+ * @access public
60
+ * @return void
61
+ */
62
+ public function open()
63
+ {
64
+ $this->_getAdapter()->checkDependencies();
65
+
66
+ if( !file_exists($this->_fileName) ) {
67
+ throw new Exception("File '{$this->_fileName}' does not exists.");
68
+ }
69
+
70
+ $this->_getAdapter()->open($this->_fileName);
71
+ }
72
+
73
+ /**
74
+ * Display handled image in your browser
75
+ *
76
+ * @access public
77
+ * @return void
78
+ */
79
+ public function display()
80
+ {
81
+ $this->_getAdapter()->display();
82
+ }
83
+
84
+ /**
85
+ * Save handled image into file
86
+ *
87
+ * @param string $destination. Default value is NULL
88
+ * @param string $newFileName. Default value is NULL
89
+ * @access public
90
+ * @return void
91
+ */
92
+ public function save($destination=null, $newFileName=null)
93
+ {
94
+ $this->_getAdapter()->save($destination, $newFileName);
95
+ }
96
+
97
+ /**
98
+ * Rotate an image.
99
+ *
100
+ * @param int $angle
101
+ * @access public
102
+ * @return void
103
+ */
104
+ public function rotate($angle)
105
+ {
106
+ $this->_getAdapter()->rotate($angle);
107
+ }
108
+
109
+ /**
110
+ * Crop an image.
111
+ *
112
+ * @param int $top. Default value is 0
113
+ * @param int $left. Default value is 0
114
+ * @param int $right. Default value is 0
115
+ * @param int $bottom. Default value is 0
116
+ * @access public
117
+ * @return void
118
+ */
119
+ public function crop($top=0, $left=0, $right=0, $bottom=0)
120
+ {
121
+ $this->_getAdapter()->crop($top, $left, $right, $bottom);
122
+ }
123
+
124
+ /**
125
+ * Resize an image
126
+ *
127
+ * @param int $width
128
+ * @param int $height
129
+ * @access public
130
+ * @return void
131
+ */
132
+ public function resize($width, $height = null)
133
+ {
134
+ $this->_getAdapter()->resize($width, $height);
135
+ }
136
+
137
+ public function keepAspectRatio($value)
138
+ {
139
+ return $this->_getAdapter()->keepAspectRatio($value);
140
+ }
141
+
142
+ public function keepFrame($value)
143
+ {
144
+ return $this->_getAdapter()->keepFrame($value);
145
+ }
146
+
147
+ public function keepTransparency($value)
148
+ {
149
+ return $this->_getAdapter()->keepTransparency($value);
150
+ }
151
+
152
+ public function constrainOnly($value)
153
+ {
154
+ return $this->_getAdapter()->constrainOnly($value);
155
+ }
156
+
157
+ public function backgroundColor($value)
158
+ {
159
+ return $this->_getAdapter()->backgroundColor($value);
160
+ }
161
+
162
+ /**
163
+ * Get/set quality, values in percentage from 0 to 100
164
+ *
165
+ * @param int $value
166
+ * @return int
167
+ */
168
+ public function quality($value)
169
+ {
170
+ return $this->_getAdapter()->quality($value);
171
+ }
172
+
173
+ /**
174
+ * Adds watermark to our image.
175
+ *
176
+ * @param string $watermarkImage. Absolute path to watermark image.
177
+ * @param int $positionX. Watermark X position.
178
+ * @param int $positionY. Watermark Y position.
179
+ * @param int $watermarkImageOpacity. Watermark image opacity.
180
+ * @param bool $repeat. Enable or disable watermark brick.
181
+ * @access public
182
+ * @return void
183
+ */
184
+ public function watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false)
185
+ {
186
+ if( !file_exists($watermarkImage) ) {
187
+ throw new Exception("Required file '{$watermarkImage}' does not exists.");
188
+ }
189
+ $this->_getAdapter()->watermark($watermarkImage, $positionX, $positionY, $watermarkImageOpacity, $repeat);
190
+ }
191
+
192
+ /**
193
+ * Get mime type of handled image
194
+ *
195
+ * @access public
196
+ * @return string
197
+ */
198
+ public function getMimeType()
199
+ {
200
+ return $this->_getAdapter()->getMimeType();
201
+ }
202
+
203
+ /**
204
+ * process
205
+ *
206
+ * @access public
207
+ * @return void
208
+ */
209
+ public function process()
210
+ {
211
+
212
+ }
213
+
214
+ /**
215
+ * instruction
216
+ *
217
+ * @access public
218
+ * @return void
219
+ */
220
+ public function instruction()
221
+ {
222
+
223
+ }
224
+
225
+ /**
226
+ * Set image background color
227
+ *
228
+ * @param int $color
229
+ * @access public
230
+ * @return void
231
+ */
232
+ public function setImageBackgroundColor($color)
233
+ {
234
+ $this->_getAdapter()->imageBackgroundColor = intval($color);
235
+ }
236
+
237
+ /**
238
+ * Set watermark position
239
+ *
240
+ * @param string $position
241
+ * @return Varien_Image
242
+ */
243
+ public function setWatermarkPosition($position)
244
+ {
245
+ $this->_getAdapter()->setWatermarkPosition($position);
246
+ return $this;
247
+ }
248
+
249
+ /**
250
+ * Set watermark image opacity
251
+ *
252
+ * @param int $imageOpacity
253
+ * @return Varien_Image
254
+ */
255
+ public function setWatermarkImageOpacity($imageOpacity)
256
+ {
257
+ $this->_getAdapter()->setWatermarkImageOpacity($imageOpacity);
258
+ return $this;
259
+ }
260
+
261
+ /**
262
+ * Set watermark width
263
+ *
264
+ * @param int $width
265
+ * @return Varien_Image
266
+ */
267
+ public function setWatermarkWidth($width)
268
+ {
269
+ $this->_getAdapter()->setWatermarkWidth($width);
270
+ return $this;
271
+ }
272
+
273
+ /**
274
+ * Set watermark heigth
275
+ *
276
+ * @param int $heigth
277
+ * @return Varien_Image
278
+ */
279
+ public function setWatermarkHeigth($heigth)
280
+ {
281
+ $this->_getAdapter()->setWatermarkHeigth($heigth);
282
+ return $this;
283
+ }
284
+
285
+ /**
286
+ * Retrieve image adapter object
287
+ *
288
+ * @param string $adapter
289
+ * @return Varien_Image_Adapter_Abstract
290
+ */
291
+ protected function _getAdapter($adapter=null)
292
+ {
293
+ if( !isset($this->_adapter) ) {
294
+ $this->_adapter = Varien_Image_Adapter::factory( $adapter );
295
+ }
296
+ return $this->_adapter;
297
+ }
298
+
299
+ /**
300
+ * Retrieve original image width
301
+ *
302
+ * @return int|null
303
+ */
304
+ public function getOriginalWidth()
305
+ {
306
+ return $this->_getAdapter()->getOriginalWidth();
307
+ }
308
+
309
+ /**
310
+ * Retrieve original image height
311
+ *
312
+ * @return int|null
313
+ */
314
+ public function getOriginalHeight()
315
+ {
316
+ return $this->_getAdapter()->getOriginalHeight();
317
+ }
318
+ }
lib/Varien/Image/Adapter.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Image
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Image_Adapter
29
+ {
30
+ const ADAPTER_GD = 'GD';
31
+ const ADAPTER_GD2 = 'GD2';
32
+ const ADAPTER_IM = 'IMAGEMAGIC';
33
+ const ADAPTER_IME = 'IMAGEMAGIC_EXTERNAL';
34
+
35
+ public static function factory($adapter)
36
+ {
37
+ switch( $adapter ) {
38
+ case self::ADAPTER_GD:
39
+ return new Varien_Image_Adapter_Gd();
40
+ break;
41
+
42
+ case self::ADAPTER_GD2:
43
+ return new Varien_Image_Adapter_Gd2();
44
+ break;
45
+
46
+ case self::ADAPTER_IM:
47
+ return new Varien_Image_Adapter_Imagemagic();
48
+ break;
49
+
50
+ case self::ADAPTER_IME:
51
+ return new Varien_Image_Adapter_ImagemagicExternal();
52
+ break;
53
+
54
+ default:
55
+ throw new Exception('Invalid adapter selected.');
56
+ break;
57
+ }
58
+ }
59
+ }
lib/Varien/Image/Adapter/Abstract.php ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Image
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * @file Abstract.php
29
+ * @author Magento Core Team <core@magentocommerce.com>
30
+ */
31
+
32
+ abstract class Varien_Image_Adapter_Abstract
33
+ {
34
+ public $fileName = null;
35
+ public $imageBackgroundColor = 0;
36
+
37
+ const POSITION_TOP_LEFT = 'top-left';
38
+ const POSITION_TOP_RIGHT = 'top-right';
39
+ const POSITION_BOTTOM_LEFT = 'bottom-left';
40
+ const POSITION_BOTTOM_RIGHT = 'bottom-right';
41
+ const POSITION_STRETCH = 'stretch';
42
+ const POSITION_TILE = 'tile';
43
+ const POSITION_CENTER = 'center';
44
+
45
+ protected $_fileType = null;
46
+ protected $_fileName = null;
47
+ protected $_fileMimeType = null;
48
+ protected $_fileSrcName = null;
49
+ protected $_fileSrcPath = null;
50
+ protected $_imageHandler = null;
51
+ protected $_imageSrcWidth = null;
52
+ protected $_imageSrcHeight = null;
53
+ protected $_requiredExtensions = null;
54
+ protected $_watermarkPosition = null;
55
+ protected $_watermarkWidth = null;
56
+ protected $_watermarkHeigth = null;
57
+ protected $_watermarkImageOpacity = null;
58
+ protected $_quality = null;
59
+
60
+ protected $_keepAspectRatio;
61
+ protected $_keepFrame;
62
+ protected $_keepTransparency;
63
+ protected $_backgroundColor;
64
+ protected $_constrainOnly;
65
+
66
+ abstract public function open($fileName);
67
+
68
+ abstract public function save($destination=null, $newName=null);
69
+
70
+ abstract public function display();
71
+
72
+ abstract public function resize($width=null, $height=null);
73
+
74
+ abstract public function rotate($angle);
75
+
76
+ abstract public function crop($top=0, $left=0, $right=0, $bottom=0);
77
+
78
+ abstract public function watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false);
79
+
80
+ abstract public function checkDependencies();
81
+
82
+ public function getMimeType()
83
+ {
84
+ if( $this->_fileType ) {
85
+ return $this->_fileType;
86
+ } else {
87
+ list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = getimagesize($this->_fileName);
88
+ $this->_fileMimeType = image_type_to_mime_type($this->_fileType);
89
+ return $this->_fileMimeType;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Retrieve Original Image Width
95
+ *
96
+ * @return int|null
97
+ */
98
+ public function getOriginalWidth()
99
+ {
100
+ $this->getMimeType();
101
+ return $this->_imageSrcWidth;
102
+ }
103
+
104
+ /**
105
+ * Retrieve Original Image Height
106
+ *
107
+ * @return int|null
108
+ */
109
+ public function getOriginalHeight()
110
+ {
111
+ $this->getMimeType();
112
+ return $this->_imageSrcHeight;
113
+ }
114
+
115
+ public function setWatermarkPosition($position)
116
+ {
117
+ $this->_watermarkPosition = $position;
118
+ return $this;
119
+ }
120
+
121
+ public function getWatermarkPosition()
122
+ {
123
+ return $this->_watermarkPosition;
124
+ }
125
+
126
+ public function setWatermarkImageOpacity($imageOpacity)
127
+ {
128
+ $this->_watermarkImageOpacity = $imageOpacity;
129
+ return $this;
130
+ }
131
+
132
+ public function getWatermarkImageOpacity()
133
+ {
134
+ return $this->_watermarkImageOpacity;
135
+ }
136
+
137
+ public function setWatermarkWidth($width)
138
+ {
139
+ $this->_watermarkWidth = $width;
140
+ return $this;
141
+ }
142
+
143
+ public function getWatermarkWidth()
144
+ {
145
+ return $this->_watermarkWidth;
146
+ }
147
+
148
+ public function setWatermarkHeigth($heigth)
149
+ {
150
+ $this->_watermarkHeigth = $heigth;
151
+ return $this;
152
+ }
153
+
154
+ public function getWatermarkHeigth()
155
+ {
156
+ return $this->_watermarkHeigth;
157
+ }
158
+
159
+
160
+ /**
161
+ * Get/set keepAspectRatio
162
+ *
163
+ * @param bool $value
164
+ * @return bool|Varien_Image_Adapter_Abstract
165
+ */
166
+ public function keepAspectRatio($value = null)
167
+ {
168
+ if (null !== $value) {
169
+ $this->_keepAspectRatio = (bool)$value;
170
+ }
171
+ return $this->_keepAspectRatio;
172
+ }
173
+
174
+ /**
175
+ * Get/set keepFrame
176
+ *
177
+ * @param bool $value
178
+ * @return bool
179
+ */
180
+ public function keepFrame($value = null)
181
+ {
182
+ if (null !== $value) {
183
+ $this->_keepFrame = (bool)$value;
184
+ }
185
+ return $this->_keepFrame;
186
+ }
187
+
188
+ /**
189
+ * Get/set keepTransparency
190
+ *
191
+ * @param bool $value
192
+ * @return bool
193
+ */
194
+ public function keepTransparency($value = null)
195
+ {
196
+ if (null !== $value) {
197
+ $this->_keepTransparency = (bool)$value;
198
+ }
199
+ return $this->_keepTransparency;
200
+ }
201
+
202
+ /**
203
+ * Get/set constrainOnly
204
+ *
205
+ * @param bool $value
206
+ * @return bool
207
+ */
208
+ public function constrainOnly($value = null)
209
+ {
210
+ if (null !== $value) {
211
+ $this->_constrainOnly = (bool)$value;
212
+ }
213
+ return $this->_constrainOnly;
214
+ }
215
+
216
+ /**
217
+ * Get/set quality, values in percentage from 0 to 100
218
+ *
219
+ * @param int $value
220
+ * @return int
221
+ */
222
+ public function quality($value = null)
223
+ {
224
+ if (null !== $value) {
225
+ $this->_quality = (int)$value;
226
+ }
227
+ return $this->_quality;
228
+ }
229
+
230
+ /**
231
+ * Get/set keepBackgroundColor
232
+ *
233
+ * @param array $value
234
+ * @return array
235
+ */
236
+ public function backgroundColor($value = null)
237
+ {
238
+ if (null !== $value) {
239
+ if ((!is_array($value)) || (3 !== count($value))) {
240
+ return;
241
+ }
242
+ foreach ($value as $color) {
243
+ if ((!is_integer($color)) || ($color < 0) || ($color > 255)) {
244
+ return;
245
+ }
246
+ }
247
+ }
248
+ $this->_backgroundColor = $value;
249
+ return $this->_backgroundColor;
250
+ }
251
+
252
+ protected function _getFileAttributes()
253
+ {
254
+ $pathinfo = pathinfo($this->_fileName);
255
+
256
+ $this->_fileSrcPath = $pathinfo['dirname'];
257
+ $this->_fileSrcName = $pathinfo['basename'];
258
+ }
259
+ }
lib/Varien/Image/Adapter/Gd2.php ADDED
@@ -0,0 +1,538 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Image
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Image_Adapter_Gd2 extends Varien_Image_Adapter_Abstract
29
+ {
30
+ protected $_requiredExtensions = Array("gd");
31
+ private static $_callbacks = array(
32
+ IMAGETYPE_GIF => array('output' => 'imagegif', 'create' => 'imagecreatefromgif'),
33
+ IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'),
34
+ IMAGETYPE_PNG => array('output' => 'imagepng', 'create' => 'imagecreatefrompng'),
35
+ IMAGETYPE_XBM => array('output' => 'imagexbm', 'create' => 'imagecreatefromxbm'),
36
+ IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromxbm'),
37
+ );
38
+
39
+ public function open($filename)
40
+ {
41
+ $this->_fileName = $filename;
42
+ $this->getMimeType();
43
+ $this->_getFileAttributes();
44
+ $this->_imageHandler = call_user_func($this->_getCallback('create'), $this->_fileName);
45
+ }
46
+
47
+ public function save($destination=null, $newName=null)
48
+ {
49
+ $fileName = ( !isset($destination) ) ? $this->_fileName : $destination;
50
+
51
+ if( isset($destination) && isset($newName) ) {
52
+ $fileName = $destination . "/" . $newName;
53
+ } elseif( isset($destination) && !isset($newName) ) {
54
+ $info = pathinfo($destination);
55
+ $fileName = $destination;
56
+ $destination = $info['dirname'];
57
+ } elseif( !isset($destination) && isset($newName) ) {
58
+ $fileName = $this->_fileSrcPath . "/" . $newName;
59
+ } else {
60
+ $fileName = $this->_fileSrcPath . $this->_fileSrcName;
61
+ }
62
+
63
+ $destinationDir = ( isset($destination) ) ? $destination : $this->_fileSrcPath;
64
+
65
+ if( !is_writable($destinationDir) ) {
66
+ try {
67
+ $io = new Varien_Io_File();
68
+ $io->mkdir($destination);
69
+ } catch (Exception $e) {
70
+ throw new Exception("Unable to write file into directory '{$destinationDir}'. Access forbidden.");
71
+ }
72
+ }
73
+
74
+ // keep alpha transparency
75
+ $isAlpha = false;
76
+ $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
77
+ if ($isAlpha) {
78
+ $this->_fillBackgroundColor($this->_imageHandler);
79
+ }
80
+
81
+ $functionParameters = array();
82
+ $functionParameters[] = $this->_imageHandler;
83
+ $functionParameters[] = $fileName;
84
+
85
+ // set quality param for JPG file type
86
+ if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_JPEG)
87
+ {
88
+ $functionParameters[] = $this->quality();
89
+ }
90
+
91
+ // set quality param for PNG file type
92
+ if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_PNG)
93
+ {
94
+ $quality = round(($this->quality() / 100) * 10);
95
+ if ($quality < 1) {
96
+ $quality = 1;
97
+ } elseif ($quality > 10) {
98
+ $quality = 10;
99
+ }
100
+ $quality = 10 - $quality;
101
+ $functionParameters[] = $quality;
102
+ }
103
+
104
+ call_user_func_array($this->_getCallback('output'), $functionParameters);
105
+ }
106
+
107
+ public function display()
108
+ {
109
+ header("Content-type: ".$this->getMimeType());
110
+ call_user_func($this->_getCallback('output'), $this->_imageHandler);
111
+ }
112
+
113
+ /**
114
+ * Obtain function name, basing on image type and callback type
115
+ *
116
+ * @param string $callbackType
117
+ * @param int $fileType
118
+ * @return string
119
+ * @throws Exception
120
+ */
121
+ private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
122
+ {
123
+ if (null === $fileType) {
124
+ $fileType = $this->_fileType;
125
+ }
126
+ if (empty(self::$_callbacks[$fileType])) {
127
+ throw new Exception($unsupportedText);
128
+ }
129
+ if (empty(self::$_callbacks[$fileType][$callbackType])) {
130
+ throw new Exception('Callback not found.');
131
+ }
132
+ return self::$_callbacks[$fileType][$callbackType];
133
+ }
134
+
135
+ private function _fillBackgroundColor(&$imageResourceTo)
136
+ {
137
+ // try to keep transparency, if any
138
+ if ($this->_keepTransparency) {
139
+ $isAlpha = false;
140
+ $transparentIndex = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
141
+
142
+ if ($isAlpha || false === $transparentIndex || $transparentIndex < 0) {
143
+ $transparencyCondition = false;
144
+ } else {
145
+ $transparencyCondition = $transparentIndex < imagecolorstotal($this->_imageHandler);
146
+ }
147
+ try {
148
+ // fill truecolor png with alpha transparency
149
+ if ($isAlpha) {
150
+
151
+ if (!imagealphablending($imageResourceTo, false)) {
152
+ throw new Exception('Failed to set alpha blending for PNG image.');
153
+ }
154
+ $transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
155
+ if (false === $transparentAlphaColor) {
156
+ throw new Exception('Failed to allocate alpha transparency for PNG image.');
157
+ }
158
+ if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
159
+ throw new Exception('Failed to fill PNG image with alpha transparency.');
160
+ }
161
+ if (!imagesavealpha($imageResourceTo, true)) {
162
+ throw new Exception('Failed to save alpha transparency into PNG image.');
163
+ }
164
+
165
+ return $transparentAlphaColor;
166
+ }
167
+ // fill image with indexed non-alpha transparency
168
+ elseif ($transparentIndex) {
169
+ $transparentColor = false;
170
+ if ($transparentIndex >=0 && $transparentIndex <= imagecolorstotal($this->_imageHandler)) {
171
+ list($r, $g, $b) = array_values(imagecolorsforindex($this->_imageHandler, $transparentIndex));
172
+ $transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
173
+ }
174
+ if (false === $transparentColor) {
175
+ throw new Exception('Failed to allocate transparent color for image.');
176
+ }
177
+ if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
178
+ throw new Exception('Failed to fill image with transparency.');
179
+ }
180
+ imagecolortransparent($imageResourceTo, $transparentColor);
181
+ return $transparentColor;
182
+ }
183
+ }
184
+ catch (Exception $e) {
185
+ // fallback to default background color
186
+ }
187
+ }
188
+ list($r, $g, $b) = $this->_backgroundColor;
189
+ $color = imagecolorallocate($imageResourceTo, $r, $g, $b);
190
+ if (!imagefill($imageResourceTo, 0, 0, $color)) {
191
+ throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
192
+ }
193
+
194
+ return $color;
195
+ }
196
+
197
+ /**
198
+ * Gives true for a PNG with alpha, false otherwise
199
+ *
200
+ * @param string $fileName
201
+ * @return boolean
202
+ */
203
+
204
+ public function checkAlpha($fileName)
205
+ {
206
+ return ((ord(file_get_contents($fileName, false, null, 25, 1)) & 6) & 4) == 4;
207
+ }
208
+
209
+ private function _getTransparency($imageResource, $fileType, &$isAlpha = false, &$isTrueColor = false)
210
+ {
211
+ $isAlpha = false;
212
+ $isTrueColor = false;
213
+ // assume that transparency is supported by gif/png only
214
+ if ((IMAGETYPE_GIF === $fileType) || (IMAGETYPE_PNG === $fileType)) {
215
+ // check for specific transparent color
216
+ $transparentIndex = imagecolortransparent($imageResource);
217
+ if ($transparentIndex >= 0) {
218
+ return $transparentIndex;
219
+ }
220
+ // assume that truecolor PNG has transparency
221
+ elseif (IMAGETYPE_PNG === $fileType) {
222
+ $isAlpha = $this->checkAlpha($this->_fileName);
223
+ $isTrueColor = true;
224
+ return $transparentIndex; // -1
225
+ }
226
+ }
227
+ if (IMAGETYPE_JPEG === $fileType) {
228
+ $isTrueColor = true;
229
+ }
230
+ return false;
231
+ }
232
+
233
+ /**
234
+ * Change the image size
235
+ *
236
+ * @param int $frameWidth
237
+ * @param int $frameHeight
238
+ */
239
+ public function resize($frameWidth = null, $frameHeight = null)
240
+ {
241
+ if (empty($frameWidth) && empty($frameHeight)) {
242
+ throw new Exception('Invalid image dimensions.');
243
+ }
244
+
245
+ // calculate lacking dimension
246
+ if (!$this->_keepFrame) {
247
+ if (null === $frameWidth) {
248
+ $frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight));
249
+ }
250
+ elseif (null === $frameHeight) {
251
+ $frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth));
252
+ }
253
+ }
254
+ else {
255
+ if (null === $frameWidth) {
256
+ $frameWidth = $frameHeight;
257
+ }
258
+ elseif (null === $frameHeight) {
259
+ $frameHeight = $frameWidth;
260
+ }
261
+ }
262
+
263
+ // define coordinates of image inside new frame
264
+ $srcX = 0;
265
+ $srcY = 0;
266
+ $dstX = 0;
267
+ $dstY = 0;
268
+ $dstWidth = $frameWidth;
269
+ $dstHeight = $frameHeight;
270
+ if ($this->_keepAspectRatio) {
271
+ // do not make picture bigger, than it is, if required
272
+ if ($this->_constrainOnly) {
273
+ if (($frameWidth >= $this->_imageSrcWidth) && ($frameHeight >= $this->_imageSrcHeight)) {
274
+ $dstWidth = $this->_imageSrcWidth;
275
+ $dstHeight = $this->_imageSrcHeight;
276
+ }
277
+ }
278
+ // keep aspect ratio
279
+ if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) {
280
+ $dstHeight = round(($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight);
281
+ } else {
282
+ $dstWidth = round(($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth);
283
+ }
284
+ }
285
+ // define position in center (TODO: add positions option)
286
+ $dstY = round(($frameHeight - $dstHeight) / 2);
287
+ $dstX = round(($frameWidth - $dstWidth) / 2);
288
+
289
+ // get rid of frame (fallback to zero position coordinates)
290
+ if (!$this->_keepFrame) {
291
+ $frameWidth = $dstWidth;
292
+ $frameHeight = $dstHeight;
293
+ $dstY = 0;
294
+ $dstX = 0;
295
+ }
296
+
297
+ // create new image
298
+ $isAlpha = false;
299
+ $isTrueColor = false;
300
+ $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
301
+ if ($isTrueColor) {
302
+ $newImage = imagecreatetruecolor($frameWidth, $frameHeight);
303
+ }
304
+ else {
305
+ $newImage = imagecreate($frameWidth, $frameHeight);
306
+ }
307
+
308
+ // fill new image with required color
309
+ $this->_fillBackgroundColor($newImage);
310
+
311
+ // resample source image and copy it into new frame
312
+ imagecopyresampled(
313
+ $newImage,
314
+ $this->_imageHandler,
315
+ $dstX, $dstY,
316
+ $srcX, $srcY,
317
+ $dstWidth, $dstHeight,
318
+ $this->_imageSrcWidth, $this->_imageSrcHeight
319
+ );
320
+ $this->_imageHandler = $newImage;
321
+ $this->refreshImageDimensions();
322
+ }
323
+
324
+ public function rotate($angle)
325
+ {
326
+ /*
327
+ $isAlpha = false;
328
+ $backgroundColor = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
329
+ list($r, $g, $b) = $this->_backgroundColor;
330
+ if ($isAlpha) {
331
+ $backgroundColor = imagecolorallocatealpha($this->_imageHandler, 0, 0, 0, 127);
332
+ }
333
+ elseif (false === $backgroundColor) {
334
+ $backgroundColor = imagecolorallocate($this->_imageHandler, $r, $g, $b);
335
+ }
336
+ $this->_imageHandler = imagerotate($this->_imageHandler, $angle, $backgroundColor);
337
+ //*/
338
+ $this->_imageHandler = imagerotate($this->_imageHandler, $angle, $this->imageBackgroundColor);
339
+ $this->refreshImageDimensions();
340
+ }
341
+
342
+ public function watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false)
343
+ {
344
+ list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($watermarkImage);
345
+ $this->_getFileAttributes();
346
+ $watermark = call_user_func($this->_getCallback(
347
+ 'create',
348
+ $watermarkFileType,
349
+ 'Unsupported watermark image format.'
350
+ ), $watermarkImage);
351
+
352
+ $merged = false;
353
+
354
+ if ($this->getWatermarkWidth() &&
355
+ $this->getWatermarkHeigth() &&
356
+ ($this->getWatermarkPosition() != self::POSITION_STRETCH)
357
+ ) {
358
+ $newWatermark = imagecreatetruecolor($this->getWatermarkWidth(), $this->getWatermarkHeigth());
359
+ imagealphablending($newWatermark, false);
360
+ $col = imagecolorallocate($newWatermark, 255, 255, 255);
361
+ imagecolortransparent($newWatermark, $col);
362
+ imagefilledrectangle($newWatermark, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), $col);
363
+ imagealphablending($newWatermark, true);
364
+ imageSaveAlpha($newWatermark, true);
365
+ imagecopyresampled(
366
+ $newWatermark,
367
+ $watermark,
368
+ 0, 0, 0, 0,
369
+ $this->getWatermarkWidth(), $this->getWatermarkHeigth(),
370
+ imagesx($watermark), imagesy($watermark)
371
+ );
372
+ $watermark = $newWatermark;
373
+ }
374
+
375
+ if( $this->getWatermarkPosition() == self::POSITION_TILE ) {
376
+ $repeat = true;
377
+ } elseif( $this->getWatermarkPosition() == self::POSITION_STRETCH ) {
378
+
379
+ $newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
380
+ imagealphablending($newWatermark, false);
381
+ $col = imagecolorallocate($newWatermark, 255, 255, 255);
382
+ imagecolortransparent($newWatermark, $col);
383
+ imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col);
384
+ imagealphablending($newWatermark, true);
385
+ imageSaveAlpha($newWatermark, true);
386
+ imagecopyresampled(
387
+ $newWatermark,
388
+ $watermark,
389
+ 0, 0, 0, 0,
390
+ $this->_imageSrcWidth, $this->_imageSrcHeight,
391
+ imagesx($watermark), imagesy($watermark)
392
+ );
393
+ $watermark = $newWatermark;
394
+
395
+ } elseif( $this->getWatermarkPosition() == self::POSITION_CENTER ) {
396
+ $positionX = ($this->_imageSrcWidth/2 - imagesx($watermark)/2);
397
+ $positionY = ($this->_imageSrcHeight/2 - imagesy($watermark)/2);
398
+ imagecopymerge(
399
+ $this->_imageHandler,
400
+ $watermark,
401
+ $positionX, $positionY,
402
+ 0, 0,
403
+ imagesx($watermark), imagesy($watermark),
404
+ $this->getWatermarkImageOpacity()
405
+ );
406
+ } elseif( $this->getWatermarkPosition() == self::POSITION_TOP_RIGHT ) {
407
+ $positionX = ($this->_imageSrcWidth - imagesx($watermark));
408
+ imagecopymerge(
409
+ $this->_imageHandler,
410
+ $watermark,
411
+ $positionX, $positionY,
412
+ 0, 0,
413
+ imagesx($watermark), imagesy($watermark),
414
+ $this->getWatermarkImageOpacity()
415
+ );
416
+ } elseif( $this->getWatermarkPosition() == self::POSITION_TOP_LEFT ) {
417
+ imagecopymerge(
418
+ $this->_imageHandler,
419
+ $watermark,
420
+ $positionX, $positionY,
421
+ 0, 0,
422
+ imagesx($watermark), imagesy($watermark),
423
+ $this->getWatermarkImageOpacity()
424
+ );
425
+ } elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT ) {
426
+ $positionX = ($this->_imageSrcWidth - imagesx($watermark));
427
+ $positionY = ($this->_imageSrcHeight - imagesy($watermark));
428
+ imagecopymerge(
429
+ $this->_imageHandler,
430
+ $watermark,
431
+ $positionX, $positionY,
432
+ 0, 0,
433
+ imagesx($watermark), imagesy($watermark),
434
+ $this->getWatermarkImageOpacity()
435
+ );
436
+ } elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT ) {
437
+ $positionY = ($this->_imageSrcHeight - imagesy($watermark));
438
+ imagecopymerge(
439
+ $this->_imageHandler,
440
+ $watermark,
441
+ $positionX, $positionY,
442
+ 0, 0,
443
+ imagesx($watermark), imagesy($watermark),
444
+ $this->getWatermarkImageOpacity()
445
+ );
446
+ }
447
+
448
+ if( $repeat === false && $merged === false ) {
449
+ imagecopymerge(
450
+ $this->_imageHandler,
451
+ $watermark,
452
+ $positionX, $positionY,
453
+ 0, 0,
454
+ imagesx($watermark), imagesy($watermark),
455
+ $this->getWatermarkImageOpacity()
456
+ );
457
+ } else {
458
+ $offsetX = $positionX;
459
+ $offsetY = $positionY;
460
+ while( $offsetY <= ($this->_imageSrcHeight+imagesy($watermark)) ) {
461
+ while( $offsetX <= ($this->_imageSrcWidth+imagesx($watermark)) ) {
462
+ imagecopymerge(
463
+ $this->_imageHandler,
464
+ $watermark,
465
+ $offsetX, $offsetY,
466
+ 0, 0,
467
+ imagesx($watermark), imagesy($watermark),
468
+ $this->getWatermarkImageOpacity()
469
+ );
470
+ $offsetX += imagesx($watermark);
471
+ }
472
+ $offsetX = $positionX;
473
+ $offsetY += imagesy($watermark);
474
+ }
475
+ }
476
+
477
+ imagedestroy($watermark);
478
+ $this->refreshImageDimensions();
479
+ }
480
+
481
+ public function crop($top=0, $left=0, $right=0, $bottom=0)
482
+ {
483
+ if( $left == 0 && $top == 0 && $right == 0 && $bottom == 0 ) {
484
+ return;
485
+ }
486
+
487
+ $newWidth = $this->_imageSrcWidth - $left - $right;
488
+ $newHeight = $this->_imageSrcHeight - $top - $bottom;
489
+
490
+ $canvas = imagecreatetruecolor($newWidth, $newHeight);
491
+
492
+ if ($this->_fileType == IMAGETYPE_PNG) {
493
+ $this->_saveAlpha($canvas);
494
+ }
495
+
496
+ imagecopyresampled(
497
+ $canvas,
498
+ $this->_imageHandler,
499
+ 0, 0, $left, $top,
500
+ $newWidth, $newHeight,
501
+ $newWidth, $newHeight
502
+ );
503
+
504
+ $this->_imageHandler = $canvas;
505
+ $this->refreshImageDimensions();
506
+ }
507
+
508
+ public function checkDependencies()
509
+ {
510
+ foreach( $this->_requiredExtensions as $value ) {
511
+ if( !extension_loaded($value) ) {
512
+ throw new Exception("Required PHP extension '{$value}' was not loaded.");
513
+ }
514
+ }
515
+ }
516
+
517
+ private function refreshImageDimensions()
518
+ {
519
+ $this->_imageSrcWidth = imagesx($this->_imageHandler);
520
+ $this->_imageSrcHeight = imagesy($this->_imageHandler);
521
+ }
522
+
523
+ function __destruct()
524
+ {
525
+ @imagedestroy($this->_imageHandler);
526
+ }
527
+
528
+ /*
529
+ * Fixes saving PNG alpha channel
530
+ */
531
+ private function _saveAlpha($imageHandler)
532
+ {
533
+ $background = imagecolorallocate($imageHandler, 0, 0, 0);
534
+ ImageColorTransparent($imageHandler, $background);
535
+ imagealphablending($imageHandler, false);
536
+ imagesavealpha($imageHandler, true);
537
+ }
538
+ }
lib/Varien/Io/Abstract.php ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Io
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Install and upgrade client abstract class
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Io
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Io_Abstract implements Varien_Io_Interface
36
+ {
37
+ /**
38
+ * If this variable is set to true, our library will be able to automaticaly
39
+ * create non-existant directories
40
+ *
41
+ * @var bool
42
+ */
43
+ protected $_allowCreateFolders = false;
44
+
45
+ /**
46
+ * Allow automaticaly create non-existant directories
47
+ *
48
+ * @param bool $flag
49
+ * @return Varien_Io_Abstract
50
+ */
51
+ public function setAllowCreateFolders($flag)
52
+ {
53
+ $this->_allowCreateFolders = (bool)$flag;
54
+ return $this;
55
+ }
56
+
57
+ /**
58
+ * Open a connection
59
+ *
60
+ * @param array $config
61
+ * @return bool
62
+ */
63
+ public function open(array $args = array())
64
+ {
65
+ return false;
66
+ }
67
+
68
+ public function dirsep()
69
+ {
70
+ return '/';
71
+ }
72
+
73
+ public function getCleanPath($path)
74
+ {
75
+ if (empty($path)) {
76
+ return './';
77
+ }
78
+
79
+ $path = trim(preg_replace("/\\\\/", "/", (string)$path));
80
+
81
+ if (!preg_match("/(\.\w{1,4})$/", $path) && !preg_match("/\?[^\\/]+$/", $path) && !preg_match("/\\/$/", $path)) {
82
+ $path .= '/';
83
+ }
84
+
85
+ $matches = array();
86
+ $pattern = "/^(\\/|\w:\\/|https?:\\/\\/[^\\/]+\\/)?(.*)$/i";
87
+ preg_match_all($pattern, $path, $matches, PREG_SET_ORDER);
88
+
89
+ $pathTokR = $matches[0][1];
90
+ $pathTokP = $matches[0][2];
91
+
92
+ $pathTokP = preg_replace(array("/^\\/+/", "/\\/+/"), array("", "/"), $pathTokP);
93
+
94
+ $pathParts = explode("/", $pathTokP);
95
+ $realPathParts = array();
96
+
97
+ for ($i = 0, $realPathParts = array(); $i < count($pathParts); $i++) {
98
+ if ($pathParts[$i] == '.') {
99
+ continue;
100
+ }
101
+ elseif ($pathParts[$i] == '..') {
102
+ if ((isset($realPathParts[0]) && $realPathParts[0] != '..') || ($pathTokR != "")) {
103
+ array_pop($realPathParts);
104
+ continue;
105
+ }
106
+ }
107
+
108
+ array_push($realPathParts, $pathParts[$i]);
109
+ }
110
+
111
+ return $pathTokR . implode('/', $realPathParts);
112
+ }
113
+
114
+ public function allowedPath($haystackPath, $needlePath)
115
+ {
116
+ return strpos($this->getCleanPath($haystackPath), $this->getCleanPath($needlePath)) === 0;
117
+ }
118
+ }
lib/Varien/Io/Exception.php ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Convert
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Io exception
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Io
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Io_Exception extends Varien_Exception
36
+ {
37
+ }
lib/Varien/Io/File.php ADDED
@@ -0,0 +1,813 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Io
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Filesystem client
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Io
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Io_File extends Varien_Io_Abstract
36
+ {
37
+ /**
38
+ * Save initial working directory
39
+ *
40
+ * @var string
41
+ */
42
+ protected $_iwd;
43
+
44
+ /**
45
+ * Use virtual current working directory for application integrity
46
+ *
47
+ * @var string
48
+ */
49
+ protected $_cwd;
50
+
51
+ /**
52
+ * Used to grep ls() output
53
+ *
54
+ * @const
55
+ */
56
+ const GREP_FILES = 'files_only';
57
+
58
+ /**
59
+ * Used to grep ls() output
60
+ *
61
+ * @const
62
+ */
63
+ const GREP_DIRS = 'dirs_only';
64
+
65
+ /**
66
+ * If this variable is set to TRUE, our library will be able to automaticaly create
67
+ * non-existant directories.
68
+ *
69
+ * @var bool
70
+ * @access protected
71
+ */
72
+ protected $_allowCreateFolders = false;
73
+
74
+ /**
75
+ * Stream open file pointer
76
+ *
77
+ * @var resource
78
+ */
79
+ protected $_streamHandler;
80
+
81
+ /**
82
+ * Stream mode filename
83
+ *
84
+ * @var string
85
+ */
86
+ protected $_streamFileName;
87
+
88
+ /**
89
+ * Stream mode chmod
90
+ *
91
+ * @var string
92
+ */
93
+ protected $_streamChmod;
94
+
95
+ /**
96
+ * Lock file
97
+ *
98
+ * @var bool
99
+ */
100
+ protected $_streamLocked = false;
101
+
102
+ /**
103
+ * Destruct
104
+ */
105
+ public function __destruct()
106
+ {
107
+ if ($this->_streamHandler) {
108
+ $this->streamClose();
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Open file in stream mode
114
+ * For set folder for file use open method
115
+ *
116
+ * @param string $fileName
117
+ * @param string $mode
118
+ * @return bool
119
+ */
120
+ public function streamOpen($fileName, $mode = 'w+', $chmod = 0666)
121
+ {
122
+ $writeableMode = preg_match('#^[wax]#i', $mode);
123
+ if ($writeableMode && !is_writeable($this->_cwd)) {
124
+ throw new Exception('Permission denied for write to ' . $this->_cwd);
125
+ }
126
+
127
+ if (!ini_get('auto_detect_line_endings')) {
128
+ ini_set('auto_detect_line_endings', 1);
129
+ }
130
+
131
+ @chdir($this->_cwd);
132
+ $this->_streamHandler = @fopen($fileName, $mode);
133
+ @chdir($this->_iwd);
134
+ if ($this->_streamHandler === false) {
135
+ throw new Exception('Error write to file ' . $fileName);
136
+ }
137
+
138
+ $this->_streamFileName = $fileName;
139
+ $this->_streamChmod = $chmod;
140
+ return true;
141
+ }
142
+
143
+ /**
144
+ * Lock file
145
+ *
146
+ * @return bool
147
+ */
148
+ public function streamLock($exclusive = true)
149
+ {
150
+ if (!$this->_streamHandler) {
151
+ return false;
152
+ }
153
+ $this->_streamLocked = true;
154
+ $lock = $exclusive ? LOCK_EX : LOCK_SH;
155
+ return flock($this->_streamHandler, $lock);
156
+ }
157
+
158
+ /**
159
+ * Unlock file
160
+ *
161
+ * @return bool
162
+ */
163
+ public function streamUnlock()
164
+ {
165
+ if (!$this->_streamHandler || !$this->_streamLocked) {
166
+ return false;
167
+ }
168
+ $this->_streamLocked = false;
169
+ return flock($this->_streamHandler, LOCK_UN);
170
+ }
171
+
172
+ /**
173
+ * Binary-safe file read
174
+ *
175
+ * @param int $length
176
+ * @return string
177
+ */
178
+ public function streamRead($length = 1024)
179
+ {
180
+ if (!$this->_streamHandler) {
181
+ return false;
182
+ }
183
+ if (feof($this->_streamHandler)) {
184
+ return false;
185
+ }
186
+ return @fgets($this->_streamHandler, $length);
187
+ }
188
+
189
+ /**
190
+ * Gets line from file pointer and parse for CSV fields
191
+ *
192
+ * @return string
193
+ */
194
+ public function streamReadCsv($delimiter = ',', $enclosure = '"')
195
+ {
196
+ if (!$this->_streamHandler) {
197
+ return false;
198
+ }
199
+ return @fgetcsv($this->_streamHandler, 0, $delimiter, $enclosure);
200
+ }
201
+
202
+ /**
203
+ * Binary-safe file write
204
+ *
205
+ * @param string $str
206
+ * @return bool
207
+ */
208
+ public function streamWrite($str)
209
+ {
210
+ if (!$this->_streamHandler) {
211
+ return false;
212
+ }
213
+ return @fwrite($this->_streamHandler, $str);
214
+ }
215
+
216
+ /**
217
+ * Format line as CSV and write to file pointer
218
+ *
219
+ * @param array $row
220
+ * @param string $delimiter
221
+ * @param string $enclosure
222
+ * @return bool|int
223
+ */
224
+ public function streamWriteCsv(array $row, $delimiter = ',', $enclosure = '"')
225
+ {
226
+ if (!$this->_streamHandler) {
227
+ return false;
228
+ }
229
+ return @fputcsv($this->_streamHandler, $row, $delimiter, $enclosure);
230
+ }
231
+
232
+ /**
233
+ * Close an open file pointer
234
+ * Set chmod on a file
235
+ *
236
+ * @return bool
237
+ */
238
+ public function streamClose()
239
+ {
240
+ if (!$this->_streamHandler) {
241
+ return false;
242
+ }
243
+
244
+ if ($this->_streamLocked) {
245
+ $this->streamUnlock();
246
+ }
247
+ @fclose($this->_streamHandler);
248
+ $this->chmod($this->_streamFileName, $this->_streamChmod);
249
+ return true;
250
+ }
251
+
252
+ /**
253
+ * Retrieve open file statistic
254
+ *
255
+ * @param string $part the part of statistic
256
+ * @param mixed $default default value for part
257
+ * @return array|bool
258
+ */
259
+ public function streamStat($part = null, $default = null)
260
+ {
261
+ if (!$this->_streamHandler) {
262
+ return false;
263
+ }
264
+ $stat = @fstat($this->_streamHandler);
265
+ if (!is_null($part)) {
266
+ return isset($stat[$part]) ? $stat[$part] : $default;
267
+ }
268
+ return $stat;
269
+ }
270
+
271
+ /**
272
+ * Retrieve stream methods exception
273
+ *
274
+ * @return Exception
275
+ */
276
+ public function getStreamException()
277
+ {
278
+ return $this->_streamException;
279
+ }
280
+
281
+ /**
282
+ * Open a connection
283
+ *
284
+ * Possible arguments:
285
+ * - path default current path
286
+ *
287
+ * @param array $args
288
+ * @return boolean
289
+ */
290
+ public function open(array $args=array())
291
+ {
292
+ if (!empty($args['path'])) {
293
+ if ($args['path']) {
294
+ if($this->_allowCreateFolders ) {
295
+ $this->_createDestinationFolder($args['path']);
296
+ }
297
+ }
298
+ }
299
+
300
+ $this->_iwd = getcwd();
301
+ $this->cd(!empty($args['path']) ? $args['path'] : $this->_iwd);
302
+ return true;
303
+ }
304
+
305
+ /**
306
+ * Used to set {@link _allowCreateFolders} value
307
+ *
308
+ * @param mixed $flag
309
+ * @access public
310
+ * @return void
311
+ */
312
+ public function setAllowCreateFolders($flag)
313
+ {
314
+ $this->_allowCreateFolders = $flag;
315
+ return $this;
316
+ }
317
+
318
+ /**
319
+ * Close a connection
320
+ *
321
+ * @return boolean
322
+ */
323
+ public function close()
324
+ {
325
+ return true;
326
+ }
327
+
328
+ /**
329
+ * Create a directory
330
+ *
331
+ * @param string $dir
332
+ * @param int $mode
333
+ * @param boolean $recursive
334
+ * @return boolean
335
+ */
336
+ public function mkdir($dir, $mode=0777, $recursive=true)
337
+ {
338
+ if ($this->_cwd) {
339
+ chdir($this->_cwd);
340
+ }
341
+
342
+ $result = @mkdir($dir, $mode, $recursive);
343
+ if ($result) {
344
+ @chmod($dir, $mode);
345
+ }
346
+ if ($this->_iwd) {
347
+ chdir($this->_iwd);
348
+ }
349
+ return $result;
350
+ }
351
+
352
+ /**
353
+ * Delete a directory
354
+ *
355
+ * @param string $dir
356
+ * @return boolean
357
+ */
358
+ public function rmdir($dir, $recursive = false)
359
+ {
360
+ if ($this->_cwd) {
361
+ @chdir($this->_cwd);
362
+ }
363
+ $result = self::rmdirRecursive($dir, $recursive);
364
+ @chdir($this->_iwd);
365
+ return $result;
366
+ }
367
+
368
+ /**
369
+ * Delete a directory recursively
370
+ * @param string $dir
371
+ * @param bool $recursive
372
+ * @return bool
373
+ */
374
+ public static function rmdirRecursive($dir, $recursive = true)
375
+ {
376
+ if ($recursive) {
377
+ if (is_dir($dir)) {
378
+ foreach (scandir($dir) as $item) {
379
+ if (!strcmp($item, '.') || !strcmp($item, '..')) {
380
+ continue;
381
+ }
382
+ self::rmdirRecursive($dir . "/" . $item, $recursive);
383
+ }
384
+ $result = @rmdir($dir);
385
+ } else {
386
+ $result = @unlink($dir);
387
+ }
388
+ } else {
389
+ $result = @rmdir($dir);
390
+ }
391
+ return $result;
392
+ }
393
+
394
+ /**
395
+ * Get current working directory
396
+ *
397
+ * @return string
398
+ */
399
+ public function pwd()
400
+ {
401
+ return $this->_cwd;
402
+ }
403
+
404
+ /**
405
+ * Change current working directory
406
+ *
407
+ * @param string $dir
408
+ * @return boolean
409
+ */
410
+ public function cd($dir)
411
+ {
412
+ if( is_dir($dir) ) {
413
+ @chdir($this->_iwd);
414
+ $this->_cwd = realpath($dir);
415
+ return true;
416
+ } else {
417
+ throw new Exception('Unable to list current working directory.');
418
+ return false;
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Read a file to result, file or stream
424
+ *
425
+ * If $dest is null the output will be returned.
426
+ * Otherwise it will be saved to the file or stream and operation result is returned.
427
+ *
428
+ * @param string $filename
429
+ * @param string|resource $dest
430
+ * @return boolean|string
431
+ */
432
+ public function read($filename, $dest=null)
433
+ {
434
+ if (!is_null($dest)) {
435
+ chdir($this->_cwd);
436
+ $result = @copy($filename, $dest);
437
+ chdir($this->_iwd);
438
+ return $result;
439
+ }
440
+
441
+ chdir($this->_cwd);
442
+ $result = @file_get_contents($filename);
443
+ chdir($this->_iwd);
444
+
445
+ return $result;
446
+ }
447
+
448
+ /**
449
+ * Write a file from string, file or stream
450
+ *
451
+ * @param string $filename
452
+ * @param string|resource $src
453
+ * @return int|boolean
454
+ */
455
+ public function write($filename, $src, $mode=null)
456
+ {
457
+ if (is_string($src) && is_readable($src)) {
458
+ $src = realpath($src);
459
+ $srcIsFile = true;
460
+ } elseif (is_string($src) || is_resource($src)) {
461
+ $srcIsFile = false;
462
+ } else {
463
+ return false;
464
+ }
465
+ @chdir($this->_cwd);
466
+
467
+ if (file_exists($filename)) {
468
+ if (!is_writeable($filename)) {
469
+ printf('File %s don\'t writeable', $filename);
470
+ return false;
471
+ }
472
+ } else {
473
+ if (!is_writable(dirname($filename))) {
474
+ printf('Folder %s don\'t writeable', dirname($filename));
475
+ return false;
476
+ }
477
+ }
478
+ if ($srcIsFile) {
479
+ $result = @copy($src, $filename);
480
+ } else {
481
+ $result = @file_put_contents($filename, $src);
482
+ }
483
+ if (!is_null($mode)) {
484
+ @chmod($filename, $mode);
485
+ }
486
+ chdir($this->_iwd);
487
+ return $result;
488
+ }
489
+
490
+ public function fileExists($file, $onlyFile = true)
491
+ {
492
+ @chdir($this->_cwd);
493
+ $result = file_exists($file);
494
+ if ($result && $onlyFile) {
495
+ $result = is_file($file);
496
+ }
497
+ @chdir($this->_iwd);
498
+ return $result;
499
+ }
500
+
501
+ public function isWriteable($path)
502
+ {
503
+ @chdir($this->_cwd);
504
+ $result = is_writeable($path);
505
+ @chdir($this->_iwd);
506
+ return $result;
507
+ }
508
+
509
+ public function getDestinationFolder($filepath)
510
+ {
511
+ preg_match('/^(.*[!\/])/', $filepath, $mathces);
512
+ if (isset($mathces[0])) {
513
+ return $mathces[0];
514
+ }
515
+ return false;
516
+ }
517
+
518
+ /**
519
+ * Create destination folder
520
+ *
521
+ * @param string $path
522
+ * @return Varien_Io_File
523
+ */
524
+ public function createDestinationDir($path)
525
+ {
526
+ if (!$this->_allowCreateFolders) {
527
+ return false;
528
+ }
529
+ return $this->_createDestinationFolder($this->getCleanPath($path));
530
+ }
531
+
532
+ /**
533
+ * Check and create if not exists folder
534
+ *
535
+ * @param string $folder
536
+ * @param int $mode
537
+ * @return bool
538
+ */
539
+ public function checkAndCreateFolder($folder, $mode = 0777)
540
+ {
541
+ if (is_dir($folder)) {
542
+ return true;
543
+ }
544
+ if (!is_dir(dirname($folder))) {
545
+ $this->checkAndCreateFolder(dirname($folder), $mode);
546
+ }
547
+ if (!is_dir($folder) && !$this->mkdir($folder, $mode)) {
548
+ throw new Exception("Unable to create directory '{$folder}'. Access forbidden.");
549
+ }
550
+ return true;
551
+ }
552
+
553
+ private function _createDestinationFolder($destinationFolder)
554
+ {
555
+ return $this->checkAndCreateFolder($destinationFolder);
556
+
557
+ if( !$destinationFolder ) {
558
+ return $this;
559
+ }
560
+ if (!(@is_dir($destinationFolder) || $this->mkdir($destinationFolder, 0777, true))) {
561
+ throw new Exception("Unable to create directory '{$destinationFolder}'.");
562
+ }
563
+
564
+ $destinationFolder = str_replace('/', DIRECTORY_SEPARATOR, $destinationFolder);
565
+ $path = explode(DIRECTORY_SEPARATOR, $destinationFolder);
566
+ $newPath = null;
567
+ $oldPath = null;
568
+ foreach( $path as $key => $directory ) {
569
+ if (trim($directory)=='') {
570
+ continue;
571
+ }
572
+ if (strlen($directory)===2 && $directory{1}===':') {
573
+ $newPath = $directory;
574
+ continue;
575
+ }
576
+ $newPath.= ( $newPath != DIRECTORY_SEPARATOR ) ? DIRECTORY_SEPARATOR . $directory : $directory;
577
+ if( is_dir($newPath) ) {
578
+ $oldPath = $newPath;
579
+ continue;
580
+ } else {
581
+ if( is_writable($oldPath) ) {
582
+ $this->mkdir($newPath, 0777);
583
+ } else {
584
+ throw new Exception("Unable to create directory '{$newPath}'. Access forbidden.");
585
+ }
586
+ }
587
+ $oldPath = $newPath;
588
+ }
589
+ return $this;
590
+ }
591
+ /**
592
+ * Delete a file
593
+ *
594
+ * @param string $filename
595
+ * @return boolean
596
+ */
597
+ public function rm($filename)
598
+ {
599
+ @chdir($this->_cwd);
600
+ $result = @unlink($filename);
601
+ @chdir($this->_iwd);
602
+ return $result;
603
+ }
604
+
605
+ /**
606
+ * Rename or move a directory or a file
607
+ *
608
+ * @param string $src
609
+ * @param string $dest
610
+ * @return boolean
611
+ */
612
+ public function mv($src, $dest)
613
+ {
614
+ chdir($this->_cwd);
615
+ $result = @rename($src, $dest);
616
+ chdir($this->_iwd);
617
+ return $result;
618
+ }
619
+
620
+ /**
621
+ * Copy a file
622
+ *
623
+ * @param string $src
624
+ * @param string $dest
625
+ * @return boolean
626
+ */
627
+ public function cp($src, $dest)
628
+ {
629
+ @chdir($this->_cwd);
630
+ $result = @copy($src, $dest);
631
+ @chdir($this->_iwd);
632
+ return $result;
633
+ }
634
+
635
+ /**
636
+ * Change mode of a directory or a file
637
+ *
638
+ * @param string $filename
639
+ * @param int $mode
640
+ * @return boolean
641
+ */
642
+ public function chmod($filename, $mode)
643
+ {
644
+ if ($this->_cwd) {
645
+ chdir($this->_cwd);
646
+ }
647
+ $result = @chmod($filename, $mode);
648
+ if ($this->_iwd) {
649
+ chdir($this->_iwd);
650
+ }
651
+ return $result;
652
+ }
653
+
654
+ /**
655
+ * Get list of cwd subdirectories and files
656
+ *
657
+ * Suggestions (from moshe):
658
+ * - Use filemtime instead of filectime for performance
659
+ * - Change $grep to $flags and use binary flags
660
+ * - LS_DIRS = 1
661
+ * - LS_FILES = 2
662
+ * - LS_ALL = 3
663
+ *
664
+ * @param Varien_Io_File const
665
+ * @access public
666
+ * @return array
667
+ */
668
+ public function ls($grep=null)
669
+ {
670
+ $ignoredDirectories = Array('.', '..');
671
+
672
+ if( is_dir($this->_cwd) ) {
673
+ $dir = $this->_cwd;
674
+ } elseif( is_dir($this->_iwd) ) {
675
+ $dir = $this->_iwd;
676
+ } else {
677
+ throw new Exception('Unable to list current working directory.');
678
+ }
679
+
680
+ $list = Array();
681
+
682
+ if ($dh = opendir($dir)) {
683
+ while (($entry = readdir($dh)) !== false) {
684
+ $list_item = Array();
685
+
686
+ $fullpath = $dir . DIRECTORY_SEPARATOR . $entry;
687
+
688
+ if( ($grep == self::GREP_DIRS) && (!is_dir($fullpath)) ) {
689
+ continue;
690
+ } elseif( ($grep == self::GREP_FILES) && (!is_file($fullpath)) ) {
691
+ continue;
692
+ } elseif( in_array($entry, $ignoredDirectories) ) {
693
+ continue;
694
+ }
695
+
696
+ $list_item['text'] = $entry;
697
+ $list_item['mod_date'] = date ('Y-m-d H:i:s', filectime($fullpath));
698
+ $list_item['permissions'] = $this->_parsePermissions(fileperms($fullpath));
699
+ $list_item['owner'] = $this->_getFileOwner($fullpath);
700
+
701
+ if( is_file($fullpath) ) {
702
+ $pathinfo = pathinfo($fullpath);
703
+ $list_item['size'] = filesize($fullpath);
704
+ $list_item['leaf'] = true;
705
+ if( isset($pathinfo['extension']) && in_array(strtolower($pathinfo['extension']), Array('jpg', 'jpeg', 'gif', 'bmp', 'png')) && $list_item['size'] > 0 ) {
706
+ $list_item['is_image'] = true;
707
+ $list_item['filetype'] = $pathinfo['extension'];
708
+ } elseif( $list_item['size'] == 0 ) {
709
+ $list_item['is_image'] = false;
710
+ $list_item['filetype'] = 'unknown';
711
+ } elseif( isset($pathinfo['extension']) ) {
712
+ $list_item['is_image'] = false;
713
+ $list_item['filetype'] = $pathinfo['extension'];
714
+ } else {
715
+ $list_item['is_image'] = false;
716
+ $list_item['filetype'] = 'unknown';
717
+ }
718
+ } else {
719
+ $list_item['leaf'] = false;
720
+ $list_item['id'] = $fullpath;
721
+ }
722
+
723
+ $list[] = $list_item;
724
+ }
725
+ closedir($dh);
726
+ } else {
727
+ throw new Exception('Unable to list current working directory. Access forbidden.');
728
+ }
729
+
730
+ return $list;
731
+ }
732
+
733
+ /**
734
+ * Convert integer permissions format into human readable
735
+ *
736
+ * @param integer $mode
737
+ * @access protected
738
+ * @return string
739
+ */
740
+ protected function _parsePermissions($mode)
741
+ {
742
+ if( $mode & 0x1000 )
743
+ $type='p'; /* FIFO pipe */
744
+ else if( $mode & 0x2000 )
745
+ $type='c'; /* Character special */
746
+ else if( $mode & 0x4000 )
747
+ $type='d'; /* Directory */
748
+ else if( $mode & 0x6000 )
749
+ $type='b'; /* Block special */
750
+ else if( $mode & 0x8000 )
751
+ $type='-'; /* Regular */
752
+ else if( $mode & 0xA000 )
753
+ $type='l'; /* Symbolic Link */
754
+ else if( $mode & 0xC000 )
755
+ $type='s'; /* Socket */
756
+ else
757
+ $type='u'; /* UNKNOWN */
758
+
759
+ /* Determine permissions */
760
+ $owner['read'] = ($mode & 00400) ? 'r' : '-';
761
+ $owner['write'] = ($mode & 00200) ? 'w' : '-';
762
+ $owner['execute'] = ($mode & 00100) ? 'x' : '-';
763
+ $group['read'] = ($mode & 00040) ? 'r' : '-';
764
+ $group['write'] = ($mode & 00020) ? 'w' : '-';
765
+ $group['execute'] = ($mode & 00010) ? 'x' : '-';
766
+ $world['read'] = ($mode & 00004) ? 'r' : '-';
767
+ $world['write'] = ($mode & 00002) ? 'w' : '-';
768
+ $world['execute'] = ($mode & 00001) ? 'x' : '-';
769
+
770
+ /* Adjust for SUID, SGID and sticky bit */
771
+ if( $mode & 0x800 )
772
+ $owner["execute"] = ($owner['execute']=='x') ? 's' : 'S';
773
+ if( $mode & 0x400 )
774
+ $group["execute"] = ($group['execute']=='x') ? 's' : 'S';
775
+ if( $mode & 0x200 )
776
+ $world["execute"] = ($world['execute']=='x') ? 't' : 'T';
777
+
778
+ $s=sprintf('%1s', $type);
779
+ $s.=sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
780
+ $s.=sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
781
+ $s.=sprintf('%1s%1s%1s', $world['read'], $world['write'], $world['execute']);
782
+ return trim($s);
783
+ }
784
+
785
+ /**
786
+ * Get file owner
787
+ *
788
+ * @param string $filename
789
+ * @access protected
790
+ * @return string
791
+ */
792
+ protected function _getFileOwner($filename)
793
+ {
794
+ if( !function_exists('posix_getpwuid') ) {
795
+ return 'n/a';
796
+ }
797
+
798
+ $owner = posix_getpwuid(fileowner($filename));
799
+ $groupinfo = posix_getgrnam(filegroup($filename));
800
+
801
+ return $owner['name'] . ' / ' . $groupinfo;
802
+ }
803
+
804
+ public function dirsep()
805
+ {
806
+ return DIRECTORY_SEPARATOR;
807
+ }
808
+
809
+ public function dirname($file)
810
+ {
811
+ return $this->getCleanPath(dirname($file));
812
+ }
813
+ }
lib/Varien/Io/Ftp.php ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Io
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * FTP client
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Io
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Io_Ftp extends Varien_Io_Abstract
36
+ {
37
+ const ERROR_EMPTY_HOST = 1;
38
+ const ERROR_INVALID_CONNECTION = 2;
39
+ const ERROR_INVALID_LOGIN = 3;
40
+ const ERROR_INVALID_PATH = 4;
41
+ const ERROR_INVALID_MODE = 5;
42
+ const ERROR_INVALID_DESTINATION = 6;
43
+ const ERROR_INVALID_SOURCE = 7;
44
+
45
+ /**
46
+ * Connection config
47
+ *
48
+ * @var array
49
+ */
50
+ protected $_config;
51
+
52
+ /**
53
+ * An FTP connection
54
+ *
55
+ * @var resource
56
+ */
57
+ protected $_conn;
58
+
59
+ /**
60
+ * Error code
61
+ *
62
+ * @var int
63
+ */
64
+ protected $_error;
65
+
66
+ protected $_tmpFilename;
67
+
68
+ /**
69
+ * Open a connection
70
+ *
71
+ * Possible argument keys:
72
+ * - host required
73
+ * - port default 21
74
+ * - timeout default 90
75
+ * - user default anonymous
76
+ * - password default empty
77
+ * - ssl default false
78
+ * - passive default false
79
+ * - path default empty
80
+ * - file_mode default FTP_BINARY
81
+ *
82
+ * @param array $args
83
+ * @return boolean
84
+ */
85
+ public function open(array $args=array())
86
+ {
87
+ if (empty($args['host'])) {
88
+ $this->_error = self::ERROR_EMPTY_HOST;
89
+ throw new Varien_Io_Exception('Empty host specified');
90
+ }
91
+
92
+ if (empty($args['port'])) {
93
+ $args['port'] = 21;
94
+ }
95
+
96
+ if (empty($args['user'])) {
97
+ $args['user'] = 'anonymous';
98
+ $args['password'] = 'anonymous@noserver.com';
99
+ }
100
+
101
+ if (empty($args['password'])) {
102
+ $args['password'] = '';
103
+ }
104
+
105
+ if (empty($args['timeout'])) {
106
+ $args['timeout'] = 90;
107
+ }
108
+
109
+ if (empty($args['file_mode'])) {
110
+ $args['file_mode'] = FTP_BINARY;
111
+ }
112
+
113
+ $this->_config = $args;
114
+
115
+ if (empty($this->_config['ssl'])) {
116
+ $this->_conn = @ftp_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
117
+ } else {
118
+ $this->_conn = @ftp_ssl_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
119
+ }
120
+ if (!$this->_conn) {
121
+ $this->_error = self::ERROR_INVALID_CONNECTION;
122
+ throw new Varien_Io_Exception('Could not establish FTP connection, invalid host or port');
123
+ }
124
+
125
+ if (!@ftp_login($this->_conn, $this->_config['user'], $this->_config['password'])) {
126
+ $this->_error = self::ERROR_INVALID_LOGIN;
127
+ $this->close();
128
+ throw new Varien_Io_Exception('Invalid user name or password');
129
+ }
130
+
131
+ if (!empty($this->_config['path'])) {
132
+ if (!@ftp_chdir($this->_conn, $this->_config['path'])) {
133
+ $this->_error = self::ERROR_INVALID_PATH;
134
+ $this->close();
135
+ throw new Varien_Io_Exception('Invalid path');
136
+ }
137
+ }
138
+
139
+ if (!empty($this->_config['passive'])) {
140
+ if (!@ftp_pasv($this->_conn, true)) {
141
+ $this->_error = self::ERROR_INVALID_MODE;
142
+ $this->close();
143
+ throw new Varien_Io_Exception('Invalid file transfer mode');
144
+ }
145
+ }
146
+
147
+ return true;
148
+ }
149
+
150
+ /**
151
+ * Close a connection
152
+ *
153
+ * @return boolean
154
+ */
155
+ public function close()
156
+ {
157
+ return @ftp_close($this->_conn);
158
+ }
159
+
160
+ /**
161
+ * Create a directory
162
+ *
163
+ * @todo implement $mode and $recursive
164
+ * @param string $dir
165
+ * @param int $mode
166
+ * @param boolean $recursive
167
+ * @return boolean
168
+ */
169
+ public function mkdir($dir, $mode=0777, $recursive=true)
170
+ {
171
+ return @ftp_mkdir($this->_conn, $dir);
172
+ }
173
+
174
+ /**
175
+ * Delete a directory
176
+ *
177
+ * @param string $dir
178
+ * @return boolean
179
+ */
180
+ public function rmdir($dir, $recursive=false)
181
+ {
182
+ return @ftp_rmdir($this->_conn, $dir);
183
+ }
184
+
185
+ /**
186
+ * Get current working directory
187
+ *
188
+ * @return string
189
+ */
190
+ public function pwd()
191
+ {
192
+ return @ftp_pwd($this->_conn);
193
+ }
194
+
195
+ /**
196
+ * Change current working directory
197
+ *
198
+ * @param string $dir
199
+ * @return boolean
200
+ */
201
+ public function cd($dir)
202
+ {
203
+ return @ftp_chdir($this->_conn, $dir);
204
+ }
205
+
206
+ /**
207
+ * Read a file to result, file or stream
208
+ *
209
+ * @param string $filename
210
+ * @param string|resource|null $dest destination file name, stream, or if null will return file contents
211
+ * @return string
212
+ */
213
+ public function read($filename, $dest=null)
214
+ {
215
+ if (is_string($dest)) {
216
+ $result = ftp_get($this->_conn, $dest, $filename, $this->_config['file_mode']);
217
+ } else {
218
+ if (is_resource($dest)) {
219
+ $stream = $dest;
220
+ } elseif (is_null($dest)) {
221
+ $stream = tmpfile();
222
+ } else {
223
+ $this->_error = self::ERROR_INVALID_DESTINATION;
224
+ return false;
225
+ }
226
+
227
+ $result = ftp_fget($this->_conn, $stream, $filename, $this->_config['file_mode']);
228
+
229
+ if (is_null($dest)) {
230
+ fseek($stream, 0);
231
+ $result = '';
232
+ for ($result = ''; $s = fread($stream, 4096); $result .= $s);
233
+ fclose($stream);
234
+ }
235
+ }
236
+ return $result;
237
+ }
238
+
239
+ /**
240
+ * Write a file from string, file or stream
241
+ *
242
+ * @param string $filename
243
+ * @param string|resource $src filename, string data or source stream
244
+ * @return int|boolean
245
+ */
246
+ public function write($filename, $src, $mode=null)
247
+ {
248
+ if (is_string($src) && is_readable($src)) {
249
+ return @ftp_put($this->_conn, $filename, $src, $this->_config['file_mode']);
250
+ } else {
251
+ if (is_string($src)) {
252
+ $stream = tmpfile();
253
+ fputs($stream, $src);
254
+ fseek($stream, 0);
255
+ } elseif (is_resource($src)) {
256
+ $stream = $src;
257
+ } else {
258
+ $this->_error = self::ERROR_INVALID_SOURCE;
259
+ return false;
260
+ }
261
+
262
+ $result = ftp_fput($this->_conn, $filename, $stream, $this->_config['file_mode']);
263
+ if (is_string($src)) {
264
+ fclose($stream);
265
+ }
266
+ return $result;
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Delete a file
272
+ *
273
+ * @param string $filename
274
+ * @return boolean
275
+ */
276
+ public function rm($filename)
277
+ {
278
+ return @ftp_delete($this->_conn, $filename);
279
+ }
280
+
281
+ /**
282
+ * Rename or move a directory or a file
283
+ *
284
+ * @param string $src
285
+ * @param string $dest
286
+ * @return boolean
287
+ */
288
+ public function mv($src, $dest)
289
+ {
290
+ return @ftp_rename($this->_conn, $src, $dest);
291
+ }
292
+
293
+ /**
294
+ * Change mode of a directory or a file
295
+ *
296
+ * @param string $filename
297
+ * @param int $mode
298
+ * @return boolean
299
+ */
300
+ public function chmod($filename, $mode)
301
+ {
302
+ return @ftp_chmod($this->_conn, $mode, $filename);
303
+ }
304
+
305
+ public function ls($grep=null)
306
+ {
307
+ $ls = @ftp_nlist($this->_conn, '.');
308
+
309
+ $list = array();
310
+ foreach ($ls as $file) {
311
+ $list[] = array(
312
+ 'text'=>$file,
313
+ 'id'=>$this->pwd().'/'.$file,
314
+ );
315
+ }
316
+
317
+ return $list;
318
+ }
319
+
320
+ protected function _tmpFilename($new=false)
321
+ {
322
+ if ($new || !$this->_tmpFilename) {
323
+ $this->_tmpFilename = tempnam( md5(uniqid(rand(), TRUE)), '' );
324
+ }
325
+ return $this->_tmpFilename;
326
+ }
327
+ }
lib/Varien/Io/Interface.php ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Io
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Input/output client interface
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Io
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ interface Varien_Io_Interface
36
+ {
37
+ /**
38
+ * Open a connection
39
+ *
40
+ */
41
+ public function open(array $args=array());
42
+
43
+ /**
44
+ * Close a connection
45
+ *
46
+ */
47
+ public function close();
48
+
49
+ /**
50
+ * Create a directory
51
+ *
52
+ */
53
+ public function mkdir($dir, $mode=0777, $recursive=true);
54
+
55
+ /**
56
+ * Delete a directory
57
+ *
58
+ */
59
+ public function rmdir($dir, $recursive=false);
60
+
61
+ /**
62
+ * Get current working directory
63
+ *
64
+ */
65
+ public function pwd();
66
+
67
+ /**
68
+ * Change current working directory
69
+ *
70
+ */
71
+ public function cd($dir);
72
+
73
+ /**
74
+ * Read a file
75
+ *
76
+ */
77
+ public function read($filename, $dest=null);
78
+
79
+ /**
80
+ * Write a file
81
+ *
82
+ */
83
+ public function write($filename, $src, $mode=null);
84
+
85
+ /**
86
+ * Delete a file
87
+ *
88
+ */
89
+ public function rm($filename);
90
+
91
+ /**
92
+ * Rename or move a directory or a file
93
+ *
94
+ */
95
+ public function mv($src, $dest);
96
+
97
+ /**
98
+ * Chamge mode of a directory or a file
99
+ *
100
+ */
101
+ public function chmod($filename, $mode);
102
+
103
+ /**
104
+ * Get list of cwd subdirectories and files
105
+ *
106
+ */
107
+ public function ls($grep=null);
108
+
109
+ /**
110
+ * Retrieve directory separator in context of io resource
111
+ *
112
+ */
113
+ public function dirsep();
114
+ }
lib/Varien/Io/Sftp.php ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Io
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ require_once('phpseclib/Net/SFTP.php');
28
+
29
+ /**
30
+ * Sftp client interface
31
+ *
32
+ * @category Varien
33
+ * @package Varien_Io
34
+ * @author Magento Core Team <core@magentocommerce.com>
35
+ * @link http://www.php.net/manual/en/function.ssh2-connect.php
36
+ */
37
+ class Varien_Io_Sftp extends Varien_Io_Abstract implements Varien_Io_Interface
38
+ {
39
+ const REMOTE_TIMEOUT = 10;
40
+ const SSH2_PORT = 22;
41
+
42
+ /**
43
+ * @var Net_SFTP $_connection
44
+ */
45
+ protected $_connection = null;
46
+
47
+
48
+ /**
49
+ * Open a SFTP connection to a remote site.
50
+ *
51
+ * @param array $args Connection arguments
52
+ * @param string $args[host] Remote hostname
53
+ * @param string $args[username] Remote username
54
+ * @param string $args[password] Connection password
55
+ * @param int $args[timeout] Connection timeout [=10]
56
+ *
57
+ */
58
+ public function open(array $args = array())
59
+ {
60
+ if (!isset($args['timeout'])) {
61
+ $args['timeout'] = self::REMOTE_TIMEOUT;
62
+ }
63
+ if (strpos($args['host'], ':') !== false) {
64
+ list($host, $port) = explode(':', $args['host'], 2);
65
+ } else {
66
+ $host = $args['host'];
67
+ $port = self::SSH2_PORT;
68
+ }
69
+ $this->_connection = new Net_SFTP($host, $port, $args['timeout']);
70
+ if (!$this->_connection->login($args['username'], $args['password'])) {
71
+ throw new Exception(sprintf(__("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])));
72
+ }
73
+
74
+ }
75
+
76
+ /**
77
+ * Close a connection
78
+ *
79
+ */
80
+ public function close()
81
+ {
82
+ return $this->_connection->disconnect();
83
+ }
84
+
85
+ /**
86
+ * Create a directory
87
+ *
88
+ * @param $mode Ignored here; uses logged-in user's umask
89
+ * @param $recursive Analogous to mkdir -p
90
+ *
91
+ * Note: if $recursive is true and an error occurs mid-execution,
92
+ * false is returned and some part of the hierarchy might be created.
93
+ * No rollback is performed.
94
+ */
95
+ public function mkdir($dir, $mode=0777, $recursive=true)
96
+ {
97
+ if ($recursive) {
98
+ $no_errors = true;
99
+ $dirlist = explode('/', $dir);
100
+ reset($dirlist);
101
+ $cwd = $this->_connection->pwd();
102
+ while ($no_errors && ($dir_item = next($dirlist))) {
103
+ $no_errors = ($this->_connection->mkdir($dir_item) && $this->_connection->chdir($dir_item));
104
+ }
105
+ $this->_connection->chdir($cwd);
106
+ return $no_errors;
107
+ } else {
108
+ return $this->_connection->mkdir($dir);
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Delete a directory
114
+ *
115
+ */
116
+ public function rmdir($dir, $recursive=false)
117
+ {
118
+ if ($recursive) {
119
+ $no_errors = true;
120
+ $cwd = $this->_connection->pwd();
121
+ if(!$this->_connection->chdir($dir)) {
122
+ throw new Exception("chdir(): $dir: Not a directory");
123
+ }
124
+ $list = $this->_connection->nlist();
125
+ if (!count($list)) {
126
+ // Go back
127
+ $this->_connection->chdir($pwd);
128
+ return $this->rmdir($dir, false);
129
+ } else {
130
+ foreach ($list as $filename) {
131
+ if($this->_connection->chdir($filename)) { // This is a directory
132
+ $this->_connection->chdir('..');
133
+ $no_errors = $no_errors && $this->rmdir($filename, $recursive);
134
+ } else {
135
+ $no_errors = $no_errors && $this->rm($filename);
136
+ }
137
+ }
138
+ }
139
+ $no_errors = $no_errors && ($this->_connection->chdir($pwd) && $this->_connection->rmdir($dir));
140
+ return $no_errors;
141
+ } else {
142
+ return $this->_connection->rmdir($dir);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Get current working directory
148
+ *
149
+ */
150
+ public function pwd()
151
+ {
152
+ return $this->_connection->pwd();
153
+ }
154
+
155
+ /**
156
+ * Change current working directory
157
+ *
158
+ */
159
+ public function cd($dir)
160
+ {
161
+ return $this->_connection->chdir($dir);
162
+ }
163
+
164
+ /**
165
+ * Read a file
166
+ *
167
+ */
168
+ public function read($filename, $dest=null)
169
+ {
170
+ if (is_null($dest)) {
171
+ $dest = false;
172
+ }
173
+ return $this->_connection->get($filename, $dest);
174
+ }
175
+
176
+ /**
177
+ * Write a file
178
+ * @param $src Must be a local file name
179
+ */
180
+ public function write($filename, $src, $mode=null)
181
+ {
182
+ return $this->_connection->put($filename, $src);
183
+ }
184
+
185
+ /**
186
+ * Delete a file
187
+ *
188
+ */
189
+ public function rm($filename)
190
+ {
191
+ return $this->_connection->delete($filename);
192
+ }
193
+
194
+ /**
195
+ * Rename or move a directory or a file
196
+ *
197
+ */
198
+ public function mv($src, $dest)
199
+ {
200
+ return $this->_connection->rename($src, $dest);
201
+ }
202
+
203
+ /**
204
+ * Chamge mode of a directory or a file
205
+ *
206
+ */
207
+ public function chmod($filename, $mode)
208
+ {
209
+ return $this->_connection->chmod($mode, $filename);
210
+ }
211
+
212
+ /**
213
+ * Get list of cwd subdirectories and files
214
+ *
215
+ */
216
+ public function ls($grep=null)
217
+ {
218
+ $list = $this->_connection->nlist();
219
+ $pwd = $this->pwd();
220
+ $result = array();
221
+ foreach($list as $name) {
222
+ $result[] = array(
223
+ 'text' => $name,
224
+ 'id' => "{$pwd}{$name}",
225
+ );
226
+ }
227
+ return $result;
228
+ }
229
+
230
+ public function rawls()
231
+ {
232
+ $list = $this->_connection->rawlist();
233
+ return $list;
234
+ }
235
+
236
+ }
lib/Varien/Object.php ADDED
@@ -0,0 +1,918 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Object
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Varien Object
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Object
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Object implements ArrayAccess
36
+ {
37
+
38
+ /**
39
+ * Object attributes
40
+ *
41
+ * @var array
42
+ */
43
+ protected $_data = array();
44
+
45
+ /**
46
+ * Data changes flag (true after setData|unsetData call)
47
+ * @var $_hasDataChange bool
48
+ */
49
+ protected $_hasDataChanges = false;
50
+
51
+ /**
52
+ * Original data that was loaded
53
+ *
54
+ * @var array
55
+ */
56
+ protected $_origData;
57
+
58
+ /**
59
+ * Name of object id field
60
+ *
61
+ * @var string
62
+ */
63
+ protected $_idFieldName = null;
64
+
65
+ /**
66
+ * Setter/Getter underscore transformation cache
67
+ *
68
+ * @var array
69
+ */
70
+ protected static $_underscoreCache = array();
71
+
72
+ /**
73
+ * Object delete flag
74
+ *
75
+ * @var boolean
76
+ */
77
+ protected $_isDeleted = false;
78
+
79
+ /**
80
+ * Map short fields names to its full names
81
+ *
82
+ * @var array
83
+ */
84
+ protected $_oldFieldsMap = array();
85
+
86
+ /**
87
+ * Map of fields to sync to other fields upon changing their data
88
+ */
89
+ protected $_syncFieldsMap = array();
90
+
91
+ /**
92
+ * Constructor
93
+ *
94
+ * By default is looking for first argument as array and assignes it as object attributes
95
+ * This behaviour may change in child classes
96
+ *
97
+ */
98
+ public function __construct()
99
+ {
100
+ $this->_initOldFieldsMap();
101
+ if ($this->_oldFieldsMap) {
102
+ $this->_prepareSyncFieldsMap();
103
+ }
104
+
105
+ $args = func_get_args();
106
+ if (empty($args[0])) {
107
+ $args[0] = array();
108
+ }
109
+ $this->_data = $args[0];
110
+ $this->_addFullNames();
111
+
112
+ $this->_construct();
113
+ }
114
+
115
+ protected function _addFullNames()
116
+ {
117
+ $existedShortKeys = array_intersect($this->_syncFieldsMap, array_keys($this->_data));
118
+ if (!empty($existedShortKeys)) {
119
+ foreach ($existedShortKeys as $key) {
120
+ $fullFieldName = array_search($key, $this->_syncFieldsMap);
121
+ $this->_data[$fullFieldName] = $this->_data[$key];
122
+ }
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Inits mapping array of object's previously used fields to new fields.
128
+ * Must be overloaded by descendants to set concrete fields map.
129
+ *
130
+ * @return Varien_Object
131
+ */
132
+ protected function _initOldFieldsMap()
133
+ {
134
+
135
+ }
136
+
137
+ /**
138
+ * Called after old fields are inited. Forms synchronization map to sync old fields and new fields
139
+ * between each other.
140
+ *
141
+ * @return Varien_Object
142
+ */
143
+ protected function _prepareSyncFieldsMap()
144
+ {
145
+ $old2New = $this->_oldFieldsMap;
146
+ $new2Old = array_flip($this->_oldFieldsMap);
147
+ $this->_syncFieldsMap = array_merge($old2New, $new2Old);
148
+ return $this;
149
+ }
150
+
151
+ /**
152
+ * Internal constructor not depended on params. Can be used for object initialization
153
+ */
154
+ protected function _construct()
155
+ {
156
+ }
157
+
158
+ /**
159
+ * Set _isDeleted flag value (if $isDeleted param is defined) and return current flag value
160
+ *
161
+ * @param boolean $isDeleted
162
+ * @return boolean
163
+ */
164
+ public function isDeleted($isDeleted=null)
165
+ {
166
+ $result = $this->_isDeleted;
167
+ if (!is_null($isDeleted)) {
168
+ $this->_isDeleted = $isDeleted;
169
+ }
170
+ return $result;
171
+ }
172
+
173
+ /**
174
+ * Get data change status
175
+ *
176
+ * @return bool
177
+ */
178
+ public function hasDataChanges()
179
+ {
180
+ return $this->_hasDataChanges;
181
+ }
182
+
183
+ /**
184
+ * set name of object id field
185
+ *
186
+ * @param string $name
187
+ * @return Varien_Object
188
+ */
189
+ public function setIdFieldName($name)
190
+ {
191
+ $this->_idFieldName = $name;
192
+ return $this;
193
+ }
194
+
195
+ /**
196
+ * Retrieve name of object id field
197
+ *
198
+ * @param string $name
199
+ * @return Varien_Object
200
+ */
201
+ public function getIdFieldName()
202
+ {
203
+ return $this->_idFieldName;
204
+ }
205
+
206
+ /**
207
+ * Retrieve object id
208
+ *
209
+ * @return mixed
210
+ */
211
+ public function getId()
212
+ {
213
+ if ($this->getIdFieldName()) {
214
+ return $this->_getData($this->getIdFieldName());
215
+ }
216
+ return $this->_getData('id');
217
+ }
218
+
219
+ /**
220
+ * Set object id field value
221
+ *
222
+ * @param mixed $value
223
+ * @return Varien_Object
224
+ */
225
+ public function setId($value)
226
+ {
227
+ if ($this->getIdFieldName()) {
228
+ $this->setData($this->getIdFieldName(), $value);
229
+ } else {
230
+ $this->setData('id', $value);
231
+ }
232
+ return $this;
233
+ }
234
+
235
+ /**
236
+ * Add data to the object.
237
+ *
238
+ * Retains previous data in the object.
239
+ *
240
+ * @param array $arr
241
+ * @return Varien_Object
242
+ */
243
+ public function addData(array $arr)
244
+ {
245
+ foreach($arr as $index=>$value) {
246
+ $this->setData($index, $value);
247
+ }
248
+ return $this;
249
+ }
250
+
251
+ /**
252
+ * Overwrite data in the object.
253
+ *
254
+ * $key can be string or array.
255
+ * If $key is string, the attribute value will be overwritten by $value
256
+ *
257
+ * If $key is an array, it will overwrite all the data in the object.
258
+ *
259
+ * @param string|array $key
260
+ * @param mixed $value
261
+ * @return Varien_Object
262
+ */
263
+ public function setData($key, $value=null)
264
+ {
265
+ $this->_hasDataChanges = true;
266
+ if(is_array($key)) {
267
+ $this->_data = $key;
268
+ $this->_addFullNames();
269
+ } else {
270
+ $this->_data[$key] = $value;
271
+ if (isset($this->_syncFieldsMap[$key])) {
272
+ $fullFieldName = $this->_syncFieldsMap[$key];
273
+ $this->_data[$fullFieldName] = $value;
274
+ }
275
+ }
276
+ return $this;
277
+ }
278
+
279
+ /**
280
+ * Unset data from the object.
281
+ *
282
+ * $key can be a string only. Array will be ignored.
283
+ *
284
+ * @param string $key
285
+ * @return Varien_Object
286
+ */
287
+ public function unsetData($key=null)
288
+ {
289
+ $this->_hasDataChanges = true;
290
+ if (is_null($key)) {
291
+ $this->_data = array();
292
+ } else {
293
+ unset($this->_data[$key]);
294
+ if (isset($this->_syncFieldsMap[$key])) {
295
+ $fullFieldName = $this->_syncFieldsMap[$key];
296
+ unset($this->_data[$fullFieldName]);
297
+ }
298
+ }
299
+ return $this;
300
+ }
301
+
302
+ /**
303
+ * Unset old fields data from the object.
304
+ *
305
+ * $key can be a string only. Array will be ignored.
306
+ *
307
+ * @param string $key
308
+ * @return Varien_Object
309
+ */
310
+ public function unsetOldData($key=null)
311
+ {
312
+ if (is_null($key)) {
313
+ foreach ($this->_syncFieldsMap as $key => $newFieldName) {
314
+ unset($this->_data[$key]);
315
+ }
316
+ } else {
317
+ unset($this->_data[$key]);
318
+ }
319
+ return $this;
320
+ }
321
+
322
+ /**
323
+ * Retrieves data from the object
324
+ *
325
+ * If $key is empty will return all the data as an array
326
+ * Otherwise it will return value of the attribute specified by $key
327
+ *
328
+ * If $index is specified it will assume that attribute data is an array
329
+ * and retrieve corresponding member.
330
+ *
331
+ * @param string $key
332
+ * @param string|int $index
333
+ * @return mixed
334
+ */
335
+ public function getData($key='', $index=null)
336
+ {
337
+ if (''===$key) {
338
+ return $this->_data;
339
+ }
340
+
341
+ $default = null;
342
+
343
+ // accept a/b/c as ['a']['b']['c']
344
+ if (strpos($key,'/')) {
345
+ $keyArr = explode('/', $key);
346
+ $data = $this->_data;
347
+ foreach ($keyArr as $i=>$k) {
348
+ if ($k==='') {
349
+ return $default;
350
+ }
351
+ if (is_array($data)) {
352
+ if (!isset($data[$k])) {
353
+ return $default;
354
+ }
355
+ $data = $data[$k];
356
+ } elseif ($data instanceof Varien_Object) {
357
+ $data = $data->getData($k);
358
+ } else {
359
+ return $default;
360
+ }
361
+ }
362
+ return $data;
363
+ }
364
+
365
+ // legacy functionality for $index
366
+ if (isset($this->_data[$key])) {
367
+ if (is_null($index)) {
368
+ return $this->_data[$key];
369
+ }
370
+
371
+ $value = $this->_data[$key];
372
+ if (is_array($value)) {
373
+ //if (isset($value[$index]) && (!empty($value[$index]) || strlen($value[$index]) > 0)) {
374
+ /**
375
+ * If we have any data, even if it empty - we should use it, anyway
376
+ */
377
+ if (isset($value[$index])) {
378
+ return $value[$index];
379
+ }
380
+ return null;
381
+ } elseif (is_string($value)) {
382
+ $arr = explode("\n", $value);
383
+ return (isset($arr[$index]) && (!empty($arr[$index]) || strlen($arr[$index]) > 0)) ? $arr[$index] : null;
384
+ } elseif ($value instanceof Varien_Object) {
385
+ return $value->getData($index);
386
+ }
387
+ return $default;
388
+ }
389
+ return $default;
390
+ }
391
+
392
+ /**
393
+ * Get value from _data array without parse key
394
+ *
395
+ * @param string $key
396
+ * @return mixed
397
+ */
398
+ protected function _getData($key)
399
+ {
400
+ return isset($this->_data[$key]) ? $this->_data[$key] : null;
401
+ }
402
+
403
+ /**
404
+ * Set object data with calling setter method
405
+ *
406
+ * @param string $key
407
+ * @param mixed $args
408
+ * @return Varien_Object
409
+ */
410
+ public function setDataUsingMethod($key, $args=array())
411
+ {
412
+ $method = 'set'.$this->_camelize($key);
413
+ $this->$method($args);
414
+ return $this;
415
+ }
416
+
417
+ /**
418
+ * Get object data by key with calling getter method
419
+ *
420
+ * @param string $key
421
+ * @param mixed $args
422
+ * @return mixed
423
+ */
424
+ public function getDataUsingMethod($key, $args=null)
425
+ {
426
+ $method = 'get'.$this->_camelize($key);
427
+ return $this->$method($args);
428
+ }
429
+
430
+ /**
431
+ * Fast get data or set default if value is not available
432
+ *
433
+ * @param string $key
434
+ * @param mixed $default
435
+ * @return mixed
436
+ */
437
+ public function getDataSetDefault($key, $default)
438
+ {
439
+ if (!isset($this->_data[$key])) {
440
+ $this->_data[$key] = $default;
441
+ }
442
+ return $this->_data[$key];
443
+ }
444
+
445
+ /**
446
+ * If $key is empty, checks whether there's any data in the object
447
+ * Otherwise checks if the specified attribute is set.
448
+ *
449
+ * @param string $key
450
+ * @return boolean
451
+ */
452
+ public function hasData($key='')
453
+ {
454
+ if (empty($key) || !is_string($key)) {
455
+ return !empty($this->_data);
456
+ }
457
+ return array_key_exists($key, $this->_data);
458
+ }
459
+
460
+ /**
461
+ * Convert object attributes to array
462
+ *
463
+ * @param array $arrAttributes array of required attributes
464
+ * @return array
465
+ */
466
+ public function __toArray(array $arrAttributes = array())
467
+ {
468
+ if (empty($arrAttributes)) {
469
+ return $this->_data;
470
+ }
471
+
472
+ $arrRes = array();
473
+ foreach ($arrAttributes as $attribute) {
474
+ if (isset($this->_data[$attribute])) {
475
+ $arrRes[$attribute] = $this->_data[$attribute];
476
+ }
477
+ else {
478
+ $arrRes[$attribute] = null;
479
+ }
480
+ }
481
+ return $arrRes;
482
+ }
483
+
484
+ /**
485
+ * Public wrapper for __toArray
486
+ *
487
+ * @param array $arrAttributes
488
+ * @return array
489
+ */
490
+ public function toArray(array $arrAttributes = array())
491
+ {
492
+ return $this->__toArray($arrAttributes);
493
+ }
494
+
495
+ /**
496
+ * Set required array elements
497
+ *
498
+ * @param array $arr
499
+ * @param array $elements
500
+ * @return array
501
+ */
502
+ protected function _prepareArray(&$arr, array $elements=array())
503
+ {
504
+ foreach ($elements as $element) {
505
+ if (!isset($arr[$element])) {
506
+ $arr[$element] = null;
507
+ }
508
+ }
509
+ return $arr;
510
+ }
511
+
512
+ /**
513
+ * Convert object attributes to XML
514
+ *
515
+ * @param array $arrAttributes array of required attributes
516
+ * @param string $rootName name of the root element
517
+ * @return string
518
+ */
519
+ protected function __toXml(array $arrAttributes = array(), $rootName = 'item', $addOpenTag=false, $addCdata=true)
520
+ {
521
+ $xml = '';
522
+ if ($addOpenTag) {
523
+ $xml.= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
524
+ }
525
+ if (!empty($rootName)) {
526
+ $xml.= '<'.$rootName.'>'."\n";
527
+ }
528
+ $xmlModel = new Varien_Simplexml_Element('<node></node>');
529
+ $arrData = $this->toArray($arrAttributes);
530
+ foreach ($arrData as $fieldName => $fieldValue) {
531
+ if ($addCdata === true) {
532
+ $fieldValue = "<![CDATA[$fieldValue]]>";
533
+ } else {
534
+ $fieldValue = $xmlModel->xmlentities($fieldValue);
535
+ }
536
+ $xml.= "<$fieldName>$fieldValue</$fieldName>"."\n";
537
+ }
538
+ if (!empty($rootName)) {
539
+ $xml.= '</'.$rootName.'>'."\n";
540
+ }
541
+ return $xml;
542
+ }
543
+
544
+ /**
545
+ * Public wrapper for __toXml
546
+ *
547
+ * @param array $arrAttributes
548
+ * @param string $rootName
549
+ * @return string
550
+ */
551
+ public function toXml(array $arrAttributes = array(), $rootName = 'item', $addOpenTag=false, $addCdata=true)
552
+ {
553
+ return $this->__toXml($arrAttributes, $rootName, $addOpenTag, $addCdata);
554
+ }
555
+
556
+ /**
557
+ * Convert object attributes to JSON
558
+ *
559
+ * @param array $arrAttributes array of required attributes
560
+ * @return string
561
+ */
562
+ protected function __toJson(array $arrAttributes = array())
563
+ {
564
+ $arrData = $this->toArray($arrAttributes);
565
+ $json = Zend_Json::encode($arrData);
566
+ return $json;
567
+ }
568
+
569
+ /**
570
+ * Public wrapper for __toJson
571
+ *
572
+ * @param array $arrAttributes
573
+ * @return string
574
+ */
575
+ public function toJson(array $arrAttributes = array())
576
+ {
577
+ return $this->__toJson($arrAttributes);
578
+ }
579
+
580
+ /**
581
+ * Convert object attributes to string
582
+ *
583
+ * @param array $arrAttributes array of required attributes
584
+ * @param string $valueSeparator
585
+ * @return string
586
+ */
587
+ // public function __toString(array $arrAttributes = array(), $valueSeparator=',')
588
+ // {
589
+ // $arrData = $this->toArray($arrAttributes);
590
+ // return implode($valueSeparator, $arrData);
591
+ // }
592
+
593
+ /**
594
+ * Public wrapper for __toString
595
+ *
596
+ * Will use $format as an template and substitute {{key}} for attributes
597
+ *
598
+ * @param string $format
599
+ * @return string
600
+ */
601
+ public function toString($format='')
602
+ {
603
+ if (empty($format)) {
604
+ $str = implode(', ', $this->getData());
605
+ } else {
606
+ preg_match_all('/\{\{([a-z0-9_]+)\}\}/is', $format, $matches);
607
+ foreach ($matches[1] as $var) {
608
+ $format = str_replace('{{'.$var.'}}', $this->getData($var), $format);
609
+ }
610
+ $str = $format;
611
+ }
612
+ return $str;
613
+ }
614
+
615
+ /**
616
+ * Set/Get attribute wrapper
617
+ *
618
+ * @param string $method
619
+ * @param array $args
620
+ * @return mixed
621
+ */
622
+ public function __call($method, $args)
623
+ {
624
+ switch (substr($method, 0, 3)) {
625
+ case 'get' :
626
+ //Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
627
+ $key = $this->_underscore(substr($method,3));
628
+ $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
629
+ //Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
630
+ return $data;
631
+
632
+ case 'set' :
633
+ //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
634
+ $key = $this->_underscore(substr($method,3));
635
+ $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
636
+ //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
637
+ return $result;
638
+
639
+ case 'uns' :
640
+ //Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
641
+ $key = $this->_underscore(substr($method,3));
642
+ $result = $this->unsetData($key);
643
+ //Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
644
+ return $result;
645
+
646
+ case 'has' :
647
+ //Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
648
+ $key = $this->_underscore(substr($method,3));
649
+ //Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
650
+ return isset($this->_data[$key]);
651
+ }
652
+ throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
653
+ }
654
+
655
+ /**
656
+ * Attribute getter (deprecated)
657
+ *
658
+ * @param string $var
659
+ * @return mixed
660
+ */
661
+
662
+ public function __get($var)
663
+ {
664
+ $var = $this->_underscore($var);
665
+ return $this->getData($var);
666
+ }
667
+
668
+ /**
669
+ * Attribute setter (deprecated)
670
+ *
671
+ * @param string $var
672
+ * @param mixed $value
673
+ */
674
+ public function __set($var, $value)
675
+ {
676
+ $var = $this->_underscore($var);
677
+ $this->setData($var, $value);
678
+ }
679
+
680
+ /**
681
+ * checks whether the object is empty
682
+ *
683
+ * @return boolean
684
+ */
685
+ public function isEmpty()
686
+ {
687
+ if (empty($this->_data)) {
688
+ return true;
689
+ }
690
+ return false;
691
+ }
692
+
693
+ /**
694
+ * Converts field names for setters and geters
695
+ *
696
+ * $this->setMyField($value) === $this->setData('my_field', $value)
697
+ * Uses cache to eliminate unneccessary preg_replace
698
+ *
699
+ * @param string $name
700
+ * @return string
701
+ */
702
+ protected function _underscore($name)
703
+ {
704
+ if (isset(self::$_underscoreCache[$name])) {
705
+ return self::$_underscoreCache[$name];
706
+ }
707
+ #Varien_Profiler::start('underscore');
708
+ $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name));
709
+ #Varien_Profiler::stop('underscore');
710
+ self::$_underscoreCache[$name] = $result;
711
+ return $result;
712
+ }
713
+
714
+ protected function _camelize($name)
715
+ {
716
+ return uc_words($name, '');
717
+ }
718
+
719
+ /**
720
+ * serialize object attributes
721
+ *
722
+ * @param array $attributes
723
+ * @param string $valueSeparator
724
+ * @param string $fieldSeparator
725
+ * @param string $quote
726
+ * @return string
727
+ */
728
+ public function serialize($attributes = array(), $valueSeparator='=', $fieldSeparator=' ', $quote='"')
729
+ {
730
+ $res = '';
731
+ $data = array();
732
+ if (empty($attributes)) {
733
+ $attributes = array_keys($this->_data);
734
+ }
735
+
736
+ foreach ($this->_data as $key => $value) {
737
+ if (in_array($key, $attributes)) {
738
+ $data[] = $key . $valueSeparator . $quote . $value . $quote;
739
+ }
740
+ }
741
+ $res = implode($fieldSeparator, $data);
742
+ return $res;
743
+ }
744
+
745
+ /**
746
+ * Get object loaded data (original data)
747
+ *
748
+ * @param string $key
749
+ * @return mixed
750
+ */
751
+ public function getOrigData($key=null)
752
+ {
753
+ if (is_null($key)) {
754
+ return $this->_origData;
755
+ }
756
+ return isset($this->_origData[$key]) ? $this->_origData[$key] : null;
757
+ }
758
+
759
+ /**
760
+ * Initialize object original data
761
+ *
762
+ * @param string $key
763
+ * @param mixed $data
764
+ * @return Varien_Object
765
+ */
766
+ public function setOrigData($key=null, $data=null)
767
+ {
768
+ if (is_null($key)) {
769
+ $this->_origData = $this->_data;
770
+ } else {
771
+ $this->_origData[$key] = $data;
772
+ }
773
+ return $this;
774
+ }
775
+
776
+ /**
777
+ * Compare object data with original data
778
+ *
779
+ * @param string $field
780
+ * @return boolean
781
+ */
782
+ public function dataHasChangedFor($field)
783
+ {
784
+ $newData = $this->getData($field);
785
+ $origData = $this->getOrigData($field);
786
+ return $newData!=$origData;
787
+ }
788
+
789
+ /**
790
+ * Clears data changes status
791
+ *
792
+ * @param boolean $value
793
+ * @return Varien_Object
794
+ */
795
+ public function setDataChanges($value)
796
+ {
797
+ $this->_hasDataChanges = (bool)$value;
798
+ return $this;
799
+ }
800
+
801
+ /**
802
+ * Present object data as string in debug mode
803
+ *
804
+ * @param mixed $data
805
+ * @param array $objects
806
+ * @return string
807
+ */
808
+ public function debug($data=null, &$objects=array())
809
+ {
810
+ if (is_null($data)) {
811
+ $hash = spl_object_hash($this);
812
+ if (!empty($objects[$hash])) {
813
+ return '*** RECURSION ***';
814
+ }
815
+ $objects[$hash] = true;
816
+ $data = $this->getData();
817
+ }
818
+ $debug = array();
819
+ foreach ($data as $key=>$value) {
820
+ if (is_scalar($value)) {
821
+ $debug[$key] = $value;
822
+ } elseif (is_array($value)) {
823
+ $debug[$key] = $this->debug($value, $objects);
824
+ } elseif ($value instanceof Varien_Object) {
825
+ $debug[$key.' ('.get_class($value).')'] = $value->debug(null, $objects);
826
+ }
827
+ }
828
+ return $debug;
829
+ }
830
+
831
+ /**
832
+ * Implementation of ArrayAccess::offsetSet()
833
+ *
834
+ * @link http://www.php.net/manual/en/arrayaccess.offsetset.php
835
+ * @param string $offset
836
+ * @param mixed $value
837
+ */
838
+ public function offsetSet($offset, $value)
839
+ {
840
+ $this->_data[$offset] = $value;
841
+ }
842
+
843
+ /**
844
+ * Implementation of ArrayAccess::offsetExists()
845
+ *
846
+ * @link http://www.php.net/manual/en/arrayaccess.offsetexists.php
847
+ * @param string $offset
848
+ * @return boolean
849
+ */
850
+ public function offsetExists($offset)
851
+ {
852
+ return isset($this->_data[$offset]);
853
+ }
854
+
855
+ /**
856
+ * Implementation of ArrayAccess::offsetUnset()
857
+ *
858
+ * @link http://www.php.net/manual/en/arrayaccess.offsetunset.php
859
+ * @param string $offset
860
+ */
861
+ public function offsetUnset($offset)
862
+ {
863
+ unset($this->_data[$offset]);
864
+ }
865
+
866
+ /**
867
+ * Implementation of ArrayAccess::offsetGet()
868
+ *
869
+ * @link http://www.php.net/manual/en/arrayaccess.offsetget.php
870
+ * @param string $offset
871
+ * @return mixed
872
+ */
873
+ public function offsetGet($offset)
874
+ {
875
+ return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
876
+ }
877
+
878
+
879
+ /**
880
+ * Enter description here...
881
+ *
882
+ * @param string $field
883
+ * @return boolean
884
+ */
885
+ public function isDirty($field=null)
886
+ {
887
+ if (empty($this->_dirty)) {
888
+ return false;
889
+ }
890
+ if (is_null($field)) {
891
+ return true;
892
+ }
893
+ return isset($this->_dirty[$field]);
894
+ }
895
+
896
+ /**
897
+ * Enter description here...
898
+ *
899
+ * @param string $field
900
+ * @param boolean $flag
901
+ * @return Varien_Object
902
+ */
903
+ public function flagDirty($field, $flag=true)
904
+ {
905
+ if (is_null($field)) {
906
+ foreach ($this->getData() as $field=>$value) {
907
+ $this->flagDirty($field, $flag);
908
+ }
909
+ } else {
910
+ if ($flag) {
911
+ $this->_dirty[$field] = true;
912
+ } else {
913
+ unset($this->_dirty[$field]);
914
+ }
915
+ }
916
+ return $this;
917
+ }
918
+ }
lib/Varien/Object/Cache.php ADDED
@@ -0,0 +1,457 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Object
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Object Cache
30
+ *
31
+ * Stores objects for reuse, cleanup and to avoid circular references
32
+ *
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Object_Cache
36
+ {
37
+ /**
38
+ * Singleton instance
39
+ *
40
+ * @var Varien_Object_Cache
41
+ */
42
+ protected static $_instance;
43
+
44
+ /**
45
+ * Running object index for anonymous objects
46
+ *
47
+ * @var integer
48
+ */
49
+ protected $_idx = 0;
50
+
51
+ /**
52
+ * Array of objects
53
+ *
54
+ * @var array of objects
55
+ */
56
+ protected $_objects = array();
57
+
58
+ /**
59
+ * SPL object hashes
60
+ *
61
+ * @var array
62
+ */
63
+ protected $_hashes = array();
64
+
65
+ /**
66
+ * SPL hashes by object
67
+ *
68
+ * @var array
69
+ */
70
+ protected $_objectHashes = array();
71
+
72
+ /**
73
+ * Objects by tags for cleanup
74
+ *
75
+ * @var array 2D
76
+ */
77
+ protected $_tags = array();
78
+
79
+ /**
80
+ * Tags by objects
81
+ *
82
+ * @var array 2D
83
+ */
84
+ protected $_objectTags = array();
85
+
86
+ /**
87
+ * References to objects
88
+ *
89
+ * @var array
90
+ */
91
+ protected $_references = array();
92
+
93
+ /**
94
+ * References by object
95
+ *
96
+ * @var array 2D
97
+ */
98
+ protected $_objectReferences = array();
99
+
100
+ /**
101
+ * Debug data such as backtrace per class
102
+ *
103
+ * @var array
104
+ */
105
+ protected $_debug = array();
106
+
107
+ /**
108
+ * Singleton factory
109
+ *
110
+ * @return Varien_Object_Cache
111
+ */
112
+ public static function singleton()
113
+ {
114
+ if (!self::$_instance) {
115
+ self::$_instance = new self();
116
+ }
117
+ return self::$_instance;
118
+ }
119
+
120
+ /**
121
+ * Load an object from registry
122
+ *
123
+ * @param string|object $idx
124
+ * @param object $default
125
+ * @return object
126
+ */
127
+ public function load($idx, $default=null)
128
+ {
129
+ if (isset($this->_references[$idx])) {
130
+ $idx = $this->_references[$idx];
131
+ }
132
+ if (isset($this->_objects[$idx])) {
133
+ return $this->_objects[$idx];
134
+ }
135
+ return $default;
136
+ }
137
+
138
+ /**
139
+ * Save an object entry
140
+ *
141
+ * @param object $object
142
+ * @param string $idx
143
+ * @param array|string $tags
144
+ * @return string
145
+ */
146
+ public function save($object, $idx=null, $tags=null)
147
+ {
148
+ //Varien_Profiler::start('OBJECT_SAVE');
149
+ if (!is_object($object)) {
150
+ return false;
151
+ }
152
+
153
+ $hash = spl_object_hash($object);
154
+ if (!is_null($idx) && strpos($idx, '{')) {
155
+ $idx = str_replace('{hash}', $hash, $idx);
156
+ }
157
+ if (isset($this->_hashes[$hash])) {
158
+ //throw new Exception('test');
159
+ if (!is_null($idx)) {
160
+ $this->_references[$idx] = $this->_hashes[$hash];
161
+ }
162
+ return $this->_hashes[$hash];
163
+ }
164
+
165
+ if (is_null($idx)) {
166
+ $idx = '#'.(++$this->_idx);
167
+ }
168
+
169
+ if (isset($this->_objects[$idx])) {
170
+ throw new Varien_Exception('Object already exists in registry ('.$idx.'). Old object class: '.get_class($this->_objects[$idx]).', new object class: '.get_class($object));
171
+ }
172
+
173
+ $this->_objects[$idx] = $object;
174
+
175
+ $this->_hashes[$hash] = $idx;
176
+ $this->_objectHashes[$idx] = $hash;
177
+
178
+ if (is_string($tags)) {
179
+ $this->_tags[$tags][$idx] = true;
180
+ $this->_objectTags[$idx][$tags] = true;
181
+ } elseif (is_array($tags)) {
182
+ foreach ($tags as $t) {
183
+ $this->_tags[$t][$idx] = true;
184
+ $this->_objectTags[$idx][$t] = true;
185
+ }
186
+ }
187
+ //Varien_Profiler::stop('OBJECT_SAVE');
188
+
189
+ return $idx;
190
+ }
191
+
192
+ /**
193
+ * Add a reference to an object
194
+ *
195
+ * @param string|array $refName
196
+ * @param string $idx
197
+ * @return boolean
198
+ */
199
+ public function reference($refName, $idx)
200
+ {
201
+ if (is_array($refName)) {
202
+ foreach ($refName as $ref) {
203
+ $this->reference($ref, $idx);
204
+ }
205
+ return;
206
+ }
207
+
208
+ if (isset($this->_references[$refName])) {
209
+ throw new Varien_Exception('The reference already exists: '.$refName.'. New index: '.$idx.', old index: '.$this->_references[$refName]);
210
+ }
211
+ $this->_references[$refName] = $idx;
212
+ $this->_objectReferences[$idx][$refName] = true;
213
+
214
+ return true;
215
+ }
216
+
217
+ /**
218
+ * Delete an object from registry
219
+ *
220
+ * @param string|object $idx
221
+ * @return boolean
222
+ */
223
+ public function delete($idx)
224
+ {
225
+ //Varien_Profiler::start("OBJECT_DELETE");
226
+ if (is_object($idx)) {
227
+ $idx = $this->find($idx);
228
+ if (false===$idx) {
229
+ //Varien_Profiler::stop("OBJECT_DELETE");
230
+ return false;
231
+ }
232
+ unset($this->_objects[$idx]);
233
+ //Varien_Profiler::stop("OBJECT_DELETE");
234
+ return false;
235
+ } elseif (!isset($this->_objects[$idx])) {
236
+ //Varien_Profiler::stop("OBJECT_DELETE");
237
+ return false;
238
+ }
239
+
240
+ unset($this->_objects[$idx]);
241
+
242
+ unset($this->_hashes[$this->_objectHashes[$idx]], $this->_objectHashes[$idx]);
243
+
244
+ if (isset($this->_objectTags[$idx])) {
245
+ foreach ($this->_objectTags[$idx] as $t=>$dummy) {
246
+ unset($this->_tags[$t][$idx]);
247
+ }
248
+ unset($this->_objectTags[$idx]);
249
+ }
250
+
251
+ if (isset($this->_objectReferences[$idx])) {
252
+ foreach ($references as $r=>$dummy) {
253
+ unset($this->_references[$r]);
254
+ }
255
+ unset($this->_objectReferences[$idx]);
256
+ }
257
+ //Varien_Profiler::stop("OBJECT_DELETE");
258
+
259
+ return true;
260
+ }
261
+
262
+ /**
263
+ * Cleanup by class name for objects of subclasses too
264
+ *
265
+ * @param string $class
266
+ */
267
+ public function deleteByClass($class)
268
+ {
269
+ foreach ($this->_objects as $idx=>$object) {
270
+ if ($object instanceof $class) {
271
+ $this->delete($idx);
272
+ }
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Cleanup objects by tags
278
+ *
279
+ * @param array|string $tags
280
+ */
281
+ public function deleteByTags($tags)
282
+ {
283
+ if (is_string($tags)) {
284
+ $tags = array($tags);
285
+ }
286
+ foreach ($tags as $t) {
287
+ foreach ($this->_tags[$t] as $idx=>$dummy) {
288
+ $this->delete($idx);
289
+ }
290
+ }
291
+ return true;
292
+ }
293
+
294
+ /**
295
+ * Check whether object id exists in registry
296
+ *
297
+ * @param string $idx
298
+ * @return boolean
299
+ */
300
+ public function has($idx)
301
+ {
302
+ return isset($this->_objects[$idx]) || isset($this->_references[$idx]);
303
+ }
304
+
305
+ /**
306
+ * Find an object id
307
+ *
308
+ * @param object $object
309
+ * @return string|boolean
310
+ */
311
+ public function find($object)
312
+ {
313
+ foreach ($this->_objects as $idx=>$obj) {
314
+ if ($object===$obj) {
315
+ return $idx;
316
+ }
317
+ }
318
+ return false;
319
+ }
320
+
321
+ public function findByIds($ids)
322
+ {
323
+ $objects = array();
324
+ foreach ($this->_objects as $idx=>$obj) {
325
+ if (in_array($idx, $ids)) {
326
+ $objects[$idx] = $obj;
327
+ }
328
+ }
329
+ return $objects;
330
+ }
331
+
332
+ public function findByHash($hash)
333
+ {
334
+ return isset($this->_hashes[$hash]) ? $this->_objects[$this->_hashes[$hash]] : null;
335
+ }
336
+
337
+ /**
338
+ * Find objects by tags
339
+ *
340
+ * @param array|string $tags
341
+ * @return array
342
+ */
343
+ public function findByTags($tags)
344
+ {
345
+ if (is_string($tags)) {
346
+ $tags = array($tags);
347
+ }
348
+ $objects = array();
349
+ foreach ($tags as $t) {
350
+ foreach ($this->_tags[$t] as $idx=>$dummy) {
351
+ if (isset($objects[$idx])) {
352
+ continue;
353
+ }
354
+ $objects[$ids] = $this->load($idx);
355
+ }
356
+ }
357
+ return $objects;
358
+ }
359
+
360
+ /**
361
+ * Find by class name for objects of subclasses too
362
+ *
363
+ * @param string $class
364
+ */
365
+ public function findByClass($class)
366
+ {
367
+ $objects = array();
368
+ foreach ($this->_objects as $idx=>$object) {
369
+ if ($object instanceof $class) {
370
+ $objects[$idx] = $object;
371
+ }
372
+ }
373
+ return $objects;
374
+ }
375
+
376
+ public function debug($idx, $object=null)
377
+ {
378
+ $bt = debug_backtrace();
379
+ $debug = array();
380
+ foreach ($bt as $i=>$step) {
381
+ $debug[$i] = array(
382
+ 'file' => isset($step['file']) ? $step['file'] : null,
383
+ 'line' => isset($step['line']) ? $step['line'] : null,
384
+ 'function' => isset($step['function']) ? $step['function'] : null,
385
+ );
386
+ }
387
+ $this->_debug[$idx] = $debug;
388
+ }
389
+
390
+ /**
391
+ * Return debug information by ids
392
+ *
393
+ * @param array|integer $ids
394
+ * @return array
395
+ */
396
+ public function debugByIds($ids)
397
+ {
398
+ if (is_string($ids)) {
399
+ $ids = array($ids);
400
+ }
401
+ $debug = array();
402
+ foreach ($ids as $idx) {
403
+ $debug[$idx] = $this->_debug[$idx];
404
+ }
405
+ return $debug;
406
+ }
407
+
408
+ /**
409
+ * Get all objects
410
+ *
411
+ * @return array
412
+ */
413
+ public function getAllObjects()
414
+ {
415
+ return $this->_objects;
416
+ }
417
+
418
+ /**
419
+ * Get all tags
420
+ *
421
+ * @return array
422
+ */
423
+ public function getAllTags()
424
+ {
425
+ return $this->_tags;
426
+ }
427
+
428
+ /**
429
+ * Get all tags by object
430
+ *
431
+ * @return array
432
+ */
433
+ public function getAllTagsByObject()
434
+ {
435
+ return $this->_objectTags;
436
+ }
437
+
438
+ /**
439
+ * Get all references
440
+ *
441
+ * @return array
442
+ */
443
+ public function getAllReferences()
444
+ {
445
+ return $this->_references;
446
+ }
447
+
448
+ /**
449
+ * Get all references by object
450
+ *
451
+ * @return array
452
+ */
453
+ public function getAllReferencesByObject()
454
+ {
455
+ return $this->_referencesByObject;
456
+ }
457
+ }
lib/Varien/Object/Mapper.php ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Object
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Utility class for mapping data between objects or arrays
29
+ */
30
+ class Varien_Object_Mapper
31
+ {
32
+ /**
33
+ * Convert data from source to target item using map array
34
+ *
35
+ * Will get or set data with generic or magic, or specified Varien Object methods, or with array keys
36
+ * from or to Varien_Object or array
37
+ * :)
38
+ *
39
+ * Map must either be associative array of keys from=>to
40
+ * or a numeric array of keys, assuming from = to
41
+ *
42
+ * Defaults must be assoc array of keys => values. Target will get default, if the value is not present in source
43
+ * If the source has getter defined instead of magic method, the value will be taken only if not empty
44
+ *
45
+ * Callbacks explanation (when $from or $to is not array):
46
+ * for $from:
47
+ * <Varien_Object> => $from->getData($key) (default)
48
+ * array(<Varien_Object>, <method>) => $from->$method($key)
49
+ * for $to (makes sense only for Varien_Object):
50
+ * <Varien_Object> => $from->setData($key, <from>)
51
+ * array(<Varien_Object>, <method>) => $from->$method($key, <from>)
52
+ *
53
+ * @param array|Varien_Object|callback $from
54
+ * @param array|Varien_Object|callback $to
55
+ * @param array $map
56
+ * @param array $defaults
57
+ * @return array|object
58
+ */
59
+ public static function &accumulateByMap($from, $to, array $map, array $defaults = array())
60
+ {
61
+ $get = 'getData';
62
+ if (is_array($from) && isset($from[0]) && is_object($from[0]) && isset($from[1]) && is_string($from[1]) && is_callable($from)) {
63
+ list($from, $get) = $from;
64
+ }
65
+ $fromIsArray = is_array($from);
66
+ $fromIsVO = $from instanceof Varien_Object;
67
+
68
+ $set = 'setData';
69
+ if (is_array($to) && isset($to[0]) && is_object($to[0]) && isset($to[1]) && is_string($to[1]) && is_callable($to)) {
70
+ list($to, $set) = $to;
71
+ }
72
+ $toIsArray = is_array($to);
73
+ $toIsVO = $to instanceof Varien_Object;
74
+
75
+ foreach ($map as $keyFrom => $keyTo) {
76
+ if (!is_string($keyFrom)) {
77
+ $keyFrom = $keyTo;
78
+ }
79
+ if ($fromIsArray) {
80
+ if (array_key_exists($keyFrom, $from)) {
81
+ if ($toIsArray) {
82
+ $to[$keyTo] = $from[$keyFrom];
83
+ } elseif ($toIsVO) {
84
+ $to->$set($keyTo, $from[$keyFrom]);
85
+ }
86
+ }
87
+ } elseif ($fromIsVO) {
88
+ // get value if (any) value is found as in magic data or a non-empty value with declared getter
89
+ $value = null;
90
+ if ($shouldGet = $from->hasData($keyFrom)) {
91
+ $value = $from->$get($keyFrom);
92
+ } elseif (method_exists($from, $get)) {
93
+ $value = $from->$get($keyFrom);
94
+ if ($value) {
95
+ $shouldGet = true;
96
+ }
97
+ }
98
+ if ($shouldGet) {
99
+ if ($toIsArray) {
100
+ $to[$keyTo] = $value;
101
+ } elseif ($toIsVO) {
102
+ $to->$set($keyTo, $value);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ foreach ($defaults as $keyTo => $value) {
108
+ if ($toIsArray) {
109
+ if (!isset($to[$keyTo])) {
110
+ $to[$keyTo] = $value;
111
+ }
112
+ } elseif ($toIsVO) {
113
+ if (!$to->hasData($keyTo)) {
114
+ $to->$set($keyTo, $value);
115
+ }
116
+ }
117
+ }
118
+ return $to;
119
+ }
120
+ }
lib/Varien/Pear.php ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Pear
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Pear package routines
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Pear
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ // Looks like PEAR is being developed without E_NOTICE (1.7.0RC1)
36
+ error_reporting(E_ALL & ~E_NOTICE);
37
+
38
+ // just a shortcut
39
+ if (!defined('DS')) {
40
+ define('DS', DIRECTORY_SEPARATOR);
41
+ }
42
+
43
+ // add PEAR lib in include_path if needed
44
+ $_includePath = get_include_path();
45
+ $_pearDir = dirname(dirname(dirname(__FILE__))) . DS . 'downloader' . DS . 'pearlib';
46
+ if (!getenv('PHP_PEAR_INSTALL_DIR')) {
47
+ putenv('PHP_PEAR_INSTALL_DIR=' . $_pearDir);
48
+ }
49
+ $_pearPhpDir = $_pearDir . DS . 'php';
50
+ if (strpos($_includePath, $_pearPhpDir) === false) {
51
+ if (substr($_includePath, 0, 2) === '.' . PATH_SEPARATOR) {
52
+ $_includePath = '.' . PATH_SEPARATOR . $_pearPhpDir . PATH_SEPARATOR . substr($_includePath, 2);
53
+ } else {
54
+ $_includePath = $_pearPhpDir . PATH_SEPARATOR . $_includePath;
55
+ }
56
+ set_include_path($_includePath);
57
+ }
58
+
59
+ // include necessary PEAR libs
60
+ require_once $_pearPhpDir."/PEAR.php";
61
+ require_once $_pearPhpDir."/PEAR/Frontend.php";
62
+ require_once $_pearPhpDir."/PEAR/Registry.php";
63
+ require_once $_pearPhpDir."/PEAR/Config.php";
64
+ require_once $_pearPhpDir."/PEAR/Command.php";
65
+ require_once $_pearPhpDir."/PEAR/Exception.php";
66
+
67
+ require_once dirname(__FILE__)."/Pear/Frontend.php";
68
+ require_once dirname(__FILE__)."/Pear/Package.php";
69
+
70
+ class Varien_Pear
71
+ {
72
+ protected $_config;
73
+
74
+ protected $_registry;
75
+
76
+ protected $_frontend;
77
+
78
+ protected $_cmdCache = array();
79
+
80
+ static protected $_instance;
81
+
82
+ static public $reloadOnRegistryUpdate = true;
83
+
84
+ public function __construct()
85
+ {
86
+ $this->getConfig();
87
+ }
88
+
89
+ public function getInstance()
90
+ {
91
+ if (!self::$_instance) {
92
+ self::$_instance = new self;
93
+ }
94
+ return self::$_instance;
95
+ }
96
+
97
+ public function isSystemPackage($pkg)
98
+ {
99
+ return in_array($pkg, array('Archive_Tar', 'Console_Getopt', 'PEAR', 'Structures_Graph'));
100
+ }
101
+
102
+ public function getBaseDir()
103
+ {
104
+ return dirname(dirname(dirname(__FILE__)));
105
+ }
106
+
107
+ public function getPearDir()
108
+ {
109
+ return $this->getBaseDir().DS.'downloader'.DS.'pearlib';
110
+ }
111
+
112
+ public function getConfig()
113
+ {
114
+ if (!$this->_config) {
115
+ $pear_dir = $this->getPearDir();
116
+
117
+ $config = PEAR_Config::singleton($pear_dir.DS.'pear.ini', '-');
118
+
119
+ $config->set('auto_discover', 1);
120
+ $config->set('cache_ttl', 60);
121
+ #$config->set('preferred_state', 'beta');
122
+
123
+ $config->set('bin_dir', $pear_dir);
124
+ $config->set('php_dir', $pear_dir.DS.'php');
125
+ $config->set('download_dir', $pear_dir.DS.'download');
126
+ $config->set('temp_dir', $pear_dir.DS.'temp');
127
+ $config->set('data_dir', $pear_dir.DS.'data');
128
+ $config->set('cache_dir', $pear_dir.DS.'cache');
129
+ $config->set('test_dir', $pear_dir.DS.'tests');
130
+ $config->set('doc_dir', $pear_dir.DS.'docs');
131
+
132
+ $mageDir = $config->get('mage_dir');
133
+
134
+ foreach ($config->getKeys() as $key) {
135
+ if (!(substr($key, 0, 5)==='mage_' && substr($key, -4)==='_dir')) {
136
+ continue;
137
+ }
138
+ $config->set($key, preg_replace('#^'.preg_quote($mageDir).'#', $this->getBaseDir(), $config->get($key)));
139
+ #echo $key.' : '.$config->get($key).'<br>';
140
+ }
141
+
142
+ $reg = $this->getRegistry();
143
+ $config->setRegistry($reg);
144
+
145
+ PEAR_DependencyDB::singleton($config, $pear_dir.DS.'php'.DS.'.depdb');
146
+
147
+ PEAR_Frontend::setFrontendObject($this->getFrontend());
148
+
149
+ PEAR_Command::registerCommands(false, $pear_dir.DS.'php'.DS.'PEAR'.DS.'Command'.DS);
150
+
151
+ $this->_config = $config;
152
+ }
153
+ return $this->_config;
154
+ }
155
+
156
+ public function getMagentoChannels()
157
+ {
158
+ return array('connect.magentocommerce.com/core', 'connect.magentocommerce.com/community');
159
+ }
160
+
161
+ public function getRegistry($redirectOnChange=true)
162
+ {
163
+ if (!$this->_registry) {
164
+ $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
165
+
166
+ $changed = false;
167
+ foreach ($this->getMagentoChannels() as $channel) {
168
+ if (!$this->getRegistry()->channelExists($channel)) {
169
+ $this->run('channel-discover', array(), array($channel));
170
+ $changed = true;
171
+ }
172
+ }
173
+
174
+ if ($changed) {
175
+ $this->_registry = new Varien_Pear_Registry($this->getPearDir().DS.'php');
176
+ }
177
+ // if ($changed && self::$reloadOnRegistryUpdate && empty($_GET['pear_registry'])) {
178
+ // echo "TEST:";
179
+ // echo self::$reloadOnRegistryUpdate;
180
+ // //TODO:refresh registry in memory to reflect discovered channels without redirect
181
+ // #header("Location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'].'&pear_registry=1');
182
+ // exit;
183
+ // }
184
+ }
185
+ return $this->_registry;
186
+ }
187
+
188
+ public function getFrontend()
189
+ {
190
+ if (!$this->_frontend) {
191
+ $this->_frontend = new Varien_Pear_Frontend;
192
+ }
193
+ return $this->_frontend;
194
+ }
195
+
196
+ public function getLog()
197
+ {
198
+ return $this->getFrontend()->getLog();
199
+ }
200
+
201
+ public function getOutput()
202
+ {
203
+ return $this->getFrontend()->getOutput();
204
+ }
205
+
206
+ public function run($command, $options=array(), $params=array())
207
+ {
208
+ @set_time_limit(0);
209
+ @ini_set('memory_limit', '256M');
210
+
211
+ if (empty($this->_cmdCache[$command])) {
212
+ $cmd = PEAR_Command::factory($command, $this->getConfig());
213
+ if ($cmd instanceof PEAR_Error) {
214
+ return $cmd;
215
+ }
216
+ $this->_cmdCache[$command] = $cmd;
217
+ } else {
218
+ $cmd = $this->_cmdCache[$command];
219
+ }
220
+ #$cmd = PEAR_Command::factory($command, $this->getConfig());
221
+ $result = $cmd->run($command, $options, $params);
222
+ return $result;
223
+ }
224
+
225
+ public function setRemoteConfig($uri) #$host, $user, $password, $path='', $port=null)
226
+ {
227
+ #$uri = 'ftp://' . $user . ':' . $password . '@' . $host . (is_numeric($port) ? ':' . $port : '') . '/' . trim($path, '/') . '/';
228
+ $this->run('config-set', array(), array('remote_config', $uri));
229
+ return $this;
230
+ }
231
+
232
+ /**
233
+ * Run PEAR command with html output console style
234
+ *
235
+ * @param array|Varien_Object $runParams command, options, params,
236
+ * comment, success_callback, failure_callback
237
+ */
238
+ public function runHtmlConsole($runParams)
239
+ {
240
+ ob_implicit_flush();
241
+
242
+ $fe = $this->getFrontend();
243
+ $oldLogStream = $fe->getLogStream();
244
+ $fe->setLogStream('stdout');
245
+
246
+ if ($runParams instanceof Varien_Object) {
247
+ $run = $runParams;
248
+ } elseif (is_array($runParams)) {
249
+ $run = new Varien_Object($runParams);
250
+ } elseif (is_string($runParams)) {
251
+ $run = new Varien_Object(array('title'=>$runParams));
252
+ } else {
253
+ throw Varien_Exception("Invalid run parameters");
254
+ }
255
+ ?>
256
+ <html><head><style type="text/css">
257
+ body { margin:0px; padding:3px; background:black; color:white; }
258
+ pre { font:normal 11px Courier New, serif; color:#2EC029; }
259
+ </style></head><body>
260
+ <?php
261
+ echo "<pre>".$run->getComment();
262
+
263
+ if ($command = $run->getCommand()) {
264
+ $result = $this->run($command, $run->getOptions(), $run->getParams());
265
+
266
+ if ($result instanceof PEAR_Error) {
267
+ echo "\r\n\r\nPEAR ERROR: ".$result->getMessage();
268
+ }
269
+ echo '</pre><script type="text/javascript">';
270
+ if ($result instanceof PEAR_Error) {
271
+ if ($callback = $run->getFailureCallback()) {
272
+ call_user_func_array($callback, array($result));
273
+ }
274
+ } else {
275
+ if ($callback = $run->getSuccessCallback()) {
276
+ call_user_func_array($callback, array($result));
277
+ }
278
+ }
279
+ echo '</script>';
280
+ } else {
281
+ $result = false;
282
+
283
+ echo '</pre>';
284
+ }
285
+ ?>
286
+ <script type="text/javascript">
287
+ if (!auto_scroll) {
288
+ var auto_scroll = window.setInterval("document.body.scrollTop+=2", 10);
289
+ }
290
+ </script>
291
+ </body></html>
292
+ <?php
293
+ $fe->setLogStream($oldLogStream);
294
+
295
+ return $result;
296
+ }
297
+ }
lib/Varien/Pear/Frontend.php ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Pear
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Pear frontend routines
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Pear
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_Pear_Frontend extends PEAR_Frontend
36
+ {
37
+ protected $_logStream = null;
38
+ protected $_outStream = null;
39
+ protected $_log = array();
40
+ protected $_out = array();
41
+
42
+ /**
43
+ * Enter description here...
44
+ *
45
+ * @param string|resource $stream 'stdout' or open php stream
46
+ */
47
+ public function setLogStream($stream)
48
+ {
49
+ $this->_logStream = $stream;
50
+ return $this;
51
+ }
52
+
53
+ public function getLogStream()
54
+ {
55
+ return $this->_logStream;
56
+ }
57
+
58
+ public function log($msg, $append_crlf = true)
59
+ {
60
+ if (is_null($msg) || false===$msg or ''===$msg) {
61
+ return;
62
+ }
63
+
64
+ if ($append_crlf) {
65
+ $msg .= "\r\n";
66
+ }
67
+
68
+ $this->_log[] = $msg;
69
+
70
+ if ('stdout'===$this->_logStream) {
71
+ if ($msg==='.') {
72
+ echo ' ';
73
+ }
74
+ echo $msg;
75
+ }
76
+ elseif (is_resource($this->_logStream)) {
77
+ fwrite($this->_logStream, $msg);
78
+ }
79
+ }
80
+
81
+ public function outputData($data, $command = '_default')
82
+ {
83
+ $this->_out[] = array('output'=>$data, 'command'=>$command);
84
+
85
+ if ('stdout'===$this->_logStream) {
86
+ if (is_string($data)) {
87
+ echo $data."\r\n";
88
+ } elseif (is_array($data) && !empty($data['message']) && is_string($data['message'])) {
89
+ echo $data['message']."\r\n";
90
+ } elseif (is_array($data) && !empty($data['data']) && is_string($data['data'])) {
91
+ echo $data['data']."\r\n";
92
+ } else {
93
+ print_r($data);
94
+ }
95
+ }
96
+ }
97
+
98
+ public function userConfirm()
99
+ {
100
+
101
+ }
102
+
103
+ public function clear()
104
+ {
105
+ $this->_log = array();
106
+ $this->_out = array();
107
+ }
108
+
109
+ public function getLog()
110
+ {
111
+ return $this->_log;
112
+ }
113
+
114
+ public function getLogText()
115
+ {
116
+ $text = '';
117
+ foreach ($this->getLog() as $log) {
118
+ $text .= $log;
119
+ }
120
+ return $text;
121
+ }
122
+
123
+ public function getOutput()
124
+ {
125
+ return $this->_out;
126
+ }
127
+ }
lib/Varien/Pear/Package.php ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Pear
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Pear package routines
29
+ * *
30
+ * @category Varien
31
+ * @package Varien_Pear
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ require_once "Varien/Pear.php";
36
+
37
+ require_once "PEAR/PackageFileManager.php";
38
+ require_once "PEAR/PackageFile/v1.php";
39
+ require_once "PEAR/PackageFile/Generator/v1.php";
40
+
41
+ require_once "PEAR/PackageFileManager2.php";
42
+ require_once "PEAR/PackageFile/v2.php";
43
+ require_once "PEAR/PackageFile/v2/rw.php";
44
+ require_once "PEAR/PackageFile/v2/Validator.php";
45
+ require_once "PEAR/PackageFile/Generator/v2.php";
46
+
47
+ // add missing but required constant...
48
+ define ('PEAR_PACKAGEFILEMANAGER_NOSVNENTRIES', 1001);
49
+ $GLOBALS['_PEAR_PACKAGEFILEMANAGER2_ERRORS']['en']['PEAR_PACKAGEFILEMANAGER_NOSVNENTRIES'] =
50
+ 'Directory "%s" is not a SVN directory (it must have the .svn/entries file)';
51
+
52
+ class Varien_Pear_Package
53
+ {
54
+ protected $_data = array(
55
+ 'options' => array(
56
+ 'baseinstalldir'=>'',
57
+ 'filelistgenerator'=>'file',
58
+ 'packagedirectory'=>'.',
59
+ 'outputdirectory'=>'.',
60
+ ),
61
+ 'package' => array(),
62
+ 'release' => array(),
63
+ );
64
+
65
+ protected $_pear;
66
+ protected $_pfm;
67
+
68
+ public function __construct()
69
+ {
70
+ $this->_pear = Varien_Pear::getInstance();
71
+ }
72
+
73
+ public function getPear()
74
+ {
75
+ return $this->_pear;
76
+ }
77
+
78
+ public function getPearConfig($key)
79
+ {
80
+ return $this->getPear()->getConfig()->get($key);
81
+ }
82
+
83
+ public function set($key, $data)
84
+ {
85
+ if (''===$key) {
86
+ $this->_data = $data;
87
+ return $this;
88
+ }
89
+
90
+ // accept a/b/c as ['a']['b']['c']
91
+ $keyArr = explode('/', $key);
92
+
93
+ $ref = &$this->_data;
94
+ for ($i=0, $l=sizeof($keyArr); $i<$l; $i++) {
95
+ $k = $keyArr[$i];
96
+ if (!isset($ref[$k])) {
97
+ $ref[$k] = array();
98
+ }
99
+ $ref = &$ref[$k];
100
+ }
101
+ $ref = $data;
102
+
103
+ return $this;
104
+ }
105
+
106
+ public function get($key)
107
+ {
108
+ if (''===$key) {
109
+ return $this->_data;
110
+ }
111
+
112
+ // accept a/b/c as ['a']['b']['c']
113
+ $keyArr = explode('/', $key);
114
+ $data = $this->_data;
115
+ foreach ($keyArr as $i=>$k) {
116
+ if ($k==='') {
117
+ return null;
118
+ }
119
+ if (is_array($data)) {
120
+ if (!isset($data[$k])) {
121
+ return null;
122
+ }
123
+ $data = $data[$k];
124
+ } else {
125
+ return null;
126
+ }
127
+ }
128
+ return $data;
129
+ }
130
+
131
+ public function setPfm($pfm)
132
+ {
133
+ $this->_pfm = $pfm;
134
+ return $this;
135
+ }
136
+
137
+ /**
138
+ * Get PackageFileManager2 instance
139
+ *
140
+ * @param string|PEAR_PackageFile_v1 $package
141
+ * @return PEAR_PackageFileManager2|PEAR_Error
142
+ */
143
+ public function getPfm($package=null)
144
+ {
145
+ if (!$this->_pfm) {
146
+ if (is_null($package)) {
147
+ $this->_pfm = new PEAR_PackageFileManager2;
148
+ $this->_pfm->setOptions($this->get('options'));
149
+ } else {
150
+ $this->defineData();
151
+
152
+ $this->_pfm = PEAR_PackageFileManager2::importOptions($package, $this->get('options'));
153
+ if ($this->_pfm instanceof PEAR_Error) {
154
+ $e = PEAR_Exception('Could not instantiate PEAR_PackageFileManager2');
155
+ $e->errorObject = $this->_pfm;
156
+ throw $e;
157
+ }
158
+ }
159
+ }
160
+ return $this->_pfm;
161
+ }
162
+
163
+ public function clearPackage()
164
+ {
165
+ $pfm = $this->getPfm();
166
+ $pfm->clearContents();
167
+ $pfm->clearCompatible();
168
+ $pfm->clearDeps();
169
+ $pfm->clearChangeLog();
170
+ return $this;
171
+ }
172
+
173
+ public function generatePackage($make=false)
174
+ {
175
+ PEAR::setErrorHandling(PEAR_ERROR_DIE);
176
+
177
+ $this->definePackage();
178
+
179
+ $pfm = $this->getPfm();
180
+ $pfm->addRelease();
181
+
182
+ $this->defineRelease();
183
+
184
+ $pfm->generateContents();
185
+ $pfm1 = $pfm->exportCompatiblePackageFile1($this->get('options'));
186
+
187
+ if ($make) {
188
+ $pfm1->writePackageFile();
189
+ $pfm->writePackageFile();
190
+
191
+ $outputDir = $this->get('options/outputdirectory');
192
+ MagePearWrapper::getInstance()->run('package', array(),
193
+ array($outputDir.'package.xml', $outputDir.'package2.xml')
194
+ );
195
+ } else {
196
+ $pfm1->debugPackageFile();
197
+ $pfm->debugPackageFile();
198
+ }
199
+
200
+ return $this;
201
+ }
202
+
203
+ public function defineData()
204
+ {
205
+ $this->set('options/outputdirectory', $this->getPear()->getPearDir().DS.'output');
206
+ $this->set('options/filelistgenerator', 'php');
207
+ $this->set('options/simpleoutput', true);
208
+
209
+ return $this;
210
+ }
211
+
212
+ public function definePackage()
213
+ {
214
+ return $this;
215
+ }
216
+
217
+ public function defineRelease()
218
+ {
219
+ return $this;
220
+ }
221
+ }
lib/Varien/Pear/Registry.php ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Pear
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+ /**
28
+ * Pear Registry
29
+ *
30
+ * @category Varien
31
+ * @package Varien_Pear
32
+ * @author Magento Core Team <core@magentocommerce.com>
33
+ */
34
+
35
+ class Varien_Pear_Registry extends PEAR_Registry
36
+ {
37
+ function _initializeDepDB()
38
+ {
39
+ if (!isset($this->_dependencyDB)) {
40
+ static $initializing = false;
41
+ if (!$initializing) {
42
+ $initializing = true;
43
+ if (!$this->_config) { // never used?
44
+ if (OS_WINDOWS) {
45
+ $file = 'pear.ini';
46
+ } else {
47
+ $file = '.pearrc';
48
+ }
49
+ $this->_config = &new PEAR_Config($this->statedir . DIRECTORY_SEPARATOR .
50
+ $file, '-'); // NO SYSTEM INI FILE
51
+ $this->_config->setRegistry($this);
52
+ $this->_config->set('php_dir', $this->install_dir);
53
+ }
54
+ $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
55
+ if (PEAR::isError($this->_dependencyDB)) {
56
+ // attempt to recover by removing the dep db
57
+ if (file_exists($this->_config->get('php_dir', null, 'pear.php.net') .
58
+ DIRECTORY_SEPARATOR . '.depdb')) {
59
+ @unlink($this->_config->get('php_dir', null, 'pear.php.net') .
60
+ DIRECTORY_SEPARATOR . '.depdb');
61
+ }
62
+ $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
63
+ if (PEAR::isError($this->_dependencyDB)) {
64
+ echo $this->_dependencyDB->getMessage();
65
+ echo 'Unrecoverable error';
66
+ exit(1);
67
+ }
68
+ }
69
+ $initializing = false;
70
+ }
71
+ }
72
+ }
73
+ }
lib/Varien/Profiler.php ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Profiler
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ class Varien_Profiler
29
+ {
30
+
31
+ /**
32
+ * Timers for code profiling
33
+ *
34
+ * @var array
35
+ */
36
+ static private $_timers = array();
37
+ static private $_enabled = false;
38
+ static private $_memory_get_usage = false;
39
+
40
+ public static function enable()
41
+ {
42
+ self::$_enabled = true;
43
+ self::$_memory_get_usage = function_exists('memory_get_usage');
44
+ }
45
+
46
+ public static function disable()
47
+ {
48
+ self::$_enabled = false;
49
+ }
50
+
51
+ public static function reset($timerName)
52
+ {
53
+ self::$_timers[$timerName] = array(
54
+ 'start'=>false,
55
+ 'count'=>0,
56
+ 'sum'=>0,
57
+ 'realmem'=>0,
58
+ 'emalloc'=>0,
59
+ );
60
+ }
61
+
62
+ public static function resume($timerName)
63
+ {
64
+ if (!self::$_enabled) {
65
+ return;
66
+ }
67
+
68
+ if (empty(self::$_timers[$timerName])) {
69
+ self::reset($timerName);
70
+ }
71
+ if (self::$_memory_get_usage) {
72
+ self::$_timers[$timerName]['realmem_start'] = memory_get_usage(true);
73
+ self::$_timers[$timerName]['emalloc_start'] = memory_get_usage();
74
+ }
75
+ self::$_timers[$timerName]['start'] = microtime(true);
76
+ self::$_timers[$timerName]['count'] ++;
77
+ }
78
+
79
+ public static function start($timerName)
80
+ {
81
+ self::resume($timerName);
82
+ }
83
+
84
+ public static function pause($timerName)
85
+ {
86
+ if (!self::$_enabled) {
87
+ return;
88
+ }
89
+
90
+ $time = microtime(true); // Get current time as quick as possible to make more accurate calculations
91
+
92
+ if (empty(self::$_timers[$timerName])) {
93
+ self::reset($timerName);
94
+ }
95
+ if (false!==self::$_timers[$timerName]['start']) {
96
+ self::$_timers[$timerName]['sum'] += $time-self::$_timers[$timerName]['start'];
97
+ self::$_timers[$timerName]['start'] = false;
98
+ if (self::$_memory_get_usage) {
99
+ self::$_timers[$timerName]['realmem'] += memory_get_usage(true)-self::$_timers[$timerName]['realmem_start'];
100
+ self::$_timers[$timerName]['emalloc'] += memory_get_usage()-self::$_timers[$timerName]['emalloc_start'];
101
+ }
102
+ }
103
+ }
104
+
105
+ public static function stop($timerName)
106
+ {
107
+ self::pause($timerName);
108
+ }
109
+
110
+ public static function fetch($timerName, $key='sum')
111
+ {
112
+ if (empty(self::$_timers[$timerName])) {
113
+ return false;
114
+ } elseif (empty($key)) {
115
+ return self::$_timers[$timerName];
116
+ }
117
+ switch ($key) {
118
+ case 'sum':
119
+ $sum = self::$_timers[$timerName]['sum'];
120
+ if (self::$_timers[$timerName]['start']!==false) {
121
+ $sum += microtime(true)-self::$_timers[$timerName]['start'];
122
+ }
123
+ return $sum;
124
+
125
+ case 'count':
126
+ $count = self::$_timers[$timerName]['count'];
127
+ return $count;
128
+
129
+ case 'realmem':
130
+ if (!isset(self::$_timers[$timerName]['realmem'])) {
131
+ self::$_timers[$timerName]['realmem'] = -1;
132
+ }
133
+ return self::$_timers[$timerName]['realmem'];
134
+
135
+ case 'emalloc':
136
+ if (!isset(self::$_timers[$timerName]['emalloc'])) {
137
+ self::$_timers[$timerName]['emalloc'] = -1;
138
+ }
139
+ return self::$_timers[$timerName]['emalloc'];
140
+
141
+ default:
142
+ if (!empty(self::$_timers[$timerName][$key])) {
143
+ return self::$_timers[$timerName][$key];
144
+ }
145
+ }
146
+ return false;
147
+ }
148
+
149
+ public static function getTimers()
150
+ {
151
+ return self::$_timers;
152
+ }
153
+
154
+ /**
155
+ * Output SQl Zend_Db_Profiler
156
+ *
157
+ */
158
+ public static function getSqlProfiler($res) {
159
+ if(!$res){
160
+ return '';
161
+ }
162
+ $out = '';
163
+ $profiler = $res->getProfiler();
164
+ if($profiler->getEnabled()) {
165
+ $totalTime = $profiler->getTotalElapsedSecs();
166
+ $queryCount = $profiler->getTotalNumQueries();
167
+ $longestTime = 0;
168
+ $longestQuery = null;
169
+
170
+ foreach ($profiler->getQueryProfiles() as $query) {
171
+ if ($query->getElapsedSecs() > $longestTime) {
172
+ $longestTime = $query->getElapsedSecs();
173
+ $longestQuery = $query->getQuery();
174
+ }
175
+ }
176
+
177
+ $out .= 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "<br>";
178
+ $out .= 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br>";
179
+ $out .= 'Queries per second: ' . $queryCount / $totalTime . "<br>";
180
+ $out .= 'Longest query length: ' . $longestTime . "<br>";
181
+ $out .= 'Longest query: <br>' . $longestQuery . "<hr>";
182
+ }
183
+ return $out;
184
+ }
185
+ }
lib/Varien/Simplexml/Config.php ADDED
@@ -0,0 +1,604 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Simplexml
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Base class for simplexml based configurations
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Simplexml
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Simplexml_Config
36
+ {
37
+
38
+ /**
39
+ * Configuration xml
40
+ *
41
+ * @var Varien_Simplexml_Element
42
+ */
43
+ protected $_xml = null;
44
+
45
+ /**
46
+ * Enter description here...
47
+ *
48
+ * @var string
49
+ */
50
+ protected $_cacheId = null;
51
+
52
+ /**
53
+ * Enter description here...
54
+ *
55
+ * @var array
56
+ */
57
+ protected $_cacheTags = array();
58
+
59
+ /**
60
+ * Enter description here...
61
+ *
62
+ * @var int
63
+ */
64
+ protected $_cacheLifetime = null;
65
+
66
+ /**
67
+ * Enter description here...
68
+ *
69
+ * @var unknown_type
70
+ */
71
+ protected $_cacheChecksum = false;
72
+
73
+ /**
74
+ * Enter description here...
75
+ *
76
+ * @var boolean
77
+ */
78
+ protected $_cacheSaved = false;
79
+
80
+ /**
81
+ * Cache resource object
82
+ *
83
+ * @var Varien_Simplexml_Config_Cache_Abstract
84
+ */
85
+ protected $_cache = null;
86
+
87
+ /**
88
+ * Class name of simplexml elements for this configuration
89
+ *
90
+ * @var string
91
+ */
92
+ protected $_elementClass = 'Varien_Simplexml_Element';
93
+
94
+ /**
95
+ * Xpath describing nodes in configuration that need to be extended
96
+ *
97
+ * @example <allResources extends="/config/modules//resource"/>
98
+ */
99
+ protected $_xpathExtends = "//*[@extends]";
100
+
101
+ /**
102
+ * Constructor
103
+ *
104
+ * Initializes XML for this configuration
105
+ *
106
+ * @see self::setXml
107
+ * @param string|Varien_Simplexml_Element $sourceData
108
+ * @param string $sourceType
109
+ */
110
+ public function __construct($sourceData=null) {
111
+ if (is_null($sourceData)) {
112
+ return;
113
+ }
114
+ if ($sourceData instanceof Varien_Simplexml_Element) {
115
+ $this->setXml($sourceData);
116
+ } elseif (is_string($sourceData) && !empty($sourceData)) {
117
+ if (strlen($sourceData)<1000 && is_readable($sourceData)) {
118
+ $this->loadFile($sourceData);
119
+ } else {
120
+ $this->loadString($sourceData);
121
+ }
122
+ }
123
+ #$this->setCache(new Varien_Simplexml_Config_Cache_File());
124
+ #$this->getCache()->setConfig($this);
125
+ }
126
+
127
+ /**
128
+ * Sets xml for this configuration
129
+ *
130
+ * @param Varien_Simplexml_Element $sourceData
131
+ * @return Varien_Simplexml_Config
132
+ */
133
+ public function setXml(Varien_Simplexml_Element $node)
134
+ {
135
+ $this->_xml = $node;
136
+ return $this;
137
+ }
138
+
139
+ /**
140
+ * Returns node found by the $path
141
+ *
142
+ * @see Varien_Simplexml_Element::descend
143
+ * @param string $path
144
+ * @return Varien_Simplexml_Element
145
+ */
146
+ public function getNode($path=null)
147
+ {
148
+ if (!$this->_xml instanceof Varien_Simplexml_Element) {
149
+ return false;
150
+ } elseif ($path === null) {
151
+ return $this->_xml;
152
+ } else {
153
+ return $this->_xml->descend($path);
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Returns nodes found by xpath expression
159
+ *
160
+ * @param string $xpath
161
+ * @return array
162
+ */
163
+ public function getXpath($xpath)
164
+ {
165
+ if (empty($this->_xml)) {
166
+ return false;
167
+ }
168
+
169
+ if (!$result = @$this->_xml->xpath($xpath)) {
170
+ return false;
171
+ }
172
+
173
+ return $result;
174
+ }
175
+
176
+ /**
177
+ * Enter description here...
178
+ *
179
+ * @param Varien_Simplexml_Config_Cache_Abstract $cache
180
+ * @return Varien_Simplexml_Config
181
+ */
182
+ public function setCache($cache)
183
+ {
184
+ $this->_cache = $cache;
185
+ return $this;
186
+ }
187
+
188
+ /**
189
+ * Enter description here...
190
+ *
191
+ * @return Varien_Simplexml_Config_Cache_Abstract
192
+ */
193
+ public function getCache()
194
+ {
195
+ return $this->_cache;
196
+ }
197
+
198
+ /**
199
+ * Enter description here...
200
+ *
201
+ * @param boolean $flag
202
+ * @return Varien_Simplexml_Config
203
+ */
204
+ public function setCacheSaved($flag)
205
+ {
206
+ $this->_cacheSaved = $flag;
207
+ return $this;
208
+ }
209
+
210
+ /**
211
+ * Enter description here...
212
+ *
213
+ * @return boolean
214
+ */
215
+ public function getCacheSaved()
216
+ {
217
+ return $this->_cacheSaved;
218
+ }
219
+
220
+ /**
221
+ * Enter description here...
222
+ *
223
+ * @param string $id
224
+ * @return Varien_Simplexml_Config
225
+ */
226
+ public function setCacheId($id)
227
+ {
228
+ $this->_cacheId = $id;
229
+ return $this;
230
+ }
231
+
232
+ /**
233
+ * Enter description here...
234
+ *
235
+ * @return string
236
+ */
237
+ public function getCacheId()
238
+ {
239
+ return $this->_cacheId;
240
+ }
241
+
242
+ /**
243
+ * Enter description here...
244
+ *
245
+ * @param array $tags
246
+ * @return Varien_Simplexml_Config
247
+ */
248
+ public function setCacheTags($tags)
249
+ {
250
+ $this->_cacheTags = $tags;
251
+ return $this;
252
+ }
253
+
254
+ /**
255
+ * Enter description here...
256
+ *
257
+ * @return array
258
+ */
259
+ public function getCacheTags()
260
+ {
261
+ return $this->_cacheTags;
262
+ }
263
+
264
+ /**
265
+ * Enter description here...
266
+ *
267
+ * @param int $lifetime
268
+ * @return Varien_Simplexml_Config
269
+ */
270
+ public function setCacheLifetime($lifetime)
271
+ {
272
+ $this->_cacheLifetime = $lifetime;
273
+ return $this;
274
+ }
275
+
276
+ /**
277
+ * Enter description here...
278
+ *
279
+ * @return int
280
+ */
281
+ public function getCacheLifetime()
282
+ {
283
+ return $this->_cacheLifetime;
284
+ }
285
+
286
+ /**
287
+ * Enter description here...
288
+ *
289
+ * @param string $data
290
+ * @return Varien_Simplexml_Config
291
+ */
292
+ public function setCacheChecksum($data)
293
+ {
294
+ if (is_null($data)) {
295
+ $this->_cacheChecksum = null;
296
+ } elseif (false===$data || 0===$data) {
297
+ $this->_cacheChecksum = false;
298
+ } else {
299
+ $this->_cacheChecksum = md5($data);
300
+ }
301
+ return $this;
302
+ }
303
+
304
+ /**
305
+ * Enter description here...
306
+ *
307
+ * @param string $data
308
+ * @return Varien_Simplexml_Config
309
+ */
310
+ public function updateCacheChecksum($data)
311
+ {
312
+ if (false===$this->getCacheChecksum()) {
313
+ return $this;
314
+ }
315
+ if (false===$data || 0===$data) {
316
+ $this->_cacheChecksum = false;
317
+ } else {
318
+ $this->setCacheChecksum($this->getCacheChecksum().':'.$data);
319
+ }
320
+ return $this;
321
+ }
322
+
323
+ /**
324
+ * Enter description here...
325
+ *
326
+ * @return string
327
+ */
328
+ public function getCacheChecksum()
329
+ {
330
+ return $this->_cacheChecksum;
331
+ }
332
+
333
+ /**
334
+ * Enter description here...
335
+ *
336
+ * @return string
337
+ */
338
+ public function getCacheChecksumId()
339
+ {
340
+ return $this->getCacheId().'__CHECKSUM';
341
+ }
342
+
343
+ /**
344
+ * Enter description here...
345
+ *
346
+ * @return boolean
347
+ */
348
+ public function fetchCacheChecksum()
349
+ {
350
+ return false;
351
+ }
352
+
353
+ /**
354
+ * Enter description here...
355
+ *
356
+ * @return boolean
357
+ */
358
+ public function validateCacheChecksum()
359
+ {
360
+ $newChecksum = $this->getCacheChecksum();
361
+ if (false===$newChecksum) {
362
+ return false;
363
+ }
364
+ if (is_null($newChecksum)) {
365
+ return true;
366
+ }
367
+ $cachedChecksum = $this->getCache()->load($this->getCacheChecksumId());
368
+ return $newChecksum===false && $cachedChecksum===false || $newChecksum===$cachedChecksum;
369
+ }
370
+
371
+ /**
372
+ * Enter description here...
373
+ *
374
+ * @return boolean
375
+ */
376
+ public function loadCache()
377
+ {
378
+ if (!$this->validateCacheChecksum()) {
379
+ return false;
380
+ }
381
+
382
+ $xmlString = $this->_loadCache($this->getCacheId());
383
+ $xml = simplexml_load_string($xmlString, $this->_elementClass);
384
+ if ($xml) {
385
+ $this->_xml = $xml;
386
+ $this->setCacheSaved(true);
387
+ return true;
388
+ }
389
+
390
+ return false;
391
+ }
392
+
393
+ /**
394
+ * Enter description here...
395
+ *
396
+ * @param array $tags
397
+ * @return Varien_Simplexml_Config
398
+ */
399
+ public function saveCache($tags=null)
400
+ {
401
+ if ($this->getCacheSaved()) {
402
+ return $this;
403
+ }
404
+ if (false===$this->getCacheChecksum()) {
405
+ return $this;
406
+ }
407
+
408
+ if (is_null($tags)) {
409
+ $tags = $this->_cacheTags;
410
+ }
411
+
412
+ if (!is_null($this->getCacheChecksum())) {
413
+ $this->_saveCache($this->getCacheChecksum(), $this->getCacheChecksumId(), $tags, $this->getCacheLifetime());
414
+ }
415
+
416
+ $xmlString = $this->getXmlString();
417
+ $this->_saveCache($xmlString, $this->getCacheId(), $tags, $this->getCacheLifetime());
418
+
419
+ $this->setCacheSaved(true);
420
+
421
+ return $this;
422
+ }
423
+
424
+ /**
425
+ * Return Xml of node as string
426
+ *
427
+ * @return string
428
+ */
429
+ public function getXmlString()
430
+ {
431
+ return $this->getNode()->asNiceXml('', false);
432
+ }
433
+
434
+ /**
435
+ * Enter description here...
436
+ *
437
+ * @return Varien_Simplexml_Config
438
+ */
439
+ public function removeCache()
440
+ {
441
+ $this->_removeCache($this->getCacheId());
442
+ $this->_removeCache($this->getCacheChecksumId());
443
+ return $this;
444
+ }
445
+
446
+ /**
447
+ * Enter description here...
448
+ *
449
+ * @param string $id
450
+ * @return boolean
451
+ */
452
+ protected function _loadCache($id)
453
+ {
454
+ return $this->getCache()->load($id);
455
+ }
456
+
457
+ /**
458
+ * Enter description here...
459
+ *
460
+ * @param string $data
461
+ * @param string $id
462
+ * @param array $tags
463
+ * @param int|boolean $lifetime
464
+ * @return boolean
465
+ */
466
+ protected function _saveCache($data, $id, $tags=array(), $lifetime=false)
467
+ {
468
+ return $this->getCache()->save($data, $id, $tags, $lifetime);
469
+ }
470
+
471
+ /**
472
+ * Enter description here...
473
+ *
474
+ * @todo check this, as there are no caches that implement remove() method
475
+ * @param string $id
476
+ * @return unknown
477
+ */
478
+ protected function _removeCache($id)
479
+ {
480
+ return $this->getCache()->remove($id);
481
+ }
482
+
483
+ /**
484
+ * Imports XML file
485
+ *
486
+ * @param string $filePath
487
+ * @return boolean
488
+ */
489
+ public function loadFile($filePath)
490
+ {
491
+ if (!is_readable($filePath)) {
492
+ //throw new Exception('Can not read xml file '.$filePath);
493
+ return false;
494
+ }
495
+
496
+ $fileData = file_get_contents($filePath);
497
+ $fileData = $this->processFileData($fileData);
498
+ return $this->loadString($fileData, $this->_elementClass);
499
+ }
500
+
501
+ /**
502
+ * Imports XML string
503
+ *
504
+ * @param string $string
505
+ * @return boolean
506
+ */
507
+ public function loadString($string)
508
+ {
509
+ if (is_string($string)) {
510
+ $xml = simplexml_load_string($string, $this->_elementClass);
511
+
512
+ if ($xml instanceof Varien_Simplexml_Element) {
513
+ $this->_xml = $xml;
514
+ return true;
515
+ }
516
+ } else {
517
+ Mage::logException(new Exception('"$string" parameter for simplexml_load_string is not a string'));
518
+ }
519
+ return false;
520
+ }
521
+
522
+ /**
523
+ * Imports DOM node
524
+ *
525
+ * @param DOMNode $dom
526
+ * @return Varien_Simplexml_Element
527
+ */
528
+ public function loadDom($dom)
529
+ {
530
+ $xml = simplexml_import_dom($dom, $this->_elementClass);
531
+
532
+ if ($xml) {
533
+ $this->_xml = $xml;
534
+ return true;
535
+ }
536
+
537
+ return false;
538
+ }
539
+
540
+ /**
541
+ * Create node by $path and set its value.
542
+ *
543
+ * @param string $path separated by slashes
544
+ * @param string $value
545
+ * @param boolean $overwrite
546
+ * @return Varien_Simplexml_Config
547
+ */
548
+ public function setNode($path, $value, $overwrite=true)
549
+ {
550
+ $xml = $this->_xml->setNode($path, $value, $overwrite);
551
+ return $this;
552
+ }
553
+
554
+ /**
555
+ * Process configuration xml
556
+ *
557
+ * @return Varien_Simplexml_Config
558
+ */
559
+ public function applyExtends()
560
+ {
561
+ $targets = $this->getXpath($this->_xpathExtends);
562
+ if (!$targets) {
563
+ return $this;
564
+ }
565
+
566
+ foreach ($targets as $target) {
567
+ $sources = $this->getXpath((string)$target['extends']);
568
+ if ($sources) {
569
+ foreach ($sources as $source) {
570
+ $target->extend($source);
571
+ }
572
+ } else {
573
+ #echo "Not found extend: ".(string)$target['extends'];
574
+ }
575
+ #unset($target['extends']);
576
+ }
577
+ return $this;
578
+ }
579
+
580
+ /**
581
+ * Stub method for processing file data right after loading the file text
582
+ *
583
+ * @param string $text
584
+ * @return string
585
+ */
586
+ public function processFileData($text)
587
+ {
588
+ return $text;
589
+ }
590
+
591
+ /**
592
+ * Enter description here...
593
+ *
594
+ * @param Varien_Simplexml_Config $config
595
+ * @param boolean $overwrite
596
+ * @return Varien_Simplexml_Config
597
+ */
598
+ public function extend(Varien_Simplexml_Config $config, $overwrite=true)
599
+ {
600
+ $this->getNode()->extend($config->getNode(), $overwrite);
601
+ return $this;
602
+ }
603
+
604
+ }
lib/Varien/Simplexml/Config/Cache/Abstract.php ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Simplexml
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Abstract class for configuration cache
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Simplexml
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ abstract class Varien_Simplexml_Config_Cache_Abstract extends Varien_Object
36
+ {
37
+ /**
38
+ * Constructor
39
+ *
40
+ * Initializes components and allows to save the cache
41
+ *
42
+ * @param array $data
43
+ */
44
+ public function __construct($data=array())
45
+ {
46
+ parent::__construct($data);
47
+
48
+ $this->setComponents(array());
49
+ $this->setIsAllowedToSave(true);
50
+ }
51
+
52
+ /**
53
+ * Add configuration component to stats
54
+ *
55
+ * @param string $component Filename of the configuration component file
56
+ * @return Varien_Simplexml_Config_Cache_Abstract
57
+ */
58
+ public function addComponent($component)
59
+ {
60
+ $comps = $this->getComponents();
61
+ if (is_readable($component)) {
62
+ $comps[$component] = array('mtime'=>filemtime($component));
63
+ }
64
+ $this->setComponents($comps);
65
+
66
+ return $this;
67
+ }
68
+
69
+ /**
70
+ * Validate components in the stats
71
+ *
72
+ * @param array $data
73
+ * @return boolean
74
+ */
75
+ public function validateComponents($data)
76
+ {
77
+ if (empty($data) || !is_array($data)) {
78
+ return false;
79
+ }
80
+ // check that no source files were changed or check file exsists
81
+ foreach ($data as $sourceFile=>$stat) {
82
+ if (empty($stat['mtime']) || !is_file($sourceFile) || filemtime($sourceFile)!==$stat['mtime']) {
83
+ return false;
84
+ }
85
+ }
86
+ return true;
87
+ }
88
+
89
+ public function getComponentsHash()
90
+ {
91
+ $sum = '';
92
+ foreach ($this->getComponents() as $comp) {
93
+ $sum .= $comp['mtime'].':';
94
+ }
95
+ $hash = md5($sum);
96
+ return $hash;
97
+ }
98
+ }
lib/Varien/Simplexml/Config/Cache/File.php ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Simplexml
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * File based cache for configuration
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Simplexml
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Simplexml_Config_Cache_File extends Varien_Simplexml_Config_Cache_Abstract
36
+ {
37
+
38
+ /**
39
+ * Initialize variables that depend on the cache key
40
+ *
41
+ * @param string $key
42
+ * @return string
43
+ */
44
+ public function setKey($key)
45
+ {
46
+ $this->setData('key', $key);
47
+
48
+ $file = $this->getDir().DS.$this->getKey();
49
+ $this->setFileName($file.'.xml');
50
+ $this->setStatFileName($file.'.stat');
51
+
52
+ return $this;
53
+ }
54
+
55
+ /**
56
+ * Try to load configuration cache from file
57
+ *
58
+ * @return boolean
59
+ */
60
+ public function load()
61
+ {
62
+ $this->setIsLoaded(false);
63
+
64
+ // try to read stats
65
+ if (!($stats = @file_get_contents($this->getStatFileName()))) {
66
+ return false;
67
+ }
68
+
69
+ // try to validate stats
70
+ if (!$this->validateComponents(unserialize($stats))) {
71
+ return false;
72
+ }
73
+
74
+ // try to read cache file
75
+ if (!($cache = @file_get_contents($this->getFileName()))) {
76
+ return false;
77
+ }
78
+
79
+ // try to process cache file
80
+ if (!($data = $this->getConfig()->processFileData($cache))) {
81
+ return false;
82
+ }
83
+
84
+ $xml = $this->getConfig()->loadString($data);
85
+ $this->getConfig()->setXml($xml);
86
+ $this->setIsLoaded(true);
87
+
88
+ return true;
89
+ }
90
+
91
+ /**
92
+ * Try to save configuration cache to file
93
+ *
94
+ * @return boolean
95
+ */
96
+ public function save()
97
+ {
98
+ if (!$this->getIsAllowedToSave()) {
99
+ return false;
100
+ }
101
+
102
+ // save stats
103
+ @file_put_contents($this->getStatFileName(), serialize($this->getComponents()));
104
+
105
+ // save cache
106
+ @file_put_contents($this->getFileName(), $this->getConfig()->getNode()->asNiceXml());
107
+
108
+ return true;
109
+ }
110
+ }
lib/Varien/Simplexml/Element.php ADDED
@@ -0,0 +1,631 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Varien
22
+ * @package Varien_Simplexml
23
+ * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
24
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25
+ */
26
+
27
+
28
+ /**
29
+ * Extends SimpleXML to add valuable functionality to SimpleXMLElement class
30
+ *
31
+ * @category Varien
32
+ * @package Varien_Simplexml
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Varien_Simplexml_Element extends SimpleXMLElement
36
+ {
37
+
38
+ /**
39
+ * Would keep reference to parent node
40
+ *
41
+ * If SimpleXMLElement would support complicated attributes
42
+ *
43
+ * @todo make use of spl_object_hash to keep global array of simplexml elements
44
+ * to emulate complicated attributes
45
+ * @var Varien_Simplexml_Element
46
+ */
47
+ protected $_parent = null;
48
+
49
+ /**
50
+ * For future use
51
+ *
52
+ * @param Varien_Simplexml_Element $element
53
+ */
54
+ public function setParent($element)
55
+ {
56
+ #$this->_parent = $element;
57
+ }
58
+
59
+ /**
60
+ * Returns parent node for the element
61
+ *
62
+ * Currently using xpath
63
+ *
64
+ * @return Varien_Simplexml_Element
65
+ */
66
+ public function getParent()
67
+ {
68
+ if (!empty($this->_parent)) {
69
+ $parent = $this->_parent;
70
+ } else {
71
+ $arr = $this->xpath('..');
72
+ $parent = $arr[0];
73
+ }
74
+ return $parent;
75
+ }
76
+
77
+ /**
78
+ * Enter description here...
79
+ *
80
+ * @return boolean
81
+ */
82
+ public function hasChildren()
83
+ {
84
+ if (!$this->children()) {
85
+ return false;
86
+ }
87
+
88
+ // simplexml bug: @attributes is in children() but invisible in foreach
89
+ foreach ($this->children() as $k=>$child) {
90
+ return true;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ /**
96
+ * Returns attribute value by attribute name
97
+ *
98
+ * @return string
99
+ */
100
+ public function getAttribute($name){
101
+ $attrs = $this->attributes();
102
+ return isset($attrs[$name]) ? (string)$attrs[$name] : null;
103
+ }
104
+
105
+ /*
106
+ public function addAttribute($name, $value=null, $namespace=null)
107
+ {
108
+ if (is_null($value)) {
109
+ return parent::addAttribute($name);
110
+ } else {
111
+ if (!is_null($value)) {
112
+ $value = $this->xmlentities($value);
113
+ }
114
+ if (!is_null($namespace)) {
115
+ return parent::addAttribute($name, $value, $namespace);
116
+ } else {
117
+ return parent::addAttribute($name, $value);
118
+ }
119
+ }
120
+ }
121
+
122
+ public function addChild($name, $value=null, $namespace=null)
123
+ {
124
+ if (is_null($value)) {
125
+ return parent::addChild($name);
126
+ } else {
127
+ if (!is_null($value)) {
128
+ $value = $this->xmlentities($value);
129
+ }
130
+ if (!is_null($namespace)) {
131
+ return parent::addChild($name, $value, $namespace);
132
+ } else {
133
+ return parent::addChild($name, $value);
134
+ }
135
+ }
136
+ }
137
+ */
138
+
139
+ /**
140
+ * Find a descendant of a node by path
141
+ *
142
+ * @todo Do we need to make it xpath look-a-like?
143
+ * @todo Check if we still need all this and revert to plain XPath if this makes any sense
144
+ * @todo param string $path Subset of xpath. Example: "child/grand[@attrName='attrValue']/subGrand"
145
+ * @param string $path Example: "child/grand@attrName=attrValue/subGrand" (to make it faster without regex)
146
+ * @return Varien_Simplexml_Element
147
+ */
148
+ public function descend($path)
149
+ {
150
+ # $node = $this->xpath($path);
151
+ # return $node[0];
152
+ if (is_array($path)) {
153
+ $pathArr = $path;
154
+ } else {
155
+ // Simple exploding by / does not suffice,
156
+ // as an attribute value may contain a / inside
157
+ // Note that there are three matches for different kinds of attribute values specification
158
+ if(strpos($path, "@") === false) {
159
+ $pathArr = explode('/', $path);
160
+ }
161
+ else {
162
+ $regex = "#([^@/\\\"]+(?:@[^=/]+=(?:\\\"[^\\\"]*\\\"|[^/]*))?)/?#";
163
+ $pathArr = $pathMatches = array();
164
+ if(preg_match_all($regex, $path, $pathMatches)) {
165
+ $pathArr = $pathMatches[1];
166
+ }
167
+ }
168
+ }
169
+ $desc = $this;
170
+ foreach ($pathArr as $nodeName) {
171
+ if (strpos($nodeName, '@')!==false) {
172
+ $a = explode('@', $nodeName);
173
+ $b = explode('=', $a[1]);
174
+ $nodeName = $a[0];
175
+ $attributeName = $b[0];
176
+ $attributeValue = $b[1];
177
+ //
178
+ // Does a very simplistic trimming of attribute value.
179
+ //
180
+ $attributeValue = trim($attributeValue, '"');
181
+ $found = false;
182
+ foreach ($desc->$nodeName as $subdesc) {
183
+ if ((string)$subdesc[$attributeName]===$attributeValue) {
184
+ $found = true;
185
+ $desc = $subdesc;
186
+ break;
187
+ }
188
+ }
189
+ if (!$found) {
190
+ $desc = false;
191
+ }
192
+ } else {
193
+ $desc = $desc->$nodeName;
194
+ }
195
+ if (!$desc) {
196
+ return false;
197
+ }
198
+ }
199
+ return $desc;
200
+ }
201
+
202
+ /**
203
+ * Returns the node and children as an array
204
+ *
205
+ * @return array|string
206
+ */
207
+ public function asArray()
208
+ {
209
+ return $this->_asArray();
210
+ }
211
+
212
+ /**
213
+ * asArray() analog, but without attributes
214
+ * @return array|string
215
+ */
216
+ public function asCanonicalArray()
217
+ {
218
+ return $this->_asArray(true);
219
+ }
220
+
221
+ /**
222
+ * Returns the node and children as an array
223
+ *
224
+ * @param bool $isCanonical - whether to ignore attributes
225
+ * @return array|string
226
+ */
227
+ protected function _asArray($isCanonical = false)
228
+ {
229
+ $result = array();
230
+ if (!$isCanonical) {
231
+ // add attributes
232
+ foreach ($this->attributes() as $attributeName => $attribute) {
233
+ if ($attribute) {
234
+ $result['@'][$attributeName] = (string)$attribute;
235
+ }
236
+ }
237
+ }
238
+ // add children values
239
+ if ($this->hasChildren()) {
240
+ foreach ($this->children() as $childName => $child) {
241
+ $result[$childName] = $child->_asArray($isCanonical);
242
+ }
243
+ } else {
244
+ if (empty($result)) {
245
+ // return as string, if nothing was found
246
+ $result = (string) $this;
247
+ } else {
248
+ // value has zero key element
249
+ $result[0] = (string) $this;
250
+ }
251
+ }
252
+ return $result;
253
+ }
254
+
255
+ /**
256
+ * Makes nicely formatted XML from the node
257
+ *
258
+ * @param string $filename
259
+ * @param int|boolean $level if false
260
+ * @return string
261
+ */
262
+ public function asNiceXml($filename='', $level=0)
263
+ {
264
+ if (is_numeric($level)) {
265
+ $pad = str_pad('', $level*3, ' ', STR_PAD_LEFT);
266
+ $nl = "\n";
267
+ } else {
268
+ $pad = '';
269
+ $nl = '';
270
+ }
271
+
272
+ $out = $pad.'<'.$this->getName();
273
+
274
+ if ($attributes = $this->attributes()) {
275
+ foreach ($attributes as $key=>$value) {
276
+ $out .= ' '.$key.'="'.str_replace('"', '\"', (string)$value).'"';
277
+ }
278
+ }
279
+
280
+ if ($this->hasChildren()) {
281
+ $out .= '>'.$nl;
282
+ foreach ($this->children() as $child) {
283
+ $out .= $child->asNiceXml('', is_numeric($level) ? $level+1 : true);
284
+ }
285
+ $out .= $pad.'</'.$this->getName().'>'.$nl;
286
+ } else {
287
+ $value = (string)$this;
288
+ if (strlen($value)) {
289
+ $out .= '>'.$this->xmlentities($value).'</'.$this->getName().'>'.$nl;
290
+ } else {
291
+ $out .= '/>'.$nl;
292
+ }
293
+ }
294
+
295
+ if ((0===$level || false===$level) && !empty($filename)) {
296
+ file_put_contents($filename, $out);
297
+ }
298
+
299
+ return $out;
300
+ }
301
+
302
+ /**
303
+ * Enter description here...
304
+ *
305
+ * @param int $level
306
+ * @return string
307
+ */
308
+ public function innerXml($level=0)
309
+ {
310
+ $out = '';
311
+ foreach ($this->children() as $child) {
312
+ $out .= $child->asNiceXml($level);
313
+ }
314
+ return $out;
315
+ }
316
+
317
+ /**
318
+ * Converts meaningful xml characters to xml entities
319
+ *
320
+ * @param string
321
+ * @return string
322
+ */
323
+ public function xmlentities($value = null)
324
+ {
325
+ if (is_null($value)) {
326
+ $value = $this;
327
+ }
328
+ $value = (string)$value;
329
+
330
+ $value = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $value);
331
+
332
+ return $value;
333
+ }
334
+
335
+ /**
336
+ * Appends $source to current node
337
+ *
338
+ * @param Varien_Simplexml_Element $source
339
+ * @return Varien_Simplexml_Element
340
+ */
341
+ public function appendChild($source)
342
+ {
343
+ if ($source->children()) {
344
+ /**
345
+ * @see http://bugs.php.net/bug.php?id=41867 , fixed in 5.2.4
346
+ */
347
+ if (version_compare(phpversion(), '5.2.4', '<')===true) {
348
+ $name = $source->children()->getName();
349
+ }
350
+ else {
351
+ $name = $source->getName();
352
+ }
353
+ $child = $this->addChild($name);
354
+ } else {
355
+ $child = $this->addChild($source->getName(), $this->xmlentities($source));
356
+ }
357
+ $child->setParent($this);
358
+
359
+ $attributes = $source->attributes();
360
+ foreach ($attributes as $key=>$value) {
361
+ $child->addAttribute($key, $this->xmlentities($value));
362
+ }
363
+
364
+ foreach ($source->children() as $sourceChild) {
365
+ $child->appendChild($sourceChild);
366
+ }
367
+ return $this;
368
+ }
369
+
370
+ /**
371
+ * Extends current node with xml from $source
372
+ *
373
+ * If $overwrite is false will merge only missing nodes
374
+ * Otherwise will overwrite existing nodes
375
+ *
376
+ * @param Varien_Simplexml_Element $source
377
+ * @param boolean $overwrite
378
+ * @return Varien_Simplexml_Element
379
+ */
380
+ public function extend($source, $overwrite=false)
381
+ {
382
+ if (!$source instanceof Varien_Simplexml_Element) {
383
+ return $this;
384
+ }
385
+
386
+ foreach ($source->children() as $child) {
387
+ $this->extendChild($child, $overwrite);
388
+ }
389
+
390
+ return $this;
391
+ }
392
+
393
+ /**
394
+ * Extends one node
395
+ *
396
+ * @param Varien_Simplexml_Element $source
397
+ * @param boolean $overwrite
398
+ * @return Varien_Simplexml_Element
399
+ */
400
+ public function extendChild($source, $overwrite=false)
401
+ {
402
+ // this will be our new target node
403
+ $targetChild = null;
404
+
405
+ // name of the source node
406
+ $sourceName = $source->getName();
407
+
408
+ // here we have children of our source node
409
+ $sourceChildren = $source->children();
410
+
411
+ if (!$source->hasChildren()) {
412
+ // handle string node
413
+ if (isset($this->$sourceName)) {
414
+ // if target already has children return without regard
415
+ if ($this->$sourceName->children()) {
416
+ return $this;
417
+ }
418
+ if ($overwrite) {
419
+ unset($this->$sourceName);
420
+ } else {
421
+ return $this;
422
+ }
423
+ }
424
+
425
+ $targetChild = $this->addChild($sourceName, $source->xmlentities());
426
+ $targetChild->setParent($this);
427
+ foreach ($source->attributes() as $key=>$value) {
428
+ $targetChild->addAttribute($key, $this->xmlentities($value));
429
+ }
430
+ return $this;
431
+ }
432
+
433
+ if (isset($this->$sourceName)) {
434
+ $targetChild = $this->$sourceName;
435
+ }
436
+
437
+ if (is_null($targetChild)) {
438
+ // if child target is not found create new and descend
439
+ $targetChild = $this->addChild($sourceName);
440
+ $targetChild->setParent($this);
441
+ foreach ($source->attributes() as $key=>$value) {
442
+ $targetChild->addAttribute($key, $this->xmlentities($value));
443
+ }
444
+ }
445
+
446
+ // finally add our source node children to resulting new target node
447
+ foreach ($sourceChildren as $childKey=>$childNode) {
448
+ $targetChild->extendChild($childNode, $overwrite);
449
+ }
450
+
451
+ return $this;
452
+ }
453
+
454
+ public function setNode($path, $value, $overwrite=true)
455
+ {
456
+ $arr1 = explode('/', $path);
457
+ $arr = array();
458
+ foreach ($arr1 as $v) {
459
+ if (!empty($v)) $arr[] = $v;
460
+ }
461
+ $last = sizeof($arr)-1;
462
+ $node = $this;
463
+ foreach ($arr as $i=>$nodeName) {
464
+ if ($last===$i) {
465
+ /*
466
+ if (isset($xml->$nodeName)) {
467
+ if ($overwrite) {
468
+ unset($xml->$nodeName);
469
+ } else {
470
+ continue;
471
+ }
472
+ }
473
+ $xml->addChild($nodeName, $xml->xmlentities($value));
474
+ */
475
+ if (!isset($node->$nodeName) || $overwrite) {
476
+ // http://bugs.php.net/bug.php?id=36795
477
+ // comment on [8 Feb 8:09pm UTC]
478
+ if (isset($node->$nodeName) && (version_compare(phpversion(), '5.2.6', '<')===true)) {
479
+ $node->$nodeName = $node->xmlentities($value);
480
+ } else {
481
+ $node->$nodeName = $value;
482
+ }
483
+ }
484
+ } else {
485
+ if (!isset($node->$nodeName)) {
486
+ $node = $node->addChild($nodeName);
487
+ } else {
488
+ $node = $node->$nodeName;
489
+ }
490
+ }
491
+
492
+ }
493
+ return $this;
494
+ }
495
+
496
+ /*
497
+ public function extendChildByNode($source, $overwrite=false, $mergeBy='name')
498
+ {
499
+ // this will be our new target node
500
+ $targetChild = null;
501
+
502
+ // name of the source node
503
+ $sourceName = $source->getName();
504
+
505
+ // here we have children of our source node
506
+ $sourceChildren = $source->children();
507
+
508
+ if (!$sourceChildren) {
509
+ // handle string node
510
+ if (isset($this->$sourceName)) {
511
+ if ($overwrite) {
512
+ unset($this->$sourceName);
513
+ } else {
514
+ return $this;
515
+ }
516
+ }
517
+ $targetChild = $this->addChild($sourceName, (string)$source);
518
+ foreach ($source->attributes() as $key=>$value) {
519
+ $targetChild->addAttribute($key, $value);
520
+ }
521
+ return $this;
522
+ }
523
+
524
+ if (isset($this->$sourceName)) {
525
+ // search for target child with same name subnode as node's name
526
+ if (isset($source->$mergeBy)) {
527
+ foreach ($this->$sourceName as $targetNode) {
528
+ if (!isset($targetNode->$mergeBy)) {
529
+ Zend::exception("Can't merge identified node with non identified");
530
+ }
531
+ if ((string)$source->$mergeBy==(string)$targetNode->$mergeBy) {
532
+ $targetChild = $targetNode;
533
+ break;
534
+ }
535
+ }
536
+ } else {
537
+ $existsWithId = false;
538
+ foreach ($this->$sourceName as $targetNode) {
539
+ if (isset($targetNode->$mergeBy)) {
540
+ Zend::exception("Can't merge identified node with non identified");
541
+ }
542
+ }
543
+ $targetChild = $this->$sourceName;
544
+ }
545
+ }
546
+
547
+ if (is_null($targetChild)) {
548
+ // if child target is not found create new and descend
549
+ $targetChild = $this->addChild($sourceName);
550
+ foreach ($source->attributes() as $key=>$value) {
551
+ $targetChild->addAttribute($key, $value);
552
+ }
553
+ }
554
+
555
+ // finally add our source node children to resulting new target node
556
+ foreach ($sourceChildren as $childKey=>$childNode) {
557
+ $targetChild->extendChildByNode($childNode, $overwrite, $mergeBy);
558
+ }
559
+
560
+ return $this;
561
+ }
562
+
563
+ public function extendChildByAttribute($source, $overwrite=false, $mergeBy='name')
564
+ {
565
+ // this will be our new target node
566
+ $targetChild = null;
567
+
568
+ // name of the source node
569
+ $sourceName = $source->getName();
570
+
571
+ // here we have children of our source node
572
+ $sourceChildren = $source->children();
573
+
574
+ if (!$sourceChildren) {
575
+ // handle string node
576
+ if (isset($this->$sourceName)) {
577
+ if ($overwrite) {
578
+ unset($this->$sourceName);
579
+ } else {
580
+ return $this;
581
+ }
582
+ }
583
+ $targetChild = $this->addChild($sourceName, (string)$source);
584
+ foreach ($source->attributes() as $key=>$value) {
585
+ $targetChild->addAttribute($key, $value);
586
+ }
587
+ return $this;
588
+ }
589
+
590
+ if (isset($this->$sourceName)) {
591
+ // search for target child with same name subnode as node's name
592
+ if (isset($source[$mergeBy])) {
593
+ foreach ($this->$sourceName as $targetNode) {
594
+ if (!isset($targetNode[$mergeBy])) {
595
+ Zend::exception("Can't merge identified node with non identified");
596
+ }
597
+ if ((string)$source[$mergeBy]==(string)$targetNode[$mergeBy]) {
598
+ $targetChild = $targetNode;
599
+ break;
600
+ }
601
+ }
602
+ } else {
603
+ $existsWithId = false;
604
+ foreach ($this->$sourceName as $targetNode) {
605
+ if (isset($targetNode[$mergeBy])) {
606
+ Zend::exception("Can't merge identified node with non identified");
607
+ }
608
+ }
609
+ $targetChild = $this->$sourceName;
610
+ }
611
+ }
612
+
613
+ if (is_null($targetChild)) {
614
+ // if child target is not found create new and descend
615
+ $targetChild = $this->addChild($sourceName);
616
+ foreach ($source->attributes() as $key=>$value) {
617
+ $targetChild->addAttribute($key, $value);
618
+ }
619
+ }
620
+
621
+ // finally add our source node children to resulting new target node
622
+ foreach ($sourceChildren as $childKey=>$childNode) {
623
+ $targetChild->extendChildByAttribute($childNode, $overwrite, $mergeBy);
624
+ }
625
+
626
+ return $this;
627
+ }
628
+ */
629
+
630
+
631
+ }
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Lib_Varien</name>
4
+ <version>1.6.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Varien Library</summary>
10
+ <description>Varien Library</description>
11
+ <notes>1.6.0.0</notes>
12
+ <authors><author><name>Magento Core Team</name><user>core</user><email>core@magentocommerce.com</email></author></authors>
13
+ <date>2011-08-18</date>
14
+ <time>08:29:37</time>
15
+ <contents><target name="magelib"><dir name="Varien"><file name="Autoload.php" hash="78ab4b97bbcca3c08de3619dd94550e6"/><dir name="Cache"><dir name="Backend"><file name="Database.php" hash="09535993a910ade50a2cd763da0f6d9e"/><file name="Eaccelerator.php" hash="00bc01db2c4457fdad760835992ae2dc"/></dir><file name="Core.php" hash="018b7952a12633b8ec2a3d856d2b2c41"/></dir><dir name="Convert"><dir name="Action"><file name="Abstract.php" hash="3de0b3f423df89243793e3b474b9cbe8"/><file name="Interface.php" hash="fe9129227525ffc1f862c6f1f85e83f1"/></dir><file name="Action.php" hash="ee3a0baf50c1bb0f1c0b67721f1965e6"/><dir name="Adapter"><file name="Abstract.php" hash="0c53dc267b75e595518365ab8f1d8ab4"/><dir name="Db"><file name="Table.php" hash="d6b8a1bfe2de4ed908ca0367fb226d2c"/></dir><dir name="Http"><file name="Curl.php" hash="938f2e176d58f61e078329949d23a92e"/></dir><file name="Http.php" hash="afb44986f4d6ba1cf84819bbb31004b2"/><file name="Interface.php" hash="2c4834f37eede972be48c8b3a2857661"/><file name="Io.php" hash="a9ceb53b810de3e183b439ba1bb1ba57"/><file name="Soap.php" hash="4b67a9ea45b12dfffabd26d2cb8942f2"/><file name="Std.php" hash="225b4e11d9ffcd1db8f99d54dc749687"/><dir name="Zend"><file name="Cache.php" hash="845fcb65276b05714b8d231c6b30f651"/><file name="Db.php" hash="794cb6226590ff7d2a7d1fbce26fcc05"/></dir></dir><dir name="Container"><file name="Abstract.php" hash="8f836665d0dec3c7cc7d93714e9c99b4"/><file name="Collection.php" hash="b1ef8a70ea806deb351b5413d6cfacef"/><file name="Generic.php" hash="bef8559c7a10ecf3ebb3f147795a2597"/><file name="Interface.php" hash="870470de0ec947f1039aa415d37d0f82"/></dir><file name="Exception.php" hash="58c9437dd946d5251234f55de3648d3f"/><dir name="Mapper"><file name="Abstract.php" hash="e1d9d87a1270ad6a499549dd87f9ac62"/><file name="Column.php" hash="7f74158a73fd8fa83052625ef07b1749"/><file name="Interface.php" hash="d8a8b98cece9ae38302914bbc910c094"/></dir><dir name="Parser"><file name="Abstract.php" hash="3559203bdcdb019daddd9ed558f596a7"/><file name="Csv.php" hash="f68d8dc8c0695296cc32b1ca716a8276"/><file name="Interface.php" hash="016ab96988f7ae02d2e84ed4878513ca"/><file name="Serialize.php" hash="52a93cacd5f3d3e0267bbffc74d7d715"/><dir name="Xml"><file name="Excel.php" hash="a8b60288a1aa41de84394b5c040c4e4d"/></dir></dir><dir name="Profile"><file name="Abstract.php" hash="c038c0b7a209c7a189beb6aa21df617b"/><file name="Collection.php" hash="2ff1f76cafa7e36826063069efdba8ad"/></dir><file name="Profile.php" hash="9b63f8909ed29bf5a3c729bc5934cb9c"/><dir name="Validator"><file name="Abstract.php" hash="3fdf8a0fcd2366563e330c30456f19b9"/><file name="Column.php" hash="da9bb1ffa8d6026a6d26365da4cec289"/><file name="Dryrun.php" hash="70f63aa2f6146c67658dc7bfd04ddf02"/><file name="Interface.php" hash="59c7a5d323e0a3bb6928754f3f4393ad"/></dir></dir><file name="Convert.php" hash="441a61c66ee524a024c7ffe8b7c7c3c2"/><dir name="Crypt"><file name="Abstract.php" hash="73980e75bef5f58855ea924c3693522d"/><file name="Mcrypt.php" hash="3c62cbb2f8e763e0da985b7bc054c5c3"/></dir><file name="Crypt.php" hash="cb28cabbc8a02fd86705bc0b52d7204b"/><dir name="Data"><dir name="Collection"><file name="Db.php" hash="7783ac1ea190203c1aab7ac71711e289"/><file name="Filesystem.php" hash="4574312fc65b08180fdcdba4d0358940"/></dir><file name="Collection.php" hash="22e12b16701101cce3aff641987ba563"/><dir name="Form"><file name="Abstract.php" hash="59476179174aeba63350312b28b4322f"/><dir name="Element"><file name="Abstract.php" hash="42d3ce5e06903c234969112e04f1b011"/><file name="Button.php" hash="98c64b5da29dff22cfae938caa5833c8"/><file name="Checkbox.php" hash="7992114fdc4aefafceebac2a08c1a434"/><file name="Checkboxes.php" hash="fdc32833161b60139e9859f387bce6c7"/><file name="Collection.php" hash="91c768b801f9eeebdd9121194e6fad24"/><file name="Column.php" hash="b6d1d32aca981ae0fd1c6a83fc655c9a"/><file name="Date.php" hash="3f5c8628e329cbc87c33c7e622a3b946"/><file name="Editor.php" hash="342d54be12c89037d5c74b5c81b78843"/><file name="Fieldset.php" hash="f78031a222ad4263826adf2f35622c72"/><file name="File.php" hash="463e8c8e106fe2018fdbf117e330423e"/><file name="Gallery.php" hash="6cb83ebc8efae252978fbd22c001642b"/><file name="Hidden.php" hash="ae6275e5d3b7df411bfda4fb4f995a8c"/><file name="Image.php" hash="9cd7231a0e14dc569045cf5fbf0e5b7f"/><file name="Imagefile.php" hash="2d6ed40fef1c2927ab22dea7d29336db"/><file name="Label.php" hash="6759a3286fb2b126e998934af8d6ce17"/><file name="Link.php" hash="fb213bbbce4e46ea03a96923defbfbb3"/><file name="Multiline.php" hash="087bd60ff98b4b9c094b9adec040f166"/><file name="Multiselect.php" hash="9e0d04674a743602438fd4190040944c"/><file name="Note.php" hash="f5b71ebcf5a2b774393d9f4d1581dc35"/><file name="Obscure.php" hash="b61a077fbdea1b93be0aafc2f2c2f391"/><file name="Password.php" hash="a15263e7b0fabe8bd6abf8487f43efcb"/><file name="Radio.php" hash="4b32afab119091759d93ec99fb2fd53b"/><file name="Radios.php" hash="9004386a01f6c2b09435f39bf9b5bb36"/><dir name="Renderer"><file name="Interface.php" hash="61baa8c8755a9a7e8eaac5786ae38f42"/></dir><file name="Reset.php" hash="51bc11836e12bd64390476e3ff393c13"/><file name="Select.php" hash="4402f264e6e2cae5fbad26bc44aa21fe"/><file name="Submit.php" hash="5b9e9e3db9bc70757030312b7d002b7e"/><file name="Text.php" hash="2e798cfd096ac0a87f9d9055000af3c3"/><file name="Textarea.php" hash="b88d00399b8f6846d56055c9416dd0bb"/><file name="Time.php" hash="f35d8d1dc0a90dd6f044f17c22d8dc4a"/></dir><dir name="Filter"><file name="Date.php" hash="80eee9775448a21a5332f3e5cb7edcc9"/><file name="Escapehtml.php" hash="0c02fdfbae30158aa14f45b937332249"/><file name="Interface.php" hash="4542e15af14cc92d7bf5a863d4bf5ddd"/><file name="Striptags.php" hash="2c4cd0905e4e3b99ba373ffeb9ab7d38"/></dir></dir><file name="Form.php" hash="407eee6f100c7befa18ca586b3c3ae04"/><dir name="Tree"><file name="Db.php" hash="d06e9a49ddb1ea94892f4f10e1b3fea1"/><file name="Dbp.php" hash="4bd7f5b9047870f125459dbf523f73a2"/><dir name="Node"><file name="Collection.php" hash="941c88db98d69ec170674380fe3413d5"/></dir><file name="Node.php" hash="e985629bbea05cd0e52d7ac82fc2ee27"/></dir><file name="Tree.php" hash="e39d73757e0535493badc5bea8459834"/></dir><file name="Date.php" hash="27ce5543f5a6ffaba55ec023c88db4d8"/><dir name="Db"><dir name="Adapter"><file name="Interface.php" hash="1c1652ccba2bd7a8afbe0725c895247e"/><file name="Mysqli.php" hash="4e6e03a6ddf127f58ab0326c4cfe9956"/><dir name="Pdo"><file name="Mysql.php" hash="7ca13eb359d054c0f09c00a207e8e8a7"/></dir></dir><dir name="Ddl"><file name="Table.php" hash="5a9390d89162ea56f73a0817de4d029f"/></dir><file name="Exception.php" hash="93966b32e96f793f031244716ff37eed"/><file name="Helper.php" hash="eee52d2574360dabe7a5e830bc0a67b8"/><file name="Select.php" hash="5a5d369789c672060f68bcdbd2fa6a15"/><dir name="Statement"><file name="Parameter.php" hash="8d688f64d6780d18e517921ea5eb9ccb"/><dir name="Pdo"><file name="Mysql.php" hash="e82abad90774a126efb8bd27bc0f43f7"/></dir></dir><dir name="Tree"><file name="Exception.php" hash="6a00d12bbed94d648a974d213a816a4b"/><dir name="Node"><file name="Exception.php" hash="f890d0fc36e89aefd7cf04ae6c10a88e"/></dir><file name="Node.php" hash="6b048bbe2b2c46e405ffae8bca23e579"/><dir name="NodeSet"><file name="Exception.php" hash="2caa6a4b09846fe3b941563aa55d3c5f"/></dir><file name="NodeSet.php" hash="5e589b0c1221caf2693fe6f7d07e1ad3"/></dir><file name="Tree.php" hash="c7cc99ac052aa5d1ac721619d47506e6"/></dir><file name="Debug.php" hash="ec721f169e5a16524a17abba094d651d"/><dir name="Directory"><file name="Collection.php" hash="30b6c2b8bb8066d1636ff0d477ffc7fc"/><file name="Factory.php" hash="1ef74a40cd121892c730722ed0e3353a"/><file name="IFactory.php" hash="5ba24f161727511bbce35e588c55fe4d"/><file name="a.txt" hash="026e450c46ac92ea375302955a4f42fb"/></dir><dir name="Event"><file name="Collection.php" hash="cb422c6c7abb160fa3d7d54669bf2b2e"/><dir name="Observer"><file name="Collection.php" hash="b9c7d52c9563a19d15723bbc4a85fac6"/><file name="Cron.php" hash="0e04e63cf24f5cce3031f5896c439b68"/><file name="Regex.php" hash="f97f5a4435d560e073d725681cb15264"/></dir><file name="Observer.php" hash="eff7b66ce01d3f8fc4c00d974a2d5440"/></dir><file name="Event.php" hash="1836bb96f191e616d59178f17e407277"/><file name="Exception.php" hash="6beb1561720cc6c7a894a955ff2251b7"/><dir name="File"><file name="Csv.php" hash="5736e17051a3e778c59b99ed669caee5"/><file name="CsvMulty.php" hash="af4518063d4d9f03cfbfca832c1b9d76"/><file name="Object.php" hash="9ffd59210edf58d402dd961d62f9819a"/><dir name="Transfer"><dir name="Adapter"><file name="Http.php" hash="c71dc1460cdcf062adb97b577e621923"/></dir></dir><dir name="Uploader"><file name="Image.php" hash="2655d6497c074c8c4998c58320d60c14"/></dir><file name="Uploader.php" hash="4b8de17ad226bac218447aafa40b5caf"/></dir><dir name="Filter"><dir name="Array"><file name="Grid.php" hash="14bd2f5ab60e7255557fc2dff7f24a37"/></dir><file name="Array.php" hash="a38e3d26f47d1bfaf4e023662d9a3dad"/><file name="Email.php" hash="067f3ea6f1597ea2501455865977d789"/><file name="Money.php" hash="8bec682670cb4cbf48e4b4264e2ab188"/><dir name="Object"><file name="Grid.php" hash="e3c3fa92761e5a1e1401192d8362301e"/></dir><file name="Object.php" hash="5b85aaff354eb0770e14efcfebda263b"/><file name="Sprintf.php" hash="0dd85ddc3d4b737d7cc6308afc12db18"/><dir name="Template"><file name="Simple.php" hash="b342cd136c10a6630d2d7dccd4137387"/><dir name="Tokenizer"><file name="Abstract.php" hash="d737a3d6599939b2a12b55975c9af780"/><file name="Parameter.php" hash="140ab2548a3da859473a46c7b0d44504"/><file name="Variable.php" hash="4ba64c31b234db22c2ff1799a49e7798"/></dir></dir><file name="Template.php" hash="59612d4851769548308449114b6c122a"/></dir><dir name="Http"><dir name="Adapter"><file name="Curl.php" hash="4b74b0a2c86732d32a653794a3afc52f"/></dir><file name="Client.php" hash="eac22d5beaca06c1678c327a13e7f45d"/></dir><dir name="Image"><dir name="Adapter"><file name="Abstract.php" hash="0ac17ab0c82dea6289caf0985be0c47d"/><file name="Gd2.php" hash="c2f287351143e69cf7c66824588f1dd4"/></dir><file name="Adapter.php" hash="edbab0bcfa931ae1d8f813f467e6b311"/></dir><file name="Image.php" hash="dfec3144e465eb2558a76a84f4736254"/><dir name="Io"><file name="Abstract.php" hash="53e7055dfe0ca7d133b94cfbf3621693"/><file name="Exception.php" hash="493365429caf5d2170c00137b081b195"/><file name="File.php" hash="4d0613516c6d3824d1ec4442c99a6b8a"/><file name="Ftp.php" hash="f349b9b833133853c902abb493d4f3bd"/><file name="Interface.php" hash="0b26ea223a4bed00df9b7b808b2acf80"/><file name="Sftp.php" hash="461d458eb368ed01f3b9194bafc4030d"/></dir><dir name="Object"><file name="Cache.php" hash="a87d2ae2d015a4e2f2fc1b611feccfcd"/><file name="Mapper.php" hash="1180b9b29c366d886e8af05181aebbce"/></dir><file name="Object.php" hash="0d5d06e7eb4439372bf90e2e41e998d7"/><dir name="Pear"><file name="Frontend.php" hash="d3d737fc569c67b177ccd0b6dcb5f22f"/><file name="Package.php" hash="8468373495be75543f7c853f94177d0b"/><file name="Registry.php" hash="5c38bf2cdb4108267acdcb23bb3dd93d"/></dir><file name="Pear.php" hash="7d068de3a3989c8575521c143261fe40"/><file name="Profiler.php" hash="d434c08a6dd3ef8eb168e3a3d46f057f"/><dir name="Simplexml"><dir name="Config"><dir name="Cache"><file name="Abstract.php" hash="6dbbd6f1bc63c1b7ee086399da30f4e0"/><file name="File.php" hash="f99292ec6d04d5464a1313a2c52880d4"/></dir></dir><file name="Config.php" hash="a957b7feccdf430239af81610a6ac281"/><file name="Element.php" hash="f8321180f2b343080ffd89591943b239"/></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php><package><name>Lib_ZF</name><channel>community</channel><min>1.11.1.0</min><max>1.12.0.0</max></package><extension><name>PDO</name><min></min><max></max></extension><extension><name>SPL</name><min></min><max></max></extension><extension><name>curl</name><min></min><max></max></extension><extension><name>SimpleXML</name><min></min><max></max></extension><extension><name>dom</name><min></min><max></max></extension><extension><name>gd</name><min></min><max></max></extension><extension><name>iconv</name><min></min><max></max></extension><extension><name>pdo_mysql</name><min></min><max></max></extension><extension><name>mcrypt</name><min></min><max></max></extension><extension><name>pcre</name><min></min><max></max></extension><extension><name>Reflection</name><min></min><max></max></extension><extension><name>session</name><min></min><max></max></extension></required></dependencies>
18
+ </package>