/protected/libs/phpexcel/Classes/PHPExcel/Calculation/Database.php

https://github.com/allinside/Yii-CMS · PHP · 384 lines · 208 code · 51 blank · 125 comment · 22 complexity · 8c91dc1141c969befc14b6eaaf7485bf MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Calculation
  23. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Calculation_Database
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Calculation
  40. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Calculation_Database {
  43. private static function __fieldExtract($database,$field) {
  44. $field = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($field));
  45. $fieldNames = array_map('strtoupper',array_shift($database));
  46. if (is_numeric($field)) {
  47. $keys = array_keys($fieldNames);
  48. return $keys[$field-1];
  49. }
  50. $key = array_search($field,$fieldNames);
  51. return ($key) ? $key : null;
  52. }
  53. private static function __filter($database,$criteria) {
  54. $fieldNames = array_shift($database);
  55. $criteriaNames = array_shift($criteria);
  56. // Convert the criteria into a set of AND/OR conditions with [:placeholders]
  57. $testConditions = $testValues = array();
  58. $testConditionsCount = 0;
  59. foreach($criteriaNames as $key => $criteriaName) {
  60. $testCondition = array();
  61. $testConditionCount = 0;
  62. foreach($criteria as $row => $criterion) {
  63. if ($criterion[$key] > '') {
  64. $testCondition[] = '[:'.$criteriaName.']'.PHPExcel_Calculation_Functions::_ifCondition($criterion[$key]);
  65. $testConditionCount++;
  66. }
  67. }
  68. if ($testConditionCount > 1) {
  69. $testConditions[] = 'OR('.implode(',',$testCondition).')';
  70. $testConditionsCount++;
  71. } elseif($testConditionCount == 1) {
  72. $testConditions[] = $testCondition[0];
  73. $testConditionsCount++;
  74. }
  75. }
  76. if ($testConditionsCount > 1) {
  77. $testConditionSet = 'AND('.implode(',',$testConditions).')';
  78. } elseif($testConditionsCount == 1) {
  79. $testConditionSet = $testConditions[0];
  80. }
  81. // Loop through each row of the database
  82. foreach($database as $dataRow => $dataValues) {
  83. // Substitute actual values from the database row for our [:placeholders]
  84. $testConditionList = $testConditionSet;
  85. foreach($criteriaNames as $key => $criteriaName) {
  86. $k = array_search($criteriaName,$fieldNames);
  87. if (isset($dataValues[$k])) {
  88. $dataValue = $dataValues[$k];
  89. $dataValue = (is_string($dataValue)) ? PHPExcel_Calculation::_wrapResult(strtoupper($dataValue)) : $dataValue;
  90. $testConditionList = str_replace('[:'.$criteriaName.']',$dataValue,$testConditionList);
  91. }
  92. }
  93. // evaluate the criteria against the row data
  94. $result = PHPExcel_Calculation::getInstance()->_calculateFormulaValue('='.$testConditionList);
  95. // If the row failed to meet the criteria, remove it from the database
  96. if (!$result) {
  97. unset($database[$dataRow]);
  98. }
  99. }
  100. return $database;
  101. }
  102. /**
  103. * DAVERAGE
  104. *
  105. */
  106. public static function DAVERAGE($database,$field,$criteria) {
  107. $field = self::__fieldExtract($database,$field);
  108. if (is_null($field)) {
  109. return NULL;
  110. }
  111. // reduce the database to a set of rows that match all the criteria
  112. $database = self::__filter($database,$criteria);
  113. // extract an array of values for the requested column
  114. $colData = array();
  115. foreach($database as $row) {
  116. $colData[] = $row[$field];
  117. }
  118. // Return
  119. return PHPExcel_Calculation_Statistical::AVERAGE($colData);
  120. } // function DAVERAGE()
  121. /**
  122. * DCOUNT
  123. *
  124. */
  125. public static function DCOUNT($database,$field,$criteria) {
  126. $field = self::__fieldExtract($database,$field);
  127. if (is_null($field)) {
  128. return NULL;
  129. }
  130. // reduce the database to a set of rows that match all the criteria
  131. $database = self::__filter($database,$criteria);
  132. // extract an array of values for the requested column
  133. $colData = array();
  134. foreach($database as $row) {
  135. $colData[] = $row[$field];
  136. }
  137. // Return
  138. return PHPExcel_Calculation_Statistical::COUNT($colData);
  139. } // function DCOUNT()
  140. /**
  141. * DCOUNTA
  142. *
  143. */
  144. public static function DCOUNTA($database,$field,$criteria) {
  145. $field = self::__fieldExtract($database,$field);
  146. if (is_null($field)) {
  147. return NULL;
  148. }
  149. // reduce the database to a set of rows that match all the criteria
  150. $database = self::__filter($database,$criteria);
  151. // extract an array of values for the requested column
  152. $colData = array();
  153. foreach($database as $row) {
  154. $colData[] = $row[$field];
  155. }
  156. // Return
  157. return PHPExcel_Calculation_Statistical::COUNTA($colData);
  158. } // function DCOUNTA()
  159. /**
  160. * DGET
  161. *
  162. */
  163. public static function DGET($database,$field,$criteria) {
  164. $field = self::__fieldExtract($database,$field);
  165. if (is_null($field)) {
  166. return NULL;
  167. }
  168. // reduce the database to a set of rows that match all the criteria
  169. $database = self::__filter($database,$criteria);
  170. // extract an array of values for the requested column
  171. $colData = array();
  172. foreach($database as $row) {
  173. $colData[] = $row[$field];
  174. }
  175. // Return
  176. if (count($colData) > 1) {
  177. return PHPExcel_Calculation_Functions::NaN();
  178. }
  179. return $colData[0];
  180. } // function DGET()
  181. /**
  182. * DMAX
  183. *
  184. */
  185. public static function DMAX($database,$field,$criteria) {
  186. $field = self::__fieldExtract($database,$field);
  187. if (is_null($field)) {
  188. return NULL;
  189. }
  190. // reduce the database to a set of rows that match all the criteria
  191. $database = self::__filter($database,$criteria);
  192. // extract an array of values for the requested column
  193. $colData = array();
  194. foreach($database as $row) {
  195. $colData[] = $row[$field];
  196. }
  197. // Return
  198. return PHPExcel_Calculation_Statistical::MAX($colData);
  199. } // function DMAX()
  200. /**
  201. * DMIN
  202. *
  203. */
  204. public static function DMIN($database,$field,$criteria) {
  205. $field = self::__fieldExtract($database,$field);
  206. if (is_null($field)) {
  207. return NULL;
  208. }
  209. // reduce the database to a set of rows that match all the criteria
  210. $database = self::__filter($database,$criteria);
  211. // extract an array of values for the requested column
  212. $colData = array();
  213. foreach($database as $row) {
  214. $colData[] = $row[$field];
  215. }
  216. // Return
  217. return PHPExcel_Calculation_Statistical::MIN($colData);
  218. } // function DMIN()
  219. /**
  220. * DPRODUCT
  221. *
  222. */
  223. public static function DPRODUCT($database,$field,$criteria) {
  224. $field = self::__fieldExtract($database,$field);
  225. if (is_null($field)) {
  226. return NULL;
  227. }
  228. // reduce the database to a set of rows that match all the criteria
  229. $database = self::__filter($database,$criteria);
  230. // extract an array of values for the requested column
  231. $colData = array();
  232. foreach($database as $row) {
  233. $colData[] = $row[$field];
  234. }
  235. // Return
  236. return PHPExcel_Calculation_MathTrig::PRODUCT($colData);
  237. } // function DPRODUCT()
  238. /**
  239. * DSTDEV
  240. *
  241. */
  242. public static function DSTDEV($database,$field,$criteria) {
  243. $field = self::__fieldExtract($database,$field);
  244. if (is_null($field)) {
  245. return NULL;
  246. }
  247. // reduce the database to a set of rows that match all the criteria
  248. $database = self::__filter($database,$criteria);
  249. // extract an array of values for the requested column
  250. $colData = array();
  251. foreach($database as $row) {
  252. $colData[] = $row[$field];
  253. }
  254. // Return
  255. return PHPExcel_Calculation_Statistical::STDEV($colData);
  256. } // function DSTDEV()
  257. /**
  258. * DSTDEVP
  259. *
  260. */
  261. public static function DSTDEVP($database,$field,$criteria) {
  262. $field = self::__fieldExtract($database,$field);
  263. if (is_null($field)) {
  264. return NULL;
  265. }
  266. // reduce the database to a set of rows that match all the criteria
  267. $database = self::__filter($database,$criteria);
  268. // extract an array of values for the requested column
  269. $colData = array();
  270. foreach($database as $row) {
  271. $colData[] = $row[$field];
  272. }
  273. // Return
  274. return PHPExcel_Calculation_Statistical::STDEVP($colData);
  275. } // function DSTDEVP()
  276. /**
  277. * DSUM
  278. *
  279. */
  280. public static function DSUM($database,$field,$criteria) {
  281. $field = self::__fieldExtract($database,$field);
  282. if (is_null($field)) {
  283. return NULL;
  284. }
  285. // reduce the database to a set of rows that match all the criteria
  286. $database = self::__filter($database,$criteria);
  287. // extract an array of values for the requested column
  288. $colData = array();
  289. foreach($database as $row) {
  290. $colData[] = $row[$field];
  291. }
  292. // Return
  293. return PHPExcel_Calculation_MathTrig::SUM($colData);
  294. } // function DSUM()
  295. /**
  296. * DVAR
  297. *
  298. */
  299. public static function DVAR($database,$field,$criteria) {
  300. $field = self::__fieldExtract($database,$field);
  301. if (is_null($field)) {
  302. return NULL;
  303. }
  304. // reduce the database to a set of rows that match all the criteria
  305. $database = self::__filter($database,$criteria);
  306. // extract an array of values for the requested column
  307. $colData = array();
  308. foreach($database as $row) {
  309. $colData[] = $row[$field];
  310. }
  311. // Return
  312. return PHPExcel_Calculation_Statistical::VARFunc($colData);
  313. } // function DVAR()
  314. /**
  315. * DVARP
  316. *
  317. */
  318. public static function DVARP($database,$field,$criteria) {
  319. $field = self::__fieldExtract($database,$field);
  320. if (is_null($field)) {
  321. return NULL;
  322. }
  323. // reduce the database to a set of rows that match all the criteria
  324. $database = self::__filter($database,$criteria);
  325. // extract an array of values for the requested column
  326. $colData = array();
  327. foreach($database as $row) {
  328. $colData[] = $row[$field];
  329. }
  330. // Return
  331. return PHPExcel_Calculation_Statistical::VARP($colData);
  332. } // function DVARP()
  333. } // class PHPExcel_Calculation_Database