/** * Metainformation catalogue * -------------------------------------------------- * * MICKA_LIB_INSERT.PHP for MicKa * * @link http://www.bnhelp.cz * @package Micka * @category Metadata * @version 20101120 * @authors DZ */ function deleteMd($user, $type, $value, $mode, $par = NULL) { setMickaLog('micka_lib_insert.php (deleteMd)', 'DEBUG', "user={$user}, {$type}={$value}, mode={$mode}, par={$par}"); $rs = FALSE; // autorizace if ($user == 'guest' || !canAction()) { return $rs; } $record = getMdHeader($type, $value, $col = '', $fetch = array('all', '=')); if (isset($record[0]['RECNO']) && $record[0]['RECNO'] > -1) { if (!getMdRight('edit', $user, $record[0]['DATA_TYPE'], $record[0]['CREATE_USER'], $record[0]['EDIT_GROUP'], $record[0]['VIEW_GROUP'])) { return $rs; } else { $sql = array(); if ($mode == 'all') { array_push($sql, 'DELETE FROM [md_values] WHERE [recno]=%i;', $record[0]['RECNO']); array_push($sql, 'DELETE FROM [md] WHERE [recno]=%i;', $record[0]['RECNO']); } elseif ($mode == 'value') { array_push($sql, 'DELETE FROM [md_values] WHERE [recno]=%i AND md_id<>38;', $record[0]['RECNO']); } dibi::begin(); try { dibi::query($sql); dibi::commit(); $rs = TRUE; } catch (DibiException $e) { setMickaLog($e, 'ERROR', 'micka_lib_insert.php (deleteMd)'); dibi::rollback(); } } } //Debug::dump($rs); setMickaLog('micka_lib_insert.php (deleteMd)', 'DEBUG', "return={$rs}"); return $rs; }
/** * update user roles [delete & insert] * * @param int User id * @param int Role id */ public function updateUserRoles($userId, $roles) { try { dibi::begin(); dibi::delete(self::ACL_USERS_2_ROLES_TABLE)->where('user_id = %i', $userId)->execute(); foreach ($roles as $role) { dibi::insert(self::ACL_USERS_2_ROLES_TABLE, array('user_id' => $userId, 'role_id' => $role))->execute(); } dibi::commit(); } catch (DibiDriverException $e) { dibi::rollback(); throw $e; } }
public function addEditOnFormSubmitted(AppForm $form) { // Permission form submitted $id = $this->getParam('id'); $values = $form->getValues(); if ($values['assertion_id'] == '0') { $values['assertion_id'] = NULL; } // add if (!$id) { $error = FALSE; dibi::begin(); try { foreach ($values['privilege_id'] as $privi) { foreach ($values['resource_id'] as $resou) { foreach ($values['role_id'] as $role) { if ($resou == '0') { $resou = NULL; } if ($privi == '0') { $privi = NULL; } dibi::query('INSERT INTO [' . TABLE_ACL . '] (role_id, privilege_id, resource_id, assertion_id, access) VALUES (%i, %i, %i, %iN, %b);', $role, $privi, $resou, $values['assertion_id'], $values['access']); } } } dibi::commit(); $this->flashMessage('Permission was successfully assigned.', 'ok'); if (ACL_CACHING) { unset($this->cache['gui_acl']); // invalidate cache } $this->redirect('Permission:'); } catch (Exception $e) { $error = FALSE; $form->addError('Permission was not successfully assigned.'); throw $e; } if ($error) { dibi::rollback(); } } else { // edit try { dibi::query('UPDATE [' . TABLE_ACL . '] SET %a WHERE id=%i;', $values, $id); // dibi::query('UPDATE ['.TABLE_ACL.'] SET (role_id, privilege_id, resource_id, assertion_id, access) VALUES (%i, %i, %i, %iN, %b) WHERE id=%i;', $role, $privi, $resou, $values['assertion_id'], $values['access'], $id); $this->flashMessage('Permission was successfully edited.', 'ok'); if (ACL_CACHING) { unset($this->cache['gui_acl']); // invalidate cache } $this->redirect('Permission:'); } catch (Exception $e) { $form->addError('Permission was not successfully edited.'); throw $e; } } }
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <h1>Using Transactions | dibi</h1> <?php require_once 'Nette/Debug.php'; require_once '../dibi/dibi.php'; dibi::connect(array('driver' => 'sqlite', 'database' => 'data/sample.sdb')); echo "<h2>Before</h2>\n"; dibi::query('SELECT * FROM [products]')->dump(); // -> 3 rows dibi::begin(); dibi::query('INSERT INTO [products]', array('title' => 'Test product')); echo "<h2>After INSERT</h2>\n"; dibi::query('SELECT * FROM [products]')->dump(); dibi::rollback(); // or dibi::commit(); echo "<h2>After rollback</h2>\n"; dibi::query('SELECT * FROM [products]')->dump(); // -> 3 rows again
/** * Insert manufacturer * @param array */ public function insertOne(array $values) { try { dibi::begin(); dibi::query('INSERT INTO [:prefix:manufacturers] ([id]) VALUES (NULL)'); if (empty($values['meta_keywords'])) { $values['meta_keywords'] = NULL; } if (empty($values['meta_description'])) { $values['meta_description'] = NULL; } if (empty($values['content'])) { $values['content'] = NULL; } if (empty($values['picture_id'])) { $values['picture_id'] = NULL; } $values['ref_id'] = dibi::query('SELECT LAST_INSERT_ID()')->fetchSingle(); $values['ref_type'] = pages::MANUFACTURER; dibi::query('INSERT INTO [:prefix:pages]', $values); dibi::commit(); return TRUE; } catch (Exception $e) { dibi::rollback(); return FALSE; } }
public function onSendMailFormSubmit(Form $form) { if (!$form->isValid()) { return; } $active = FALSE; try { dibi::begin(); $active = TRUE; mapper::order_emails()->insertOne(array('order_id' => $form['order_id']->getValue(), 'subject' => $form['subject']->getValue(), 'body' => $form['body']->getValue())); $mail = new Mail(); $mail->setFrom(Environment::expand('%shopName% <%shopEmail%>'))->addTo($form['to']->getValue())->setSubject($form['subject']->getValue())->setBody($form['body']->getValue())->send(); adminlog::log(__('Sent e-mail to "%s" with subject "%s"'), $form['to']->getValue(), $form['subject']->getValue()); $this->redirect('this'); $this->terminate(); } catch (RedirectingException $e) { dibi::commit(); throw $e; } catch (Exception $e) { if ($active) { dibi::rollback(); } $form->addError(__('Cannot send e-mail.')); } }
/** * Inserts page * @param array * @return someting */ public function insertOne(array $values) { if (empty($values['meta_keywords'])) { $values['meta_keywords'] = NULL; } if (empty($values['meta_description'])) { $values['meta_description'] = NULL; } if (empty($values['content'])) { $values['content'] = NULL; } if (empty($values['picture_id'])) { $values['picture_id'] = NULL; } try { dibi::begin(); dibi::query('INSERT INTO [:prefix:actualities]', array('added_at' => new DibiVariable('NOW()', 'sql'))); $id = intval(dibi::query('SELECT LAST_INSERT_ID()')->fetchSingle()); $values['ref_id'] = $id; $values['ref_type'] = pages::ACTUALITY; dibi::query('INSERT INTO [:prefix:pages]', $values); dibi::commit(); return TRUE; } catch (Exception $e) { dibi::rollback(); return FALSE; } }
/** * Delete */ public function deleteOne($id) { $id = intval($id); try { dibi::begin(); $one = dibi::query('SELECT [lft], [rgt] FROM [:prefix:categories] WHERE [id] = %i', $id)->fetch(); $ids = array(); foreach (dibi::query('SELECT [id] FROM [:prefix:categories] WHERE [lft] >= %i', $one->lft, ' AND [rgt] <= %i', $one->rgt) as $id) { $ids[] = intval($id->id); } dibi::query('DELETE FROM [:prefix:pages] WHERE [ref_id] IN %l', $ids, 'AND [ref_type] = %s', pages::CATEGORY); dibi::query('DELETE FROM [:prefix:categories] WHERE [id] IN %l', $ids); dibi::query('UPDATE [:prefix:categories] SET', '[lft] = [lft] - %i', intval($one->lft) - intval($one->rgt) + 1, 'WHERE [lft] > %i', $one->rgt); dibi::query('UPDATE [:prefix:categories] SET', '[rgt] = [rgt] - %i', intval($one->lft) - intval($one->rgt) + 1, 'WHERE [rgt] > %i', $one->rgt); dibi::commit(); return TRUE; } catch (Exception $e) { dibi::rollback(); return FALSE; } }
/** * Update */ public function updateOne(array $values) { try { dibi::begin(); $product = array(); $product['price'] = intval($values['price']); if (isset($values['manufacturer_id'])) { $product['manufacturer_id'] = $values['manufacturer_id'] == 0 ? NULL : intval($values['manufacturer_id']); } if (isset($values['category_id'])) { $product['category_id'] = $values['category_id'] == 0 ? NULL : intval($values['category_id']); } $product_id = intval($values['id']); if (isset($values['availability_id'])) { $product['availability_id'] = $values['availability_id'] == 0 ? NULL : intval($values['availability_id']); } if (isset($values['code'])) { $product['code'] = empty($values['code']) ? NULL : $values['code']; } unset($values['price'], $values['manufacturer_id'], $values['category_id'], $values['availability_id'], $values['id'], $values['code']); dibi::query('UPDATE [:prefix:products] SET', $product, 'WHERE [id] = %i', $product_id); $change = array('product_id' => $product_id, 'price' => $product['price'], 'changed_at' => new DibiVariable('NOW()', 'sql')); dibi::query('INSERT INTO [:prefix:price_changes]', $change); if (isset($values['picture_id'])) { $values['picture_id'] = $values['picture_id'] == 0 ? NULL : intval($values['picture_id']); } if (isset($values['meta_keywords']) && empty($values['meta_keywords'])) { $values['meta_keywords'] = NULL; } if (isset($values['meta_description']) && empty($values['meta_description'])) { $values['meta_description'] = NULL; } if (isset($values['content']) && empty($values['content'])) { $values['content'] = NULL; } if (!empty($values)) { $where = array(); $where['ref_id'] = $product_id; $where['ref_type'] = pages::PRODUCT; dibi::query('UPDATE [:prefix:pages] SET', $values, 'WHERE %and', $where); } dibi::commit(); //<fulltext> fulltext::dirty($product_id, TRUE); //</fulltext> return TRUE; } catch (Exception $e) { dibi::rollback(); return FALSE; } }
public function insert(array $data) { if ($this->config['useAcl']) { // check rights if (!$this->user->isAllowed(Acl::RESOURCE_USER, Acl::PRIVILEGE_ADD)) { throw new OperationNotAllowedException(); } } $data['token'] = md5($data['email'] . $data['username']); $data['registered'] = dibi::datetime(); if (isset($data['roles'])) { $roles = $data['roles']; unset($data['roles']); } if (isset($data['client_logo'])) { $clientLogo = $data['client_logo']; unset($data['client_logo']); } // create user and update his password - needed because getHasherParamsFromUserData() requires $userId try { dibi::begin(); // save random password temporarily $realPassword = $data['password']; $data['password'] = Basic::randomizer(40); $userId = parent::insert($data); $data['password'] = $realPassword; $this->update($userId, $data); dibi::commit(); } catch (DibiDriverException $e) { dibi::rollback(); throw $e; } if (isset($roles)) { $this->getRolesModel()->updateUserRoles($userId, (array) $roles); } if (!empty($clientLogo)) { $this->saveClientLogo($userId, $clientLogo); } return $userId; }
public function addEditOnFormSubmitted(NAppForm $form) { $error = false; dibi::begin(); // add action if ($this->getAction() == 'add') { try { $values = $form->getValues(); $roles = $values['roles']; unset($values['password2'], $values['roles']); // $values['password'] = md5($values['password']); // dibi::query('INSERT INTO ['.TABLE_USERS.'] %v;', $values); $user_id = UserModel::insert($values); if (count($roles)) { foreach ($roles as $role) { dibi::query('INSERT INTO [' . TABLE_USERS_ROLES . '] (user_id, role_id) VALUES (%i, %i);', $user_id, $role); } } $this->flashMessage('The user has been added.', 'ok'); dibi::commit(); if (ACL_CACHING) { unset($this->cache['gui_acl']); // invalidate cache } $this->redirect('Users:'); } catch (Exception $e) { $error = true; $form->addError('The user has not been added.'); throw $e; } } else { // edit action $id = $this->getParam('id'); try { $values = $form->getValues(); $roles = $values['roles']; unset($values['roles']); unset($values['password2']); // dibi::query('UPDATE ['.TABLE_USERS.'] SET %a WHERE id=%i;', $values, $id); UserModel::update($id, $values); dibi::query('DELETE FROM [' . TABLE_USERS_ROLES . '] WHERE user_id=%i;', $id); if (count($roles)) { foreach ($roles as $role) { dibi::query('INSERT INTO [' . TABLE_USERS_ROLES . '] (user_id, role_id) VALUES (%i, %i);', $id, $role); } } $this->flashMessage('The user has been edited.', 'ok'); dibi::commit(); if (ACL_CACHING) { unset($this->cache['gui_acl']); // invalidate cache } $this->redirect('Users:'); } catch (Exception $e) { $error = true; $form->addError('The user has not been edited.'); throw $e; } } if ($error) { dibi::rollback(); } }
/** * Save given order * @param order * @param array * @return bool */ public function save(order $order, array $products, array $visited) { // order data $data = $order->__toArray(); $data['at'] = date('Y-m-d H:i:s', time()); $data['delivery_type_id'] = $data['delivery_type']->getId(); unset($data['delivery_type']); $data['payment_type_id'] = $data['payment_type']->getId(); unset($data['payment_type']); $data['status_id'] = $data['status']->getId(); unset($data['status']); // start transaction dibi::begin(); try { $this->insert($data); $order_id = dibi::query('SELECT LAST_INSERT_ID()')->fetchSingle(); $order->setId($order_id); $order->dirty(order::UNDIRT); foreach (mapper::products()->findByIds(array_keys($products)) as $product) { dibi::query('INSERT INTO [:prefix:orders_products]', array('order_id' => $order_id, 'product_id' => $product->getId(), 'price' => $product->getPrice(), 'amount' => $products[$product->getId()])); } foreach ($visited as $_) { $values = array('order_id' => $order_id); $values['product_id'] = $_[0]->getId(); $values['visited_at'] = date('Y-m-d H:i:s', $_[1]); dibi::query('INSERT INTO [:prefix:order_visited_products]', $values); } $mail = new Mail(); $mail->setFrom(Environment::expand('%shopEmail%'))->addTo(Environment::expand('%shopEmail%'))->setSubject(__('New order'))->setBody(__('Hello, new order arrived'))->send(); $mail = new Mail(); $mail->setFrom(Environment::expand('%shopEmail%'))->addTo($data['email'])->setSubject(__('Your order at %s has been accepted', Environment::expand('%shopName%')))->setBody(str_replace('\\n', "\n", __('Hello, your order has been accepted.')))->send(); } catch (Exception $e) { dibi::rollback(); return FALSE; } dibi::commit(); return TRUE; }