EMongoSort.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * EMongoSort
  4. * @author Andrea Cardinale <a.cardinale80@gmail.com>
  5. * corresponding to Csort for MongoYii
  6. * @see yii/framework/web/CSort
  7. */
  8. /**
  9. * This is only ever used in conjunction with CGridView and CListView. It is not designed to be used independantly
  10. */
  11. class EMongoSort extends CSort
  12. {
  13. /**
  14. * @see CSort::resolveAttribute()
  15. * @param string $attribute
  16. * @return bool|string|array
  17. */
  18. public function resolveAttribute($attribute)
  19. {
  20. if($this->attributes !== array()){
  21. $attributes = $this->attributes;
  22. }elseif($this->modelClass !== null){
  23. $attributes = EmongoDocument::model($this->modelClass)->attributeNames();
  24. if(empty($attributes)){
  25. // The previous statement can return null in certain models. So this is used as backup.
  26. $attributes = EmongoDocument::model($this->modelClass)->safeAttributeNames;
  27. }
  28. }else{
  29. return false;
  30. }
  31. foreach($attributes as $name => $definition){
  32. if(is_string($name)){
  33. if($name === $attribute){
  34. return $definition;
  35. }
  36. }elseif($definition === '*'){
  37. if($this->modelClass !== null && EmongoDocument::model($this->modelClass)->hasAttribute($attribute)){
  38. return $attribute;
  39. }
  40. }elseif($definition === $attribute){
  41. return $attribute;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * @see CSort::resolveLabel()
  48. * @param string $attribute
  49. * @return string
  50. */
  51. public function resolveLabel($attribute)
  52. {
  53. $definition = $this->resolveAttribute($attribute);
  54. if(is_array($definition)){
  55. if(isset($definition['label'])){
  56. return $definition['label'];
  57. }
  58. }elseif(is_string($definition)){
  59. $attribute = $definition;
  60. }
  61. if($this->modelClass !== null){
  62. return EmongoDocument::model($this->modelClass)->getAttributeLabel($attribute);
  63. }
  64. return $attribute;
  65. }
  66. /**
  67. * @see CSort::getOrderBy()
  68. * @param EMongoCriteria $criteria
  69. * @return array|string
  70. * @throws EMongoException
  71. */
  72. public function getOrderBy($criteria = null)
  73. {
  74. $directions = $this->getDirections();
  75. if(empty($directions)){
  76. return is_string($this->defaultOrder) ? $this->defaultOrder : array();
  77. }
  78. $schema = null; // ATM the schema aspect of this function has been disabled, the code below for schema isset is left in for future reference
  79. $orders = array();
  80. foreach($directions as $attribute => $descending){
  81. $definition = $this->resolveAttribute($attribute);
  82. if(is_array($definition)){
  83. // Atm only single cell sorting is allowed, this will change to allow you to define
  84. // a true definition of multiple fields to sort when one sort field is triggered but atm that is not possible
  85. if($descending){
  86. $orders[$attribute] = isset($definition['desc']) ? -1 : 1;
  87. }else{
  88. $orders[$attribute] = isset($definition['asc']) ? 1 : -1;
  89. }
  90. }elseif($definition !== false){
  91. $attribute = $definition;
  92. if(isset($schema)){
  93. if(($pos = strpos($attribute,'.')) !== false){
  94. throw new EMongoException('MongoDB cannot sort on joined fields please modify ' . $attribute . ' to not be sortable');
  95. //$attribute=$schema->quoteTableName(substr($attribute,0,$pos)).'.'.$schema->quoteColumnName(substr($attribute,$pos+1));
  96. }else{
  97. // MongoDB does not need these escaping or table namespacing elements at all so they have been commented out for the second
  98. //$attribute=($criteria===null || $criteria->alias===null ? EMongoDocument::model($this->modelClass)->getTableAlias(true) : $schema->quoteTableName($criteria->alias)).'.'.$schema->quoteColumnName($attribute);
  99. }
  100. }
  101. $orders[$attribute] = $descending ? -1 : 1;
  102. }
  103. }
  104. return $orders;
  105. }
  106. }