AbstractMongo.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * Questa classe fa da interfaccia verso MongoDb
  4. *
  5. * @author Paolo Libertini <paolo.libertini@thinkopen.it>
  6. * @method getCollection()
  7. * @method sendAsArray()
  8. * @method getCollectionData()
  9. * @method loadByAttribute()
  10. * @method load()
  11. * @method addFieldToFilter($_attributeName, $_value)
  12. * @method addFieldToSelect($_attributeName)
  13. */
  14. class Mooses_AbstractMongo extends Mooses_Mongodb_Mongo_Document {
  15. const ASC = "ASC";
  16. protected static $_db;
  17. protected static $_collection;
  18. protected static $_instance;
  19. protected $calledClass;
  20. protected $_classMethods = array();
  21. protected static $_order = array();
  22. protected $_queryConditions = array();
  23. protected $_fields = array();
  24. protected $_sorterArray = array();
  25. protected $_sorterAttribute = "";
  26. protected $_sorterDate = false;
  27. protected $_sorterOrder = self::ASC;
  28. protected $_forceNoCachePaginatedCollection = false;
  29. public $loadKey = array();
  30. protected $_dataUpdate = array();
  31. protected $_sendAsArray = false;
  32. public function __construct($_data = []) {
  33. $this->_setCollectionAndDatabase();
  34. parent::__construct($_data, array());
  35. $this->calledClass = get_called_class();
  36. self::$_instance = $this;
  37. return $this;
  38. }
  39. private function _setCollectionAndDatabase(){
  40. $_mongoInformations = $this->_retrieveTableName();
  41. self::$_db = $_mongoInformations['db'];
  42. static::$_db = $_mongoInformations['db'];
  43. self::$_collection = $_mongoInformations['collection'];
  44. static::$_collection = $_mongoInformations['collection'];
  45. $this->_classMethods = get_class_methods($this);
  46. }
  47. public static function getInstance() {
  48. if (self::$_instance === NULL || self::$_instance->calledClass !== get_called_class()) {
  49. $_className = get_called_class();
  50. self::$_instance = new $_className();
  51. }
  52. return self::$_instance;
  53. }
  54. protected static function ___retrieveTableName(){
  55. $_return = array();
  56. $annotations = new ReflectionClass(get_called_class());
  57. $_tableSuffix = NULL;
  58. $_tablePrefix = "re_";
  59. $_databaseAnnotated = self::_parseDocComment($annotations->getDocComment(), "@Db");
  60. $_collectionAnnotated = self::_parseDocComment($annotations->getDocComment(), "@Collection");
  61. $_return['collection'] = (stristr($_collectionAnnotated, $_tableSuffix)) ? $_collectionAnnotated : $_tablePrefix . $_collectionAnnotated . $_tableSuffix;
  62. $_return['db'] = $_databaseAnnotated;
  63. return $_return;
  64. }
  65. protected function _retrieveTableName() {
  66. return self::___retrieveTableName();
  67. }
  68. private static function _parseDocComment($str, $tag = ''){
  69. if (empty($tag)) {
  70. return $str;
  71. }
  72. $matches = array();
  73. preg_match("/" . $tag . ":(.*)(\\r\\n|\\r|\\n)/U", $str, $matches);
  74. if (isset($matches[1])) {
  75. return trim($matches[1]);
  76. }
  77. return '';
  78. }
  79. protected function ___sendAsArray(){
  80. $this->_sendAsArray = true;
  81. return $this;
  82. }
  83. protected function _convertMongoCursor($_mongoCursor, $_forceArray = false, $_order = false){
  84. $_calledClass = $_mongoCursor->getDocumentClass();
  85. if($_mongoCursor->count() == 0){
  86. return (($_forceArray) ? array() : false);
  87. } elseif($_mongoCursor->count() == 1){
  88. $_object = new $_calledClass($_mongoCursor->next());
  89. $_object->loadKey = $this->loadKey;
  90. $_object = (($_forceArray) ? array($_object) : $_object);
  91. return $_object;
  92. } else {
  93. $_arrayResults = array();
  94. while($_result = $_mongoCursor->next()){
  95. $_object = new $_calledClass($_result);
  96. $_object->loadKey = $this->_loadKey;
  97. array_push($_arrayResults, (($this->_sendAsArray && $_order === false) ? $_object->getData() : $_object));
  98. }
  99. if($_order !== false && is_array($_order)){
  100. $this->_sorterAttribute = $_order[0];
  101. $this->_sorterOrder = $_order[1];
  102. $this->_sorterDate = (($_order[2] === true) ? true : false);
  103. $_orderer = function($a, $b){
  104. if($this->_sorterDate === false) {
  105. if (strcasecmp((string)$a->getData($this->_sorterAttribute), (string)$b->getData($this->_sorterAttribute)) > 0) {
  106. return (($this->_sorterOrder == self::ASC) ? 1 : -1);
  107. } else {
  108. return (($this->_sorterOrder == self::ASC) ? -1 : 1);
  109. }
  110. } else {
  111. if(is_a($a, "Default_Model_Mapper_Utenti")) {
  112. $_datiA = $a->getAllData();
  113. } else {
  114. $_datiA = $a->getData();
  115. }
  116. if(is_array($_datiA[$this->_sorterAttribute]) && isset($_datiA[$this->_sorterAttribute]['date'])) {
  117. $_dataA = DateTime::createFromFormat("Y-m-d H:i:s", substr($_datiA[$this->_sorterAttribute]['date'], 0, 19));
  118. } else {
  119. $_dataA = DateTime::createFromFormat("Y-m-d H:i:s", substr($_datiA[$this->_sorterAttribute], 0, 19));
  120. }
  121. if(is_a($b, "Default_Model_Mapper_Utenti")) {
  122. $_datiB = $b->getAllData();
  123. } else {
  124. $_datiB = $b->getData();
  125. }
  126. if(is_array($_datiB[$this->_sorterAttribute]) && isset($_datiB[$this->_sorterAttribute]['date'])) {
  127. $_dataB = DateTime::createFromFormat("Y-m-d H:i:s", substr($_datiB[$this->_sorterAttribute]['date'], 0, 19));
  128. } else {
  129. $_dataB = DateTime::createFromFormat("Y-m-d H:i:s", substr($_datiB[$this->_sorterAttribute], 0, 19));
  130. }
  131. if($_dataA > $_dataB){
  132. return (($this->_sorterOrder == self::ASC) ? 1 : -1);
  133. } else {
  134. return (($this->_sorterOrder == self::ASC) ? -1 : 1);
  135. }
  136. }
  137. };
  138. usort($_arrayResults, $_orderer);
  139. }
  140. return $_arrayResults;
  141. }
  142. }
  143. public static function __callStatic($name, $arguments)
  144. {
  145. if(get_called_class() != get_class(self::$_instance)){
  146. $_mongoInformations = self::_retrieveTableName();
  147. self::$_db = $_mongoInformations['db'];
  148. static::$_db = $_mongoInformations['db'];
  149. self::$_collection = $_mongoInformations['collection'];
  150. static::$_collection = $_mongoInformations['collection'];
  151. }
  152. }
  153. public function __call($methodName, $params = null) {
  154. if(get_called_class() != get_class(self::$_instance)){
  155. $this->_setCollectionAndDatabase();
  156. }
  157. $_methodsAllowed = array("setData", "setProperty", "getData", "getProperty");
  158. $methodPrefix = substr($methodName, 0, 3);
  159. $key = strtolower(substr($methodName, 3));
  160. $_isUppercase = ctype_upper(substr($methodName, 3, 1));
  161. if (in_array("___".$methodName, $_methodsAllowed) || in_array("___".$methodName, $this->_classMethods)) {
  162. return call_user_func_array(array($this, "___".$methodName), $params);
  163. } elseif ($methodPrefix == 'set' && count($params) == 1 && $_isUppercase) {
  164. $value = ((is_string($params[0])) ? htmlspecialchars($params[0],ENT_QUOTES,"UTF-8") : $params[0]);
  165. return parent::setProperty($key, $value);
  166. } elseif ($methodPrefix == 'get') {
  167. return htmlspecialchars(parent::getProperty($key),ENT_QUOTES);
  168. } elseif (!in_array($methodName, array_flip(get_class_methods($this)))) {
  169. throw new Exception("Method \"" . $methodName . "\" doesn't exist in " . get_called_class(), 500);
  170. }
  171. }
  172. public static function addMongoRegexp(&$_value){
  173. if(!is_array($_value) && stristr($_value, "/") != FALSE){
  174. $_value = new MongoRegex($_value);
  175. }
  176. }
  177. protected function ___getCollection(){
  178. $this->_queryConditions = array();
  179. return $this;
  180. }
  181. protected function ___addFieldToFilter($_attributeName, $_value){
  182. if(!is_array($_value) && stristr($_value, "/") != FALSE){
  183. $_value = new MongoRegex($_value);
  184. }
  185. if(is_array($_value)){
  186. array_walk_recursive($_value, array(get_called_class(), "addMongoRegexp"));
  187. $_condition = array();
  188. foreach ($_value as $_operators => $_realValue) {
  189. $_condition[$_operators] = $_realValue;
  190. }
  191. } else {
  192. $_condition = $_value;
  193. }
  194. if(isset($this->_queryConditions[$_attributeName])){
  195. $_alreadyExistingConditions = $this->_queryConditions[$_attributeName];
  196. $_condition = array_merge($_alreadyExistingConditions, $_condition);
  197. }
  198. $this->_queryConditions[$_attributeName] = $_condition;
  199. return $this;
  200. }
  201. protected function ___setFieldToSelect($_fields = array()){
  202. $_truesFiller = function(&$_value){$_value = true;};
  203. $_keysFields = array_flip($_fields);
  204. array_walk_recursive($_keysFields, $_truesFiller);
  205. $this->_fields = $_keysFields;
  206. return $this;
  207. }
  208. protected function ___getCollectionData($_forceArray = true){
  209. $_result = $this->fetchAll($this->_queryConditions, $this->_fields);
  210. if (count($this->_sorterArray) == 0) {
  211. return $this->_convertMongoCursor($_result, $_forceArray);
  212. } else {
  213. return $this->_convertMongoCursor($_result, $_forceArray, $this->_sorterArray);
  214. }
  215. }
  216. protected function ___getPaginatedCollectionData($_page = 1, $_itemsPerPage = 15){
  217. // $_cacher = Mooses_Cacher::getInstance(strtolower("p_".str_replace("Default_Model_Mapper", "", get_called_class())), 3600);
  218. // $_idCache = preg_replace("/[^a-zA-Z0-9_]+/", "", "query_" . json_encode($this->_queryConditions));
  219. // if($this->_forceNoCachePaginatedCollection === true || !$_arrayResults = $_cacher->load($_idCache)) {
  220. $_result = $this->fetchAll($this->_queryConditions, $this->_fields);
  221. if (count($this->_sorterArray) == 0) {
  222. $_arrayResults = $this->_convertMongoCursor($_result, true);
  223. } else {
  224. $_arrayResults = $this->_convertMongoCursor($_result, true, $this->_sorterArray);
  225. }
  226. // $_cacher->save($_arrayResults, $_idCache);
  227. // }
  228. if(count($_arrayResults) > 0) {
  229. $_paginatorAdapter = new Zend_Paginator_Adapter_Array($_arrayResults);
  230. $_paginator = new Zend_Paginator($_paginatorAdapter);
  231. $_paginator->setItemCountPerPage($_itemsPerPage)->setCurrentPageNumber($_page);
  232. return $_paginator;
  233. } else {
  234. return false;
  235. }
  236. }
  237. protected function ___setCollectionOrder($_sorterArray){
  238. $this->_sorterArray = $_sorterArray;
  239. return $this;
  240. }
  241. protected function ___useCache($_useCache = true){
  242. $this->_forceNoCachePaginatedCollection = !$_useCache;
  243. return $this;
  244. }
  245. protected function ___getData($_key = NULL, $_default = NULL, $_forceDateExtendedFormat = false){
  246. if(is_null($_key)) {
  247. return parent::getAllData();
  248. } else {
  249. if(parent::hasProperty($_key)) {
  250. $_value = parent::getProperty($_key);
  251. if (is_object($_value)) {
  252. if(isset($_value->date)) {
  253. $_valueDatetime = DateTime::createFromFormat("Y-m-d H:i:s", $_value->date);
  254. if(is_object($_valueDatetime) && $_forceDateExtendedFormat){
  255. $_value = $_valueDatetime->format("d/m/Y H:i:s");
  256. } elseif(is_object($_valueDatetime)) {
  257. $_value = $_valueDatetime->format("d/m/Y");
  258. } else {
  259. $_value = $_value->date;
  260. }
  261. } else {
  262. $_value = $_value->getAllData();
  263. }
  264. }
  265. return $_value;
  266. } else {
  267. return (($_default != NULL) ? $_default : NULL);
  268. }
  269. }
  270. }
  271. protected function ___setData($_key, $_value = NULL, $_forceCleanData = false){
  272. if(is_array($_key)){
  273. foreach($_key as $_chiave => $_value){
  274. parent::setProperty($_chiave, $_value);
  275. $this->_dataUpdate[$_chiave] = $_value;
  276. }
  277. } else {
  278. if($_key == "birthdate" || is_a($_value, "DateTime")){
  279. $_oldValue = $_value;
  280. $_value = new stdClass();
  281. $_value->date = $_oldValue->format("Y-m-d H:i:s");
  282. $_value->timezone_type = 3;
  283. $_value->timezone = "Europe/Rome";
  284. }
  285. parent::setProperty($_key, $_value, $_forceCleanData);
  286. }
  287. $this->_dataUpdate[$_key] = $_value;
  288. return $this;
  289. }
  290. protected function ___load($_value, $_attributeName = NULL){
  291. if(is_null($_attributeName)){
  292. return parent::find($_value);
  293. } else {
  294. return parent::fetchOne(array($_attributeName => $_value));
  295. }
  296. }
  297. protected function ___loadByAttribute($_attributeName = "", $_value = "", $_forceArray = false){
  298. if(stristr($_value, "/") !== FALSE){
  299. $_value = new MongoRegex($_value);
  300. }
  301. $this->loadKey = array($_attributeName => $_value);
  302. $_results = parent::fetchAll($this->loadKey);
  303. return $this->_convertMongoCursor($_results, $_forceArray);
  304. }
  305. public function ___save($entierDocument = false, $safe = true){
  306. return parent::save($entierDocument, $safe);
  307. }
  308. public function deleteData($_safe = true, $onlyOne = true){
  309. $mongoCollection = $this->_getMongoCollection(true);
  310. $this->preDelete();
  311. if (!$this->isRootDocument()) {
  312. $result = $mongoCollection->update($this->loadKey, array('$unset' => array($this->getPathToDocument() => 1)), array('safe' => $_safe));
  313. }
  314. else {
  315. $result = $mongoCollection->remove($this->loadKey, array('justOne' => $onlyOne, 'safe' => $_safe));
  316. }
  317. return $result;
  318. }
  319. public function updateData(){
  320. $_return = $this->update($this->loadKey, array('$set' => $this->_dataUpdate));
  321. return $_return;
  322. }
  323. public function setUpdateCriteria($_array){
  324. $this->loadKey = $_array;
  325. return $this;
  326. }
  327. public function flatten($array, $prefix = '') {
  328. $result = array();
  329. foreach($array as $key=>$value) {
  330. if(is_array($value)) {
  331. $result = $result + $this->flatten($value, strtolower($prefix . $key . '_'));
  332. }
  333. else {
  334. $result[strtolower($prefix . $key)] = $value;
  335. }
  336. }
  337. return $result;
  338. }
  339. }