/**
  * Populates an CategoryModel with post data.
  *
  * @param CategoryModel $category
  *
  * @return null
  */
 private function _populateCategoryModel(CategoryModel $category)
 {
     // Set the category attributes, defaulting to the existing values for whatever is missing from the post data
     $category->slug = craft()->request->getPost('slug', $category->slug);
     $category->enabled = (bool) craft()->request->getPost('enabled', $category->enabled);
     $category->getContent()->title = craft()->request->getPost('title', $category->title);
     $fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
     $category->setContentFromPost($fieldsLocation);
     // Parent
     $parentId = craft()->request->getPost('parentId');
     if (is_array($parentId)) {
         $parentId = isset($parentId[0]) ? $parentId[0] : null;
     }
     $category->newParentId = $parentId;
 }
Esempio n. 2
0
 /**
  * Saves a category.
  *
  * @return null
  */
 public function actionCreateCategory()
 {
     $this->requireLogin();
     $this->requireAjaxRequest();
     $groupId = craft()->request->getRequiredPost('groupId');
     craft()->userSession->requirePermission('editCategories:' . $groupId);
     $category = new CategoryModel();
     $category->groupId = $groupId;
     $category->getContent()->title = craft()->request->getPost('title');
     if (craft()->categories->saveCategory($category)) {
         $this->returnJson(array('success' => true, 'id' => $category->id, 'title' => $category->title, 'status' => $category->getStatus(), 'url' => $category->getUrl()));
     } else {
         $this->returnJson(array('success' => false));
     }
 }
Esempio n. 3
0
 public function prepCategories($data, $field)
 {
     $fieldData = array();
     if (!empty($data)) {
         $settings = $field->getFieldType()->getSettings();
         // Get category group id
         $source = $settings->getAttribute('source');
         list($type, $groupId) = explode(':', $source);
         $categories = ArrayHelper::stringToArray($data);
         foreach ($categories as $category) {
             // Skip empty
             if (empty($category)) {
                 continue;
             }
             $categoryArray = array();
             $category = DbHelper::escapeParam($category);
             // Find existing category by title or slug
             $criteria = craft()->elements->getCriteria(ElementType::Category);
             $criteria->groupId = $groupId;
             $criteria->limit = 1;
             $query = craft()->elements->buildElementsQuery($criteria);
             $query->select('elements.id');
             $conditions = array('or', array('in', 'title', $category), array('in', 'slug', $category));
             $query->andWhere($conditions);
             $results = $query->queryAll();
             if (!empty($results)) {
                 foreach ($results as $result) {
                     $categoryArray = [$result['id']];
                 }
             } else {
                 // Create category if one doesn't already exist
                 $newCategory = new CategoryModel();
                 $newCategory->getContent()->title = $category;
                 $newCategory->groupId = $groupId;
                 // Save category
                 if (craft()->categories->saveCategory($newCategory)) {
                     $categoryArray = [$newCategory->id];
                 }
             }
             // Add categories to data array
             $fieldData = array_merge($fieldData, $categoryArray);
         }
     }
     // Check for field limit - only return the specified amount
     if ($fieldData) {
         if ($field->settings['limit']) {
             $fieldData = array_chunk($fieldData, $field->settings['limit']);
             $fieldData = $fieldData[0];
         }
     }
     return $fieldData;
 }
 /**
  * Saves category from wordpress
  *
  * @param string $title Title for new/existing category
  * @param number $categoryGroupId Category group id for saving category
  *
  * @return integer Id for category.
  */
 protected function _importCategory($title, $categoryGroupId)
 {
     // Check to see if category exists
     $criteria = craft()->elements->getCriteria(ElementType::Category);
     $criteria->groupId = $categoryGroupId;
     $criteria->status = null;
     $criteria->limit = null;
     $findCategories = $criteria->find();
     $match = false;
     foreach ($findCategories as $findCategory) {
         if ($findCategory->title == $title) {
             return $findCategory->id;
         }
     }
     // Save the category
     $category = new CategoryModel();
     $category->groupId = $categoryGroupId;
     $category->getContent()->title = $title;
     if (!craft()->categories->saveCategory($category)) {
         Craft::log('Couldn’t save the category "' . $title . '"', LogLevel::Warning, true, '_importCategory', 'InstaBlog');
         return false;
     } else {
         return $category->id;
     }
 }
Esempio n. 5
0
 public function prepCategories($data, $field)
 {
     $fieldData = array();
     if (!empty($data)) {
         $settings = $field->getFieldType()->getSettings();
         // Get category group id
         $source = $settings->getAttribute('source');
         list($type, $groupId) = explode(':', $source);
         $categories = ArrayHelper::stringToArray($data);
         foreach ($categories as $category) {
             $categoryArray = array();
             if (!empty($category)) {
                 // Find existing category
                 $criteria = craft()->elements->getCriteria(ElementType::Category);
                 $criteria->title = DbHelper::escapeParam($category);
                 $criteria->groupId = $groupId;
                 $criteria->limit = 1;
                 if (!$criteria->total()) {
                     // Create category if one doesn't already exist
                     $newCategory = new CategoryModel();
                     $newCategory->getContent()->title = $category;
                     $newCategory->groupId = $groupId;
                     // Save category
                     if (craft()->categories->saveCategory($newCategory)) {
                         $categoryArray = array($newCategory->id);
                     }
                 } else {
                     $categoryArray = $criteria->ids();
                 }
             }
             // Add categories to data array
             $fieldData = array_merge($fieldData, $categoryArray);
         }
     }
     // Check for field limit - only return the specified amount
     if ($fieldData) {
         if ($field->settings['limit']) {
             $fieldData = array_chunk($fieldData, $field->settings['limit']);
             $fieldData = $fieldData[0];
         }
     }
     return $fieldData;
 }