AbstractMongo.php 15 KB

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