/** * Method to save item. * * @param int $pk The primary key value. * @param array $data The item data. * * @return mixed */ protected function save($pk, $data) { if (!$this->allowAdd($data)) { return false; } // We load existing item first and bind data into it. $this->table->reset(); $this->table->load($pk); $this->table->bind($data); // Dump as array $item = $this->table->getProperties(true); // Handle Title increment $table2 = $this->model->getTable(); $condition = array(); // Check table has increment fields, default is title and alias. foreach ($this->incrementFields as $field => $type) { if (property_exists($this->table, $field)) { $condition[$field] = $item[$field]; } } // Recheck item with same conditions(default is title & alias), if true, increment them. // If no item got, means it is the max number. while ($table2->load($condition)) { foreach ($this->incrementFields as $field => $type) { if (property_exists($this->table, $field)) { $item[$field] = $condition[$field] = StringHelper::increment($item[$field], $type); } } } // Unset the primary key so that we can copy it. unset($item[$this->urlVar]); return $this->model->save($item); }
/** * save * * @param int|string $pk * @param DataInterface $data * * @return boolean * * @throws \Windwalker\Record\Exception\NoResultException * @throws \UnexpectedValueException * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \LogicException * @throws \DomainException */ protected function save($pk, DataInterface $data) { // We load existing item first and bind data into it. $record = $this->model->getRecord(); $record->reset(); $record->load($pk); $record->bind($data); $recordClone = $this->model->getRecord(); $condition = array(); // Check table has increment fields, default is title and alias. foreach ($this->incrementFields as $field => $type) { if ($record->hasField($field)) { $condition[$field] = $record[$field]; } } // Recheck item with same conditions(default is title & alias), if true, increment them. // If no item got, means it is the max number. do { $result = true; try { $recordClone->load($condition); foreach ($this->incrementFields as $field => $type) { if ($record->hasField($field)) { $record[$field] = $condition[$field] = StringHelper::increment($record[$field], $type); } } } catch (\RuntimeException $e) { $result = false; } $recordClone->reset(false); } while ($result); unset($record->{$this->keyName}); return $this->model->save($record); }
/** * Pose execute hook. * * @param mixed $return Executed return value. * * @return mixed */ protected function postExecute($return = null) { // Run old save process to release edit id. parent::postExecute($return); // Attempt to check-in the current record. $data = array('cid' => array($this->recordId), 'quiet' => true); $this->fetch($this->prefix, $this->viewList . '.check.checkin', $data); // Reset the ID and then treat the request as for Apply. $this->data[$this->key] = 0; $this->data['checked_out'] = ''; $this->data['checked_out_time'] = ''; if (isset($this->data['title'])) { $this->data['title'] = StringHelper::increment($this->data['title']); } if (isset($this->data['alias'])) { $this->data['alias'] = StringHelper::increment($this->data['alias'], 'dash'); } // Set new date into session. $this->app->setUserState($this->context . '.data', $this->data); return $return; }
/** * Method to change the title & alias. * * @param integer $categoryId The id of the category. * @param string $alias The alias. * @param string $title The title. * * @return array Contains the modified title and alias. */ protected function generateNewTitle($categoryId, $alias, $title) { // Alter the title & alias $table = $this->getTable(); while ($table->load(array('alias' => $alias, 'catid' => $categoryId))) { $title = StringHelper::increment($title); $alias = StringHelper::increment($alias, 'dash'); } return array($title, $alias); }
/** * Test... * * @param string $string String to increment. * @param string $style Default of Dash. * @param string $number Number to increment. * @param string $expected Expected value. * * @return void * * @covers Windwalker\String\StringHelper::increment * @dataProvider seedTestIncrement * @since 2.0 */ public function testIncrement($string, $style, $number, $expected) { $this->assertEquals($expected, StringHelper::increment($string, $style, $number)); }
/** * getSuccessRedirect * * @param DataInterface|Entity $data * * @return string * * @throws \OutOfRangeException */ protected function getSuccessRedirect(DataInterface $data = null) { $data = $data ?: $this->getDataObject(); switch ($this->task) { case 'save2close': return $this->router->route(strtolower($this->config['list_name']), $this->getRedirectQuery()); case 'save2new': return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery(array('new' => ''))); case 'save2copy': $data->{$this->keyName} = null; if ($data->title) { $data->title = StringHelper::increment($data->title); } if ($data->alias) { $data->alias = StringHelper::increment($data->alias, StringHelper::INCREMENT_STYLE_DASH); } $this->setUserState($this->getContext('edit.data'), $data->dump(true)); return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery()); default: $pk = $data->{$this->keyName}; return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery(array($this->keyName => $pk))); } }