Esempio n. 1
1
 public function moveTo($id, $position)
 {
     $item = R::findOne($this->table, 'id=?', [$this->id]);
     $desItem = R::findOne($this->table, 'id=?', [$id]);
     $itemOrder = $item->sort_order;
     $destOrder = $desItem->sort_order;
     $item->sort_order = $destOrder;
     $op = $itemOrder < $destOrder ? "-" : "+";
     if ($itemOrder < $destOrder) {
         $op = "-";
         $execParam = [$itemOrder, $destOrder];
     } else {
         $op = "+";
         $execParam = [$destOrder, $itemOrder];
     }
     // if($op == "-" && $position == "before") $destOrder--;
     // if($op == "+" && $position == "after") $destOrder++;
     $query = "UPDATE {$this->table} SET sort_order = sort_order {$op} 1";
     $query .= " WHERE (sort_order BETWEEN ? AND ?) AND id != ?";
     $execParam[] = $this->id;
     if ($op == "-" && $position == "before" || $op == "+" && $position == "after") {
         $query .= " AND id != ?";
         $execParam[] = $id;
         $destOrder2 = $op == "-" && $position == "before" ? $destOrder - 1 : $destOrder + 1;
     } else {
         $destOrder2 = $destOrder;
     }
     R::exec($query, $execParam);
     $item->sort_order = $destOrder2;
     R::store($item);
     $this->makeUnique();
     return true;
 }
Esempio n. 2
0
 public function replace($content = '')
 {
     if (empty($this->name)) {
         throw new Exception('Page needs name before it can be saved');
     }
     $userBean = Enpowi\App::user()->bean();
     R::exec('UPDATE page SET is_revision = 1 WHERE name = ? and is_revision = 0', [$this->name]);
     $oldBean = $this->_bean;
     $originalUserBean = $userBean;
     //TODO: ensure createdBy is set once and contributors is an incremental list
     $bean = R::dispense('page');
     $bean->name = $this->name;
     $bean->content = $content;
     $bean->created = R::isoDateTime();
     $bean->user = $originalUserBean;
     $bean->isRevision = false;
     if ($oldBean !== null) {
         $bean->sharedUser = $oldBean->sharedUser;
     }
     $bean->sharedUser[] = $userBean;
     R::store($bean);
     return new Page($this->name, $bean);
 }
