AbstractMongo.php 15 KB

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