public function testSearchableImpliedIfFilterIsSet() { $column = new Column('test_name', 'testField'); $this->assertTrue($column->getOptions()['searchable']); $column = new Column('test_name', 'testField', ['searchable' => false]); $this->assertFalse($column->getOptions()['searchable']); $column = new Column('test_name', 'testField', ['filter' => 'text', 'searchable' => false]); $this->assertTrue($column->getOptions()['searchable']); }
/** * @param Column $column * @return $this * @throws \Exception */ public function addColumn(Column $column) { if (isset($this->columns[$column->getName()])) { throw new \Exception(sprintf('a column with the name "%s" already exists.', $column->getName())); } $this->columns[$column->getName()] = $column; if ($column instanceof EntitiesCountColumn) { $this->hasCountColumns = true; } elseif ($column instanceof EntitiesScalarColumn) { $this->hasScalarColumns = true; } if (false !== $column->getOptions()['filter']) { $this->hasColumnFilter = true; } $column->setTable($this); return $this; }
/** * @param Column $column * @param null|string $value * @param QueryBuilder $qb * @param string $field * @param null|bool $empty * @throws \Exception */ private function applyColumnFilter(Column $column, $value, QueryBuilder $qb, $field, $empty) { if (null === $value && false === $column->getOptions()['filter_empty']) { throw new \Exception('this is just wrong'); } $parameter = ':' . $column->getName() . '_filter'; if ($column->getOptions()['filter'] instanceof \Voelkel\DataTablesBundle\Table\Filter\AbstractColumnFilter) { if (isset($column->getOptions()['filter']->options['field']) && null !== $column->getOptions()['filter']->options['field']) { $field = $this->table->getPrefix() . '.' . $column->getOptions()['filter']->options['field']; } $column->getOptions()['filter']->setContainer($this->table->getContainer()); $column->getOptions()['filter']->buildQuery($qb, $field, $parameter, $value); } elseif (false !== $column->getOptions()['filter']) { throw new \Exception(sprintf('invalid filter type "%s"', $column->getOptions()['filter'])); } if (null !== $empty && is_bool($empty) && true === $empty && true === $column->getOptions()['filter_empty']) { $qb->andWhere($field . ' is ' . ($empty ? '' : 'not ') . 'null'); } }
/** * @param Column $column * @param null|string $value * @param QueryBuilder $qb * @param string $field * @param null|bool $empty * @throws \Exception */ private function applyColumnFilter(Column $column, $value, QueryBuilder $qb, $field, $empty) { if (null === $value && false === $column->getOptions()['filter_empty']) { throw new \Exception('this is just wrong'); } $parameter = ':' . $column->getName() . '_filter'; if ('select' === $column->getOptions()['filter']) { if (null !== $value) { if (true === $column->getOptions()['multiple']) { $qb->andWhere($field . ' in (' . $parameter . ')'); $qb->setParameter($parameter, explode(',', $value)); } else { $qb->andWhere($field . ' = ' . $parameter); $qb->setParameter($parameter, $value); } } } elseif ('text' === $column->getOptions()['filter']) { if (null !== $value) { $filterQuery = $column->getOptions()['filter_query']; if (false !== strpos($filterQuery, 'value')) { $like = str_replace('value', $value, $filterQuery); $qb->andWhere($field . ' like ' . $parameter); $qb->setParameter($parameter, $like); } elseif (false !== strpos($filterQuery, 'split(')) { $splitStart = strpos($filterQuery, 'split('); $splitEnd = strpos($filterQuery, ')', $splitStart) + 1; $split = substr($filterQuery, $splitStart, $splitEnd - $splitStart); $splitSettings = str_replace('split(', '', $split); $splitSettings = str_replace(')', '', $splitSettings); $splitSettings = explode('|', $splitSettings); $splitChar = $splitSettings[0]; $parts = explode($splitChar, $value); $splitOp = $splitSettings[1]; $fields = []; $params = []; $param = str_replace('.', '_', $field); for ($i = 0; $i < sizeof($parts); $i++) { $parameter = str_replace($split, $parts[$i], $filterQuery); if (0 === strlen(str_replace('%', '', $parameter))) { continue; } $fields[$i] = $field . ' like :' . $param . '_' . $i; $params[$i] = $parameter; } $sql = '(' . join(' ' . $splitOp . ' ', $fields) . ')'; $qb->andWhere($sql); foreach ($params as $key => $value) { $qb->setParameter($param . '_' . $key, $value); } } } } elseif (false !== $column->getOptions()['filter']) { throw new \Exception(sprintf('invalid filter type "%s"', $column->getOptions()['filter'])); } if (null !== $empty && is_bool($empty) && true === $empty && true === $column->getOptions()['filter_empty']) { $qb->andWhere($field . ' is ' . ($empty ? '' : 'not ') . 'null'); } }
/** * @param mixed $object * @param string $name * @param Column $column * @return mixed * @throws \Exception */ private static function callGetterByColumName($object, $name, Column $column) { $methods = []; $methods[] = 'get' . ucfirst($name); foreach ($methods as $method) { if (is_array($object) || $object instanceof \ArrayAccess) { if (!$column instanceof EntitiesColumn) { throw new \Exception(sprintf('unexpected array data for column "%s"', $column->getName())); } if (sizeof($object) > $column->getOptions()['display_join_max_entries']) { return '... ' . sizeof($object); } $result = []; foreach ($object as $entity) { if (method_exists($entity, $method)) { $result[] = $entity->{$method}(); } } return join($column->getOptions()['display_join_glue'], $result); } else { $sub = null; $pos = strpos($method, '.'); if (false !== $pos) { $sub = substr($method, $pos + 1); $method = substr($method, 0, $pos); } if (method_exists($object, $method)) { $result = $object->{$method}(); if (null !== $sub && null !== $result) { $result = self::callGetterByColumName($result, $sub, $column); } return $result; } } } throw new \Exception(sprintf('no getter found for property "%s" in object of class "%s".', $name, get_class($object))); }