PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/form/dmFormFilterDoctrine.php

https://github.com/xdade/diem
PHP | 228 lines | 179 code | 40 blank | 9 comment | 27 complexity | 7698fb5b2f36a21f38d5f75b346e24f7 MD5 | raw file
  1. <?php
  2. abstract class dmFormFilterDoctrine extends sfFormFilterDoctrine
  3. {
  4. protected function setupNestedSet() {
  5. //die(print_r(get_class($this->getTable()),1));
  6. if ($this->getTable() instanceof myDoctrineTable) {
  7. // unset NestedSet columns not needed as added in the getAutoFieldsToUnset() method
  8. $this->updateNestedSetWidget($this->getTable(), 'nested_set_parent_id', 'Child of');
  9. // check all relations for NestedSet
  10. foreach ($this->getTable()->getRelationHolder()->getAll() as $relation) {
  11. if ($relation->getTable() instanceof dmDoctrineTable && $relation->getTable()->isNestedSet()) {
  12. // check for many to many
  13. $fieldname = $relation->getType()
  14. ? dmString::underscore($relation->getAlias()) . '_list'
  15. : $relation->getLocalColumnName()
  16. ;
  17. $this->updateNestedSetWidget($relation->getTable(), $fieldname);
  18. }
  19. }
  20. }
  21. }
  22. protected function updateNestedSetWidget(dmDoctrineTable $table, $fieldname = null, $label = null)
  23. {
  24. if ($table->isNestedSet()) {
  25. if (null === $fieldname) {
  26. $fieldname = 'nested_set_parent_id';
  27. }
  28. // create if not exists
  29. if (!($this->widgetSchema[$fieldname] instanceof sfWidgetFormDoctrineChoice)) {
  30. $this->widgetSchema[$fieldname] = new sfWidgetFormDoctrineChoice(array('model' => $table->getComponentName()));
  31. }
  32. if (!($this->validatorSchema[$fieldname] instanceof sfValidatorDoctrineChoice)) {
  33. $this->validatorSchema[$fieldname] = new sfValidatorDoctrineChoice(array('model' => $table->getComponentName()));
  34. }
  35. if (null !== $label) {
  36. $this->widgetSchema[$fieldname]->setLabel('$label');
  37. }
  38. // set sorting
  39. $orderBy = 'lft';
  40. if ($table->getTemplate('NestedSet')->getOption('hasManyRoots', false)) {
  41. $orderBy = $table->getTemplate('NestedSet')->getOption('rootColumnName', 'root_id') . ', ' . $orderBy;
  42. }
  43. $this->widgetSchema[$fieldname]->setOptions(array_merge(
  44. $this->widgetSchema[$fieldname]->getOptions(),
  45. array(
  46. 'method' => 'getNestedSetIndentedName',
  47. 'order_by' => array($orderBy, ''),
  48. )));
  49. }
  50. }
  51. public function setup() {
  52. $this->setupNestedSet();
  53. parent::setup();
  54. }
  55. protected function mergeI18nForm($culture = null)
  56. {
  57. $this->mergeForm($this->createI18nForm());
  58. }
  59. public function isI18n()
  60. {
  61. return $this->getTable()->hasI18n();
  62. }
  63. /**
  64. * Create current i18n form
  65. */
  66. protected function createI18nForm($culture = null)
  67. {
  68. if (!$this->isI18n())
  69. {
  70. throw new dmException(sprintf('The model "%s" is not internationalized.', $this->getModelName()));
  71. }
  72. $i18nFormClass = $this->getI18nFormClass();
  73. $options = array();
  74. if($widgets = $this->getOption('widgets')) $options['widgets'] = $widgets;
  75. $i18nForm = new $i18nFormClass(array(), $options);
  76. unset($i18nForm['id'], $i18nForm['lang']);
  77. return $i18nForm;
  78. }
  79. protected function getI18nFormClass()
  80. {
  81. return $this->getTable()->getI18nTable()->getComponentName().'FormFilter';
  82. }
  83. protected function getRootAlias(Doctrine_Query $query, $fieldName)
  84. {
  85. return $this->getTable()->isI18nColumn($fieldName)
  86. ? $query->getRootAlias().'Translation'
  87. : $query->getRootAlias();
  88. }
  89. protected function addForeignKeyQuery(Doctrine_Query $query, $field, $value)
  90. {
  91. $fieldName = $this->getFieldName($field);
  92. if (is_array($value))
  93. {
  94. $query->andWhereIn(sprintf('%s.%s', $this->getRootAlias($query, $fieldName), $fieldName), $value);
  95. }
  96. else
  97. {
  98. $query->addWhere(sprintf('%s.%s = ?', $this->getRootAlias($query, $fieldName), $fieldName), $value);
  99. }
  100. }
  101. protected function addEnumQuery(Doctrine_Query $query, $field, $value)
  102. {
  103. $fieldName = $this->getFieldName($field);
  104. $counter = 0;
  105. $enumConditions='';
  106. $rootAlias = $this->getRootAlias($query, $fieldName);
  107. foreach ($value as $val) {
  108. $counter++;
  109. $glue = $counter < count($value) ? ' OR ' : '';
  110. $enumConditions .= sprintf('%s.%s = ?'.$glue , $rootAlias, $fieldName);
  111. }
  112. $query->addWhere($enumConditions, $value);
  113. }
  114. protected function addTextQuery(Doctrine_Query $query, $field, $values)
  115. {
  116. $fieldName = $this->getFieldName($field);
  117. if (is_array($values) && isset($values['is_empty']) && $values['is_empty'])
  118. {
  119. $query->addWhere(sprintf('(%s.%s IS NULL OR %1$s.%2$s = ?)', $this->getRootAlias($query, $fieldName), $fieldName), array(''));
  120. }
  121. else if (is_array($values) && isset($values['text']) && '' != $values['text'])
  122. {
  123. $query->addWhere(sprintf('%s.%s LIKE ?', $this->getRootAlias($query, $fieldName), $fieldName), '%'.$values['text'].'%');
  124. }
  125. }
  126. protected function addNumberQuery(Doctrine_Query $query, $field, $values)
  127. {
  128. if(!is_array($values))
  129. {
  130. $values = array('text' => $values);
  131. }
  132. $fieldName = $this->getFieldName($field);
  133. if (isset($values['is_empty']) && $values['is_empty'])
  134. {
  135. $query->addWhere(sprintf('(%s.%s IS NULL OR %1$s.%2$s = ?)', $this->getRootAlias($query, $fieldName), $fieldName), array(''));
  136. }
  137. else if (isset($values['text']) && '' !== $values['text'])
  138. {
  139. $query->addWhere(sprintf('%s.%s = ?', $this->getRootAlias($query, $fieldName), $fieldName), $values['text']);
  140. }
  141. }
  142. protected function addBooleanQuery(Doctrine_Query $query, $field, $value)
  143. {
  144. $fieldName = $this->getFieldName($field);
  145. $query->addWhere(sprintf('%s.%s = ?', $this->getRootAlias($query, $fieldName), $fieldName), $value);
  146. }
  147. protected function addDateQuery(Doctrine_Query $query, $field, $values)
  148. {
  149. if(is_array($values))
  150. {
  151. parent::addDateQuery($query, $field, $values);
  152. }
  153. else
  154. {
  155. $fieldName = $this->getFieldName($field);
  156. switch($values)
  157. {
  158. case null:
  159. case '':
  160. break;
  161. case 'today':
  162. $query->andWhere(
  163. sprintf('%s.%s >= ?', $this->getRootAlias($query, $fieldName), $fieldName),
  164. date('Y-m-d H:i:s', strtotime('-1 day'))
  165. );
  166. break;
  167. case 'week':
  168. $query->andWhere(
  169. sprintf('%s.%s >= ?', $this->getRootAlias($query, $fieldName), $fieldName),
  170. date('Y-m-d H:i:s', strtotime('-1 week'))
  171. );
  172. break;
  173. case 'month':
  174. $query->andWhere(
  175. sprintf('%s.%s >= ?', $this->getRootAlias($query, $fieldName), $fieldName),
  176. date('Y-m-d H:i:s', strtotime('-1 month'))
  177. );
  178. break;
  179. case 'year':
  180. $query->andWhere(
  181. sprintf('%s.%s >= ?', $this->getRootAlias($query, $fieldName), $fieldName),
  182. date('Y-m-d H:i:s', strtotime('-1 year'))
  183. );
  184. break;
  185. }
  186. }
  187. }
  188. }