/** * Delete rows by id values. * * @param string|array $ids * @param \Phalcon\DiInterface $di * @param \Phalcon\Events\ManagerInterface $eventsManager * @return string */ public static function deleteRows($params, $key, \Phalcon\DiInterface $di = null, \Phalcon\Events\ManagerInterface $eventsManager = null) { $result = ['success' => false, 'error' => []]; if (is_string($params)) { if (!\Engine\Tools\String::isJson($params)) { $result['error'][] = 'Params not valid'; return $result; } $params = json_decode($params); } if (is_array($params)) { if (!isset($params[$key]) && !is_array($params[$key])) { $result['error'][] = 'Array params not valid'; return $result; } $rows = !isset($rows[0]) ? [$params[$key]] : $params[$key]; } elseif ($params instanceof \stdClass) { if (!isset($params->{$key})) { $result['error'][] = 'Object params not valid'; return $result; } $rows = is_object($params->{$key}) ? [(array) $params->{$key}] : $params->{$key}; } else { $result['error'][] = 'Params not valid'; return $result; } $false = false; $form = new static(null, [], $di, $eventsManager); if (!$form->isRemovable()) { $result['error'][] = 'Data can\'t be remove from this form'; } $primary = $form->getPrimaryField(); if (!$primary) { throw new \Engine\Exception('Primary field not found'); } $primaryKey = $primary->getKey(); foreach ($rows as $id) { if (is_array($id)) { if (!isset($id[$primaryKey])) { throw new \Engine\Exception('Primary key not found in params'); } $id = $id[$primaryKey]; } elseif (is_object($id)) { if (!isset($id->{$primaryKey})) { throw new \Engine\Exception('Primary key not found in params'); } $id = $id->{$primaryKey}; } $resultRow = $form->delete($id); if ($resultRow === false) { $false = true; //$result['error'] = array_merge($result['error'], $rowResult['error']); } } if (!$false) { $result['success'] = true; $result['msg'] = "Deleted"; } return $result; }
/** * Set grid params * * @param array $params * @return \Engine\Crud\Grid */ public function setParams(array $params) { $sort = $this->getSortParamName(); $direction = $this->getSortDirectionParamName(); if (isset($params[$sort])) { if (\Engine\Tools\String::isJson($params[$sort])) { $sortParams = json_decode($params[$sort])[0]; $params[$sort] = $sortParams->property; $params[$direction] = $sortParams->direction; } $this->_sortParamValue = $params[$sort]; } if (isset($params[$direction])) { $this->_directionParamValue = $params[$direction]; } $limit = $this->getLimitParamName(); if (isset($params[$limit])) { $this->_limitParamValue = $params[$limit]; } $page = $this->getPageParamName(); if (isset($params[$page])) { $this->_pageParamValue = $params[$page]; } $this->_params = $params; return $this; }