Esempio n. 1
0
 public function forFullUpdateCategoryByParentId($categoryId, $parents)
 {
     $VALUES = '';
     $categoryId = intval($categoryId);
     if ($categoryId <= 0) {
         return false;
     }
     foreach ($parents as $_parent) {
         $_parent = intval($_parent);
         if ($_parent > 0) {
             if ($VALUES != '') {
                 $VALUES .= ',';
             }
             $VALUES .= sprintf('(%d,%d)', $categoryId, $_parent);
         }
     }
     $category = new Category();
     if ($VALUES) {
         $this->getWriteConnection()->execute('INSERT INTO ' . $this->getSource() . '(categoryId, parentId) VALUES ' . $VALUES . ' ON DUPLICATE KEY UPDATE categoryId=categoryId;');
         // 全量更新时,删除 $parents 中没有的 id
         $this->getModelsManager()->executeQuery(sprintf('DELETE FROM Eva\\Wiki\\Entities\\CategoriesCategories  WHERE categoryId=%d AND parentId NOT IN (%s)', $categoryId, implode(',', $parents)));
         $category->unmarkRoot($categoryId);
     } else {
         // $VALUES为空时,即表示解除所有分类父子关系。
         $this->getModelsManager()->executeQuery('DELETE FROM Eva\\Wiki\\Entities\\CategoriesCategories  WHERE categoryId=:categoryId:', array('categoryId' => $categoryId));
         $category->markRoot($categoryId);
     }
 }
Esempio n. 2
0
 public function addCid()
 {
     if ($this->cid) {
         return $this->cid;
     }
     $category = new Models\Category();
     $categories = $category->find(array("order" => "id DESC", "limit" => 100));
     if ($categories) {
         $options = array('All Categories');
         foreach ($categories as $categoryitem) {
             $options[$categoryitem->id] = $categoryitem->categoryName;
         }
         $element = new Select('cid', $options);
     }
     $this->add($element);
     return $this->cid = $element;
 }
Esempio n. 3
0
 public function deleteAction()
 {
     if (!$this->request->isDelete()) {
         $this->response->setStatusCode('405', 'Method Not Allowed');
         $this->response->setContentType('application/json', 'utf-8');
         return $this->response->setJsonContent(array('errors' => array(array('code' => 405, 'message' => 'ERR_POST_REQUEST_METHOD_NOT_ALLOW'))));
     }
     $id = $this->dispatcher->getParam('id');
     $category = Models\Category::findFirst($id);
     try {
         $category->delete();
     } catch (\Exception $e) {
         return $this->showExceptionAsJson($e, $category->getMessages());
     }
     $this->response->setContentType('application/json', 'utf-8');
     return $this->response->setJsonContent($category);
 }
Esempio n. 4
0
 public function getParents()
 {
     if ($this->parents) {
         return $this->parents;
     }
     $category = new Models\Category();
     $categories = $category->find(array("limit" => 100));
     $category = $this->getModel();
     if ($category->parents) {
         $values = array();
         foreach ($category->parents as $_cate) {
             $values[] = $_cate->id;
         }
         foreach ($categories as $key => $item) {
             $check = new Check('parents[]', array('value' => $item->id));
             if (in_array($item->id, $values)) {
                 $check->setDefault($item->id);
             }
             $check->setLabel($item->categoryName);
             $this->parents[] = $check;
         }
     }
     return $this->parents;
 }
Esempio n. 5
0
 public static function getOrCreate($categoryName)
 {
     $categoryDAO = new Category();
     $category = $categoryDAO->findFirst("categoryName='{$categoryName}'");
     $pinyin = new Pinyin();
     if (!$category) {
         $category = new Category();
         $category->categoryName = $categoryName;
         $category->initial = substr($pinyin->transformUcwords($category->categoryName), 0, 1);
         $category->save();
     }
     return $category;
 }
Esempio n. 6
0
 public function createEntry($data)
 {
     $textData = isset($data['text']) ? $data['text'] : array();
     $synonymies = isset($data['synonymies']) ? $data['synonymies'] : array();
     $synonymies = !$synonymies && isset($data['keywords']) ? $data['keywords'] : $synonymies;
     $categoryData = isset($data['categories']) ? $data['categories'] : array();
     $categoryNamesData = isset($data['categoryNames']) ? $data['categoryNames'] : array();
     $entry = self::findFirst("title='{$data['title']}'");
     $newEntry = false;
     if (!$entry) {
         $entry = $this;
         $newEntry = true;
     }
     if ($textData) {
         unset($data['text']);
         $text = new EntryTexts();
         $text->assign($textData);
         $entry->text = $text;
     }
     $keywords = array();
     if ($synonymies) {
         $synonymiesArray = is_array($synonymies) ? $synonymies : explode(',', $synonymies);
         $mainKeyword = new EntryKeywords();
         $mainKeyword->keyword = $data['title'];
         $keywords[] = $mainKeyword;
         foreach ($synonymiesArray as $_keyword) {
             $keyword = new EntryKeywords();
             $keyword->keyword = $_keyword;
             $keywords[] = $keyword;
         }
     }
     $entry->keywords = $keywords;
     $categories = array();
     if ($categoryData) {
         unset($data['categories']);
         foreach ($categoryData as $categoryId) {
             $category = Category::findFirst($categoryId);
             if ($category) {
                 $categories[] = $category;
             }
         }
     }
     // 通过分类名指定分类
     if ($categoryNamesData) {
         unset($data['categoryNames']);
         foreach ($categoryNamesData as $_categoryName) {
             $categories[] = Category::getOrCreate($_categoryName);
         }
     }
     $entry->categories = $categories;
     if ($newEntry) {
         $entry->assign($data);
     }
     $pinyin = new Pinyin();
     $entry->initial = substr($pinyin->transformUcwords($entry->title), 0, 1);
     if (!$entry->save()) {
         throw new RuntimeException('Create post failed');
     }
     return $entry;
 }