Esempio n. 3
0
$app->post('/boards/remove', function () use($app, $jsonResponse) {
    $data = json_decode($app->environment['slim.input']);
    if (validateToken(true)) {
        $board = R::load('board', $data->boardId);
        if ($board->id == $data->boardId) {
            $before = $board->export();
            foreach ($board->sharedUser as $user) {
                if ($user->defaultBoard == $data->boardId) {
                    $user->defaultBoard = null;
                    R::store($user);
                }
            }
            R::trashAll($board->xownLane);
            R::trashAll($board->xownCategory);
            R::trash($board);
            R::exec('DELETE from board_user WHERE board_id = ?', [$data->boardId]);
            $jsonResponse->addAlert('success', 'Removed board ' . $board->name . '.');
            $actor = getUser();
            logAction($actor->username . ' removed board ' . $board->name, $before, null);
        }
        $jsonResponse->addBeans(getBoards());
        $jsonResponse->users = R::exportAll(getUsers());
    }
    $app->response->setBody($jsonResponse->asJson());
});
$app->post('/autoactions', function () use($app, $jsonResponse) {
    $data = json_decode($app->environment['slim.input']);
    if (validateToken(true)) {
        $board = R::load('board', $data->boardId);
        if ($board->id) {
            $autoAction = R::dispense('autoaction');
Esempio n. 4
0
 public function destroy($id)
 {
     R::exec('DELETE
 			FROM `passwordResets`
 		WHERE `passwordResets`.`id` = :id', [':id' => $id]);
 }
Esempio n. 5
0
 public function addRoomUse()
 {
     $productId = $this->slim->request->post()['product_id'];
     $roomName = $this->slim->request->post()['room_name'];
     $item = R::findOne('product', 'id=?', [$productId]);
     if (!$item) {
         header('Content-Type: application/json');
         echo json_encode(['error' => 'NOT_FOUND_PRODUCT'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
         exit;
     }
     $dateStr = date('Y-m-d');
     $productView = R::getRow('SELECT * FROM product_room WHERE product_id=? AND room_name=? AND view_date=?', [$productId, $roomName, $dateStr]);
     if (!$productView) {
         R::exec('INSERT INTO product_room SET product_id=?, room_name=? , view_date=?', [$productId, $roomName, $dateStr]);
     }
     R::exec('UPDATE product_room SET view_count = view_count+1 WHERE product_id=? AND room_name=? AND view_date=?', [$productId, $roomName, $dateStr]);
     header('Content-Type: application/json');
     echo json_encode(['successs' => true], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
     exit;
 }
Esempio n. 6
0
 /**
  */
 function alter()
 {
     return;
     R::freeze(true);
     R::exec('ALTER TABLE `quiz_user` ADD `session` VARCHAR(191)  NULL  DEFAULT NULL;');
 }
Esempio n. 7
0
 /**
  * @param $sql
  * @param $params
  * @return array|int
  */
 private function execQuery($sql, $params)
 {
     return strtolower(substr($sql, 0, 6)) === 'select' ? new IteratorResult(R::getAll($sql, $params), 'redbean') : R::exec($sql, $params);
 }
Esempio n. 8
0
 public function productAddStat($productId)
 {
     $dateStr = date('Y-m-d');
     $productView = R::getRow('SELECT * FROM product_add WHERE product_id=? AND add_date=?', [$productId, $dateStr]);
     if (!$productView) {
         R::exec('INSERT INTO product_add SET product_id=?, add_date=?', [$productId, $dateStr]);
     }
     R::exec('UPDATE product_add SET add_count = add_count+1 WHERE product_id=? AND add_date=?', [$productId, $dateStr]);
     header('Content-Type: application/json');
     echo json_encode(['success' => true], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
     exit;
 }
Esempio n. 9
0
 public function delete($id)
 {
     $item = R::findOne('product', 'id=?', [$id]);
     R::trash($item);
     R::exec('DELETE FROM account_product WHERE product_id = ?', [$id]);
     @unlink('upload/' . $item['picture']);
     // @unlink('upload/'.$item['thumb']);
     $this->slim->redirect($this->slim->request()->getRootUri() . '/product');
 }
Esempio n. 10
0
 /**
  * Insert or Update a bean
  *
  * @param OODBBean $bean
  * @param $data array
  * @return Result
  */
 private function saveBean($bean, $data)
 {
     // Handle File Field that may not in the $data, because Filename always go into $_FILES.
     foreach ($_FILES as $fieldName => $file) {
         $data[$fieldName] = $file["name"];
     }
     // Store Showing fields only
     $fields = $this->getShowFields();
     foreach ($fields as $field) {
         // Check is unique
         if ($field->isUnique()) {
             // Try to find duplicate beans
             $fieldName = $field->getName();
             $duplicateBeans = R::find($bean->getMeta('type'), " {$fieldName} = ? ", [$data[$field->getName()]]);
             if (count($duplicateBeans) > 0) {
                 $validateResult = "Email 已存在!";
             }
         }
         if ($field->getFieldRelation() == Field::MANY_TO_MANY) {
             // 1. Many to many
             // http://www.redbeanphp.com/many_to_many
             $keyName = "shared" . ucfirst($field->getName()) . "List";
             // Clear the current list (tableB_tableA)
             try {
                 $tableName = $this->getTableName() . "_" . $field->getName();
                 $idName = $this->getTableName() . "_id";
                 R::exec("DELETE FROM {$tableName} WHERE {$idName} = ?", [$bean->id]);
             } catch (\Exception $ex) {
             }
             // Clear the current list (tableA_tableB)
             try {
                 $tableName = $field->getName() . "_" . $this->getTableName();
                 $idName = $this->getTableName() . "_id";
                 R::exec("DELETE FROM {$tableName} WHERE {$idName} = ?", [$bean->id]);
             } catch (\Exception $ex) {
             }
             // If User have checked a value in checkbox
             if (isset($data[$field->getName()])) {
                 $valueList = $data[$field->getName()];
                 $slots = R::genSlots($valueList);
                 $relatedBeans = R::find($field->getName(), " id IN ({$slots})", $valueList);
                 foreach ($relatedBeans as $relatedBean) {
                     $bean->{$keyName}[] = $relatedBean;
                 }
             }
         } else {
             if ($field->getFieldRelation() == Field::ONE_TO_MANY) {
                 // TODO One to many
             } else {
                 if (!$field->isStorable()) {
                     // 2. If not storable, skip
                     continue;
                 } elseif ($field->getFieldRelation() == Field::NORMAL) {
                     // 3.Normal data field
                     $value = $field->getStoreValue($data);
                     if ($value == LouisCRUD::NULL) {
                         $value = null;
                     }
                     // Validate the value
                     if ($field->isStorable()) {
                         $validateResult = $field->validate($value, $data);
                     } else {
                         // TODO: check non-storable?
                         $validateResult = true;
                     }
                     // If validate failed, return result object.
                     if ($validateResult !== true) {
                         $result = new Result();
                         $result->id = @$bean->id;
                         $result->msg = $validateResult;
                         $result->fieldName = $field->getName();
                         $result->class = "callout-danger";
                         return $result;
                     }
                     // Set the value to the current bean directly
                     $bean->{$field->getName()} = $value;
                 }
             }
         }
     }
     // Store
     // TODO: Return result object
     $id = R::store($bean);
     $result = new Result();
     $result->id = $id;
     return $result;
 }
Esempio n. 11
0
            R::store($user);
            addUserToBoard($data->defaultBoard, $user);
            foreach ($data->boardAccess as $board) {
                addUserToBoard($board, $user);
            }
            logAction($actor->username . ' updated user ' . $user->username, $before, $user->export());
            $jsonResponse->addAlert('success', 'User updated.');
        }
        $jsonResponse->addBeans(getUsers());
        $jsonResponse->boards = R::exportAll(getBoards());
    }
    $app->response->setBody($jsonResponse->asJson());
});
// Remove a user.
$app->post('/users/remove', function () use($app, $jsonResponse) {
    $data = json_decode($app->environment['slim.input']);
    if (validateToken(true)) {
        $user = R::load('user', $data->userId);
        $actor = getUser();
        if ($user->id == $data->userId && $actor->isAdmin) {
            $before = $user->export();
            R::trash($user);
            R::exec('DELETE from board_user WHERE user_id = ?', [$data->userId]);
            logAction($actor->username . ' removed user ' . $before['username'], $before, null);
            $jsonResponse->addAlert('success', 'Removed user ' . $user->username . '.');
        }
        $jsonResponse->addBeans(getUsers());
        $jsonResponse->boards = R::exportAll(getBoards());
    }
    $app->response->setBody($jsonResponse->asJson());
});