Exemple #1
0
 public function moveDocuments($document_id_arr, $tocat_id, $move_option_arr = array())
 {
     $dispatcher = JDispatcher::getInstance();
     $user = JFactory::getUser();
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_judownload/tables');
     $catTable = JTable::getInstance("Category", "JUDownloadTable");
     if ($tocat_id) {
         if (!$catTable->load($tocat_id)) {
             JError::raiseWarning(500, JText::_('COM_JUDOWNLOAD_TARGET_CATEGORY_NOT_FOUND'));
             return false;
         }
         $table = $this->getTable();
         $db = JFactory::getDbo();
         $assetName = 'com_judownload.category.' . (int) $tocat_id;
         $query = 'SELECT id FROM #__assets WHERE name="' . $assetName . '"';
         $db->setQuery($query);
         $tocat_asset_id = $db->loadResult();
         $canCreate = $user->authorise('judl.document.create', $assetName);
         if (!$canCreate) {
             JError::raiseError(100, JText::sprintf('COM_JUDOWNLOAD_CAN_NOT_CREATE_DOCUMENT_IN_THIS_CATEGORY', $catTable->title));
             return false;
         }
     } else {
         JError::raiseWarning(500, JText::_('COM_JUDOWNLOAD_NO_TARGET_CATEGORY_SELECTED'));
         return false;
     }
     if (empty($document_id_arr)) {
         JError::raiseError(100, JText::_('COM_JUDOWNLOAD_NO_ITEM_SELECTED'));
         return false;
     }
     set_time_limit(0);
     $moved_documents = array();
     foreach ($document_id_arr as $doc_id) {
         if (!$table->load($doc_id)) {
             continue;
         }
         $assetName = 'com_judownload.document.' . (int) $doc_id;
         $canDoEdit = $user->authorise('judl.document.edit', $assetName);
         if (!$canDoEdit) {
             if (!$user->id) {
                 JError::raiseWarning(100, JText::sprintf('COM_JUDOWNLOAD_YOU_DONT_HAVE_PERMISSION_TO_ACCESS_DOCUMENT', $table->title));
                 continue;
             } else {
                 if ($user->id == $table->created_by) {
                     $canDoEditOwn = $user->authorise('judl.document.edit.own', $assetName);
                     if (!$canDoEditOwn) {
                         JError::raiseWarning(100, JText::sprintf('COM_JUDOWNLOAD_YOU_DONT_HAVE_PERMISSION_TO_ACCESS_DOCUMENT', $table->title));
                         continue;
                     }
                 }
             }
         }
         $query = "SELECT cat_id FROM #__judownload_documents_xref WHERE doc_id = " . $doc_id . " AND main=1";
         $db->setQuery($query);
         $cat_id = $db->loadResult();
         if ($tocat_id == $cat_id) {
             continue;
         }
         $result = $dispatcher->trigger($this->onContentBeforeMove, array($this->option . '.' . $this->name, $table, $tocat_id, $move_option_arr));
         if (in_array(false, $result, true)) {
             $this->setError($table->getError());
             return false;
         }
         if ($table->style_id == -1) {
             $oldTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($cat_id);
             $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($tocat_id);
             if ($oldTemplateStyleObject->template_id != $newTemplateStyleObject->template_id) {
                 if (in_array('keep_template_params', $move_option_arr)) {
                     $table->style_id = $oldTemplateStyleObject->id;
                 } else {
                     $query = "UPDATE #__judownload_documents SET template_params = '' WHERE id=" . $doc_id;
                     $db->setQuery($query);
                     $db->execute();
                 }
             }
         }
         $query = "SELECT COUNT(*) FROM #__judownload_documents_xref WHERE cat_id=" . $tocat_id . " AND doc_id=" . $doc_id . " AND main=0";
         $db->setQuery($query);
         $is_secondary_cat = $db->loadResult();
         if ($is_secondary_cat) {
             $query = "DELETE FROM #__judownload_documents_xref WHERE doc_id=" . $doc_id . " AND main=1";
             $db->setQuery($query);
             $db->execute();
             $query = "UPDATE #__judownload_documents_xref SET main=1 WHERE cat_id=" . $tocat_id . " AND doc_id=" . $doc_id;
             $db->setQuery($query);
             $db->execute();
         } else {
             $query = "UPDATE #__judownload_documents_xref SET cat_id=" . $tocat_id . " WHERE doc_id=" . $doc_id . " AND main=1";
             $db->setQuery($query);
             $db->execute();
         }
         if (in_array('keep_permission', $move_option_arr)) {
             $query = 'UPDATE #__assets SET `parent_id` = ' . $tocat_asset_id . ' WHERE name="com_judownload.document.' . $doc_id . '"';
             $db->setQuery($query);
             $db->execute();
         } else {
             $query = 'UPDATE #__assets SET `parent_id` = ' . $tocat_asset_id . ', `rules` = "{}" WHERE name="com_judownload.document.' . $doc_id . '"';
             $db->setQuery($query);
             $db->execute();
         }
         $moved_documents[] = $doc_id;
         $this->cleanCache();
         $dispatcher->trigger($this->onContentAfterMove, array($this->option . '.' . $this->name, $table, $tocat_id, $move_option_arr));
     }
     $total_moved_documents = count($moved_documents);
     if ($total_moved_documents) {
         $old_field_groupid = JUDownloadHelper::getCategoryById($cat_id)->fieldgroup_id;
         $new_field_groupid = JUDownloadHelper::getCategoryById($tocat_id)->fieldgroup_id;
         $keep_extra_fields = in_array("keep_extra_fields", $move_option_arr);
         if ($keep_extra_fields) {
             $keep_extra_fields = $old_field_groupid == $new_field_groupid ? true : false;
         }
         if (!$keep_extra_fields) {
             foreach ($moved_documents as $doc_id) {
                 JUDownloadHelper::deleteFieldValuesOfDocument($doc_id);
             }
         }
         $old_criteria_groupid = JUDownloadHelper::getCategoryById($cat_id)->criteriagroup_id;
         $new_criteria_groupid = JUDownloadHelper::getCategoryById($tocat_id)->criteriagroup_id;
         $keep_rates = in_array("keep_rates", $move_option_arr);
         if ($keep_rates) {
             $keep_rates = $old_criteria_groupid == $new_criteria_groupid ? true : false;
         }
         if (!$keep_rates) {
             JTable::addIncludePath(JPATH_ADMINISTRATOR . "/components/com_judownload/tables");
             $ratingTable = JTable::getInstance("Rating", "JUDownloadTable");
             foreach ($moved_documents as $doc_id) {
                 $query = "SELECT id FROM #__judownload_rating WHERE doc_id = " . $doc_id;
                 $db->setQuery($query);
                 $ratingIds = $db->loadColumn();
                 foreach ($ratingIds as $ratingId) {
                     $ratingTable->delete($ratingId);
                 }
             }
         }
     }
     return $total_moved_documents;
 }
 public function prepareDataToImport($data, $isInsert, $config = array())
 {
     $db = JFactory::getDbo();
     $defaultData = array('cat_id' => $data['main_cat']);
     if ($isInsert) {
         $user = JFactory::getUser();
         $offset = $user->getParam('timezone', JFactory::getConfig()->get('offset'));
         if (!empty($config['publish_up'])) {
             $defaultData['publish_up'] = JFactory::getDate($config['publish_up'], $offset)->toSql();
         }
         if (!empty($config['force_publish'])) {
             $data[$this->field_name_id_array['published']] = $config['force_publish'];
         }
         if (!empty($config['publish_down']) && $config['publish_down'] != $db->getNullDate()) {
             $defaultData['publish_down'] = JFactory::getDate($config['publish_down'], $offset)->toSql();
         }
         if (!empty($config['created_by']) && $config['created_by'] > 0) {
             $defaultData['created_by'] = $config['created_by'];
         }
         if (!empty($config['default_icon'])) {
             $defaultData['icon'] = $config['default_icon'];
         }
         if (empty($defaultData['metadescription']) && !empty($config['meta_description'])) {
             $defaultData['metadescription'] = $config['meta_description'];
         }
         if (empty($defaultData['metakeyword']) && !empty($config['meta_keyword'])) {
             $defaultData['metakeyword'] = $config['meta_keyword'];
         }
         if (empty($data['secondary_cats']) && !empty($config['secondary_cats_assign'])) {
             $data['secondary_cats'] = $config['secondary_cats_assign'];
         }
         if (empty($defaultData['language'])) {
             $defaultData['language'] = '*';
         }
         if (empty($defaultData['access'])) {
             $defaultData['access'] = 1;
         }
         if (empty($defaultData['style_id'])) {
             $defaultData['style_id'] = -1;
         }
         if (empty($defaultData['layout'])) {
             $defaultData['layout'] = -1;
         }
         if (empty($defaultData['params'])) {
             $params = array('display_params' => array('show_comment' => -2, 'fields' => array('title' => array('detail_view' => -2), 'created' => array('detail_view' => -2), 'author' => array('detail_view' => -2), 'cat_id' => array('detail_view' => -2), 'rating' => array('detail_view' => -2))));
             $defaultData['params'] = json_encode($params);
         }
     }
     if (!empty($data['secondary_cats'])) {
         if (is_string($data['secondary_cats'])) {
             $data['secondary_cats'] = explode(',', $data['secondary_cats']);
         }
         $index = array_search($data['main_cat'], $data['secondary_cats']);
         if ($index) {
             unset($data['secondary_cats'][$index]);
         }
         $rootCat = JUDownloadFrontHelperCategory::getRootCategory();
         $params = JUDownloadHelper::getParams();
         $index = array_search($rootCat->id, $data['secondary_cats']);
         if ($index && !$params->get('allow_add_doc_to_root', 0)) {
             unset($data['secondary_cats'][$index]);
         }
         $data[$this->field_name_id_array['cat_id']]['secondary'] = implode(',', $data['secondary_cats']);
     }
     $aliasFieldId = $this->field_name_id_array['alias'];
     $titleFieldId = $this->field_name_id_array['title'];
     if (!$isInsert) {
         $oldMainCatId = JUDownloadFrontHelperCategory::getMainCategoryId($data['id']);
         if ($data['main_cat'] != $oldMainCatId) {
             if (empty($data[$titleFieldId])) {
                 $document = JUDownloadHelper::getDocumentById($data['id']);
                 $data[$titleFieldId] = $document->title;
             }
             if (empty($data[$aliasFieldId])) {
                 $document = JUDownloadHelper::getDocumentById($data['id']);
                 $data[$aliasFieldId] = $document->alias;
             }
         }
     }
     if (empty($data[$aliasFieldId])) {
         $data[$aliasFieldId] = strtolower(JApplication::stringURLSafe($data[$titleFieldId]));
     }
     $this->titleIncrement($data['main_cat'], $data['id'], $data[$aliasFieldId], $data[$titleFieldId]);
     $data['gallery'] = !empty($data['gallery']) ? explode('|', $data['gallery']) : array();
     $data['files'] = !empty($data['files']) ? explode('|', $data['files']) : array();
     $data['related_docs'] = !empty($data['related_docs']) ? explode(',', $data['related_docs']) : array();
     if (!empty($data['style_id']) && !$isInsert) {
         $oldTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfDocument($data['id']);
         $styleId = $data['style_id'];
         if ($styleId == -2) {
             $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getDefaultTemplateStyle();
         } elseif ($styleId == -1) {
             $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($data['main_cat']);
         } else {
             $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleObject($styleId);
         }
         if ($oldTemplateStyleObject->template_id != $newTemplateStyleObject->template_id && !isset($data['template_params'])) {
             $data['template_params'] = '';
         }
     }
     if (!empty($data['template_params']) && is_array($data['template_params'])) {
         $registry = new JRegistry();
         $registry->loadArray($data['template_params']);
         $data['template_params'] = $registry->toString();
     }
     $data['plugin_params'] = !empty($data['plugin_params']) ? $data['plugin_params'] : array();
     if (!is_array($data['plugin_params'])) {
         $registry = new JRegistry();
         $registry->loadString($data['plugin_params']);
         $data['plugin_params'] = $registry->toArray();
     }
     $db_plugin_params = array();
     if (!$isInsert) {
         $db->setQuery("SELECT plugin_params FROM #__judownload_documents WHERE id = " . $data['id']);
         $rule_str = $db->loadResult();
         $rule_registry = new JRegistry();
         $rule_registry->loadString($rule_str);
         $db_plugin_params = $rule_registry->toArray();
     }
     if (!empty($db_plugin_params)) {
         $db->setQuery("SELECT element FROM #__extensions WHERE type='plugin' AND folder = 'judownload'");
         $rule_plugins = $db->loadColumn();
         foreach ($db_plugin_params as $name => $value) {
             if (!in_array($name, $rule_plugins)) {
                 unset($db_plugin_params[$name]);
             }
             if (array_key_exists($name, $data['plugin_params'])) {
                 unset($db_plugin_params[$name]);
             }
         }
     }
     $plugin_params = array_merge($db_plugin_params, $data['plugin_params']);
     $registry = new JRegistry();
     $registry->loadArray($plugin_params);
     $data['plugin_params'] = $registry->toString();
     $fieldsData = array();
     foreach ($data as $key => $value) {
         if (is_numeric($key)) {
             $fieldsData[$key] = $value;
         } elseif (is_string($key) && $key != 'files' && $key != 'gallery' && $key != 'main_cat' && $key != 'secondary_cats' && $key != 'related_docs') {
             $defaultData[$key] = $value;
         }
     }
     $postData = array('main_cat' => $data['main_cat'], 'data' => $defaultData, 'fieldsData' => $fieldsData, 'files' => $data['files'], 'gallery' => $data['gallery'], 'related_docs' => $data['related_docs']);
     return $postData;
 }
Exemple #3
0
 public function delete($pk = null)
 {
     $db = JFactory::getDbo();
     $k = $this->_tbl_key;
     $pk = is_null($pk) ? $this->{$k} : $pk;
     $this->load($pk);
     if ($this->home == 1 || $this->default == 1) {
         $this->setError(JText::_('COM_JUDOWNLOAD_CAN_NOT_DELETE_DEFAULT_TEMPLATE_STYLE'));
         return false;
     }
     $defaultStyleObject = JUDownloadFrontHelperTemplate::getDefaultTemplateStyle();
     $query = $db->getQuery(true);
     $query->update('#__judownload_categories');
     $query->set('style_id = -2');
     if ($defaultStyleObject->template_id != $this->template_id) {
         $query->set('template_params = ""');
     }
     $query->where('parent_id = 0');
     $query->where('style_id = ' . $pk);
     $db->setQuery($query);
     $db->execute();
     $query = $db->getQuery(true);
     $query->select('id');
     $query->from('#__judownload_categories');
     $query->where('style_id = ' . $pk);
     $db->setQuery($query);
     $categoryArrayAssignedToStyle = $db->loadColumn();
     $query = $db->getQuery(true);
     $query->update('#__judownload_categories');
     $query->set('style_id = -1');
     $query->where('parent_id != 0');
     $query->where('style_id = ' . $pk);
     $db->setQuery($query);
     $db->execute();
     foreach ($categoryArrayAssignedToStyle as $categoryIdAssignedToStyle) {
         $styleObjectOfCategory = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($categoryIdAssignedToStyle);
         if ($styleObjectOfCategory->template_id != $this->template_id) {
             $query = $db->getQuery(true);
             $query->update('#__judownload_categories');
             $query->set('template_params = ""');
             $query->where('id = ' . $categoryIdAssignedToStyle);
             $db->setQuery($query);
             $db->execute();
             $query = $db->getQuery(true);
             $query->update('#__judownload_documents AS d');
             $query->set('d.template_params = ""');
             $query->join('', '#__judownload_documents_xref AS dxref ON d.id = dxref.doc_id AND dxref.main = 1');
             $query->where('d.style_id = -1');
             $query->where('dxref.cat_id = ' . $categoryIdAssignedToStyle);
             $db->setQuery($query);
             $db->execute();
             JUDownloadFrontHelperTemplate::removeTemplateParamsOfInheritedStyleCatDoc($categoryIdAssignedToStyle);
         }
     }
     $query = $db->getQuery(true);
     $query->select('d.id');
     $query->select('dxref.cat_id AS cat_id');
     $query->from('#__judownload_documents AS d');
     $query->join('', '#__judownload_documents_xref AS dxref ON d.id = dxref.doc_id AND dxref.main = 1');
     $query->where('d.style_id = ' . $pk);
     $db->setQuery($query);
     $documentObjectListAssignedToStyle = $db->loadObjectList();
     $catArrayResetTemplateParams = array();
     foreach ($documentObjectListAssignedToStyle as $documentObject) {
         $styleObjectOfCategory = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($documentObject->cat_id);
         if ($styleObjectOfCategory->template_id != $this->template_id) {
             $catArrayResetTemplateParams[] = $documentObject->cat_id;
         }
     }
     $catArrayResetTemplateParams = array_unique($catArrayResetTemplateParams);
     if (is_array($catArrayResetTemplateParams) && count($catArrayResetTemplateParams)) {
         $query = $db->getQuery(true);
         $query->update('#__judownload_documents AS d');
         $query->join('', '#__judownload_documents_xref AS dxref ON d.id = dxref.doc_id AND dxref.main = 1');
         $query->set('d.template_params = ""');
         $query->set('d.style_id = -1');
         $query->where('dxref.cat_id IN (' . implode(',', $catArrayResetTemplateParams) . ')');
         $query->where('d.style_id = ' . $pk);
         $db->setQuery($query);
         $db->execute();
     }
     return parent::delete($pk);
 }
Exemple #4
0
 public function docChangeCategory()
 {
     $app = JFactory::getApplication();
     if ($app->input->get('action', '') == 'update-maincat') {
         $ori_cat_id = $app->input->getInt('ori_cat_id', 0);
         $ori_cat = JUDownloadHelper::getCategoryById($ori_cat_id);
         $data['ori_field_group_id'] = 0;
         $data['new_field_group_id'] = 0;
         $data['new_field_group_name'] = "";
         $data['path'] = "";
         if ($ori_cat) {
             $data['ori_field_group_id'] = $ori_cat->fieldgroup_id;
         }
         $new_cat_id = $app->input->getInt('new_cat_id', 0);
         $rootCat = JUDownloadFrontHelperCategory::getRootCategory();
         $params = JUDownloadHelper::getParams();
         if ($rootCat->id == $new_cat_id && !$params->get('allow_add_doc_to_root', 0)) {
             return "";
         }
         $new_cat = JUDownloadHelper::getCategoryById($new_cat_id);
         if ($new_cat) {
             $db = JFactory::getDbo();
             $query = "SELECT id, name FROM #__judownload_fields_groups WHERE id = " . $new_cat->fieldgroup_id . " AND published = 1";
             $db->setQuery($query);
             $fieldgroup = $db->loadObject();
             if ($fieldgroup) {
                 $data['new_field_group_id'] = $fieldgroup->id;
                 $data['new_field_group_name'] = $fieldgroup->name;
             }
             $data['path'] = JUDownloadHelper::generateCategoryPath($new_cat_id);
         }
         if ($data['ori_field_group_id'] != $data['new_field_group_id']) {
             $data['msg_field_group'] = JText::_('COM_JUDOWNLOAD_CHANGE_MAIN_CATEGORY_CAUSE_CHANGE_FIELD_GROUP_WARNING');
         }
         $documentId = $app->input->getInt('id', 0);
         $data['message_style'] = JText::_('COM_JUDOWNLOAD_INHERIT');
         if ($documentId) {
             $documentObject = JUDownloadHelper::getDocumentById($documentId);
             if ($documentObject->style_id == -1) {
                 $oldStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($documentId->cat_id);
                 $newStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($new_cat->id);
                 if ($oldStyleObject->template_id != $newStyleObject->template_id) {
                     $data['msg_style'] = JText::_('COM_JUDOWNLOAD_CHANGE_MAIN_CATEGORY_CAUSE_CHANGE_TEMPLATE_WARNING');
                 }
             }
             $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($new_cat->id);
             $data['message_style'] = JText::_('COM_JUDOWNLOAD_INHERIT') . ' (' . $newTemplateStyleObject->title . ' [' . $newTemplateStyleObject->template_title . ' ]' . ')';
         }
         return json_encode($data);
     } elseif ($app->input->getInt('action', '') == 'insert_secondary_cat') {
         $cat_id_str = $app->input->get('secondary_cat_id', '', 'string');
         $html = '';
         if (!empty($cat_id_str)) {
             $cat_id_arr = explode(",", $cat_id_str);
             foreach ($cat_id_arr as $key => $cat_id) {
                 $html .= "<li id=\"cat-" . $cat_id . "\"><a class=\"drag-icon\"></a><span>" . JUDownloadHelper::generateCategoryPath($cat_id) . "</span><a href=\"#\" onclick=\"return false\" class=\"remove-secondary-cat\" ><i class=\"icon-minus fa fa-minus-circle\"></i> " . JText::_('COM_JUDOWNLOAD_REMOVE') . "</a></li>";
             }
         }
         return $html;
     }
 }
Exemple #5
0
 public function save($data)
 {
     $app = JFactory::getApplication();
     $categoryArrayAddToStyle = $data['categories'] ? $data['categories'] : array();
     unset($data['categories']);
     $dispatcher = JDispatcher::getInstance();
     $table = $this->getTable();
     $key = $table->getKeyName();
     $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
     $isNew = true;
     $db = JFactory::getDbo();
     $defaultTemplateStyleObject = JUDownloadFrontHelperTemplate::getDefaultTemplateStyle();
     JPluginHelper::importPlugin('content');
     try {
         if ($pk > 0) {
             $table->load($pk);
             if ($table->home == 1) {
                 $data["home"] = 1;
             }
             $templateId = $table->template_id;
             $isNew = false;
         } else {
             $templateId = $data["template_id"];
         }
         if ($data["home"] == 1 && $table->home != 1) {
             if ($templateId != $defaultTemplateStyleObject->template_id) {
                 if ($data['changeTemplateStyleAction'] == 1) {
                     JUDownloadFrontHelperTemplate::updateStyleIdForCatDocUsingDefaultStyle($defaultTemplateStyleObject->id);
                     $app->enqueueMessage(JText::sprintf('COM_JUDOWNLOAD_DEFAULT_TEMPLATE_STYLE_HAS_BEEN_CHANGED_TO_X', $defaultTemplateStyleObject->title), 'Notice');
                 } else {
                     JUDownloadFrontHelperTemplate::removeTemplateParamsOfCatDocUsingDefaultStyle();
                 }
             }
         }
         if ($data['home']) {
             if ($data['home'] != 1) {
                 if ($defaultTemplateStyleObject->template_id != $templateId) {
                     $data['home'] = 0;
                 }
             }
         }
         $query = $db->getQuery(true);
         $query->select("tpl.*");
         $query->select("plg.title,plg.folder");
         $query->from("#__judownload_templates AS tpl");
         $query->join("", "#__judownload_plugins AS plg ON plg.id = tpl.plugin_id");
         $query->where("tpl.id = " . $templateId);
         $db->setQuery($query);
         $templateObject = $db->loadObject();
         if (!is_object($templateObject)) {
             $this->setError(JText::_("COM_JUDOWNLOAD_TEMPLATE_DOES_NOT_EXIST"));
             return false;
         }
         $query = $db->getQuery(true);
         $query->select("id");
         $query->from("#__judownload_template_styles");
         $query->where("template_id = " . $templateObject->parent_id);
         $db->setQuery($query);
         $styleParentObjectList = $db->loadColumn();
         $parentStyleId = (int) $data["parent_id"];
         if (is_array($styleParentObjectList) && !empty($styleParentObjectList)) {
             if (!in_array($parentStyleId, $styleParentObjectList)) {
                 $this->setError(JText::_("COM_JUDOWNLOAD_INVALID_PARENT_STYLE"));
                 return false;
             }
         } else {
             if ($templateObject->parent_id == 1) {
                 $this->setError(JText::_("COM_JUDOWNLOAD_PARENT_STYLE_DOES_NOT_EXIST_YOU_NEED_TO_CREATE_ROOT_STYLE"));
                 return false;
             }
             $query = $db->getQuery(true);
             $query->select("tpl.*");
             $query->select("plg.title,plg.folder");
             $query->from("#__judownload_templates AS tpl");
             $query->join("", "#__judownload_plugins AS plg ON plg.id = tpl.plugin_id");
             $query->where("tpl.id = " . $templateObject->parent_id);
             $db->setQuery($query);
             $templateParentObject = $db->loadObject();
             $this->setError(JText::_("COM_JUDOWNLOAD_PARENT_STYLE_DOES_NOT_EXIST") . $templateParentObject->title);
             return false;
         }
         if ($table->parent_id != $data['parent_id'] || $data['id'] == 0) {
             $table->setLocation($data['parent_id'], 'last-child');
         }
         if (!$table->bind($data)) {
             $this->setError($table->getError());
             return false;
         }
         $this->prepareTable($table);
         if (!$table->check()) {
             $this->setError($table->getError());
             return false;
         }
         $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $isNew));
         if (in_array(false, $result, true)) {
             $this->setError($table->getError());
             return false;
         }
         if (!$table->store()) {
             $this->setError($table->getError());
             return false;
         }
         if ($table->home) {
             $query = $db->getQuery(true);
             $query->update("#__judownload_template_styles");
             $query->set("home = 0");
             $query->where("home = " . $db->quote($table->home));
             $query->where("id != " . $table->id);
             $db->setQuery($query);
             $db->execute();
         }
         $query = $db->getQuery(true);
         $query->select('id');
         $query->from('#__judownload_categories');
         $query->where('style_id =' . $table->id);
         $db->setQuery($query);
         $categoryArrayAssignedToStyle = $db->loadColumn();
         $categoryArrayRemovedStyle = array_diff($categoryArrayAssignedToStyle, $categoryArrayAddToStyle);
         if (!empty($categoryArrayRemovedStyle)) {
             foreach ($categoryArrayRemovedStyle as $categoryIdRemovedStyle) {
                 $query = $db->getQuery(true);
                 $query->update('#__judownload_categories');
                 $query->set('style_id = -2');
                 if ($defaultTemplateStyleObject->template_id != $table->template_id) {
                     $query->set('template_params = ""');
                 }
                 $query->where('parent_id = 0');
                 $query->where('id = ' . $categoryIdRemovedStyle);
                 $db->setQuery($query);
                 $db->execute();
                 $query = $db->getQuery(true);
                 $query->update('#__judownload_categories');
                 $query->set('style_id = -1');
                 $query->where('parent_id != 0');
                 $query->where('id = ' . $categoryIdRemovedStyle);
                 $db->setQuery($query);
                 $db->execute();
             }
             foreach ($categoryArrayRemovedStyle as $categoryIdRemovedStyle) {
                 $templateStyleOfCategoryObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($categoryIdRemovedStyle);
                 if ($templateStyleOfCategoryObject->template_id != $table->template_id) {
                     $query = $db->getQuery(true);
                     $query->update('#__judownload_categories');
                     $query->set('template_params = ""');
                     $query->where('style_id = -1');
                     $query->where('parent_id != 0');
                     $query->where('id = ' . $categoryIdRemovedStyle);
                     $db->setQuery($query);
                     $db->execute();
                     JUDownloadFrontHelperTemplate::removeTemplateParamsOfInheritedStyleCatDoc($categoryIdRemovedStyle);
                 }
             }
         }
         if (!empty($categoryArrayAddToStyle)) {
             foreach ($categoryArrayAddToStyle as $categoryIdAddToStyle) {
                 $query = $db->getQuery(true);
                 $query->update('#__judownload_categories');
                 $query->set('style_id = ' . $table->id);
                 $query->where('style_id != ' . $table->id);
                 $query->where('id = ' . $categoryIdAddToStyle);
                 $db->setQuery($query);
                 $db->execute();
                 JUDownloadFrontHelperTemplate::removeTemplateParamsOfInheritedStyleCatDoc($categoryIdAddToStyle);
             }
         }
         $this->cleanCache();
         $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $isNew));
     } catch (Exception $e) {
         $this->setError($e->getMessage());
         return false;
     }
     $pkName = $table->getKeyName();
     if (isset($table->{$pkName})) {
         $this->setState($this->getName() . '.id', $table->{$pkName});
     }
     $this->setState($this->getName() . '.new', $isNew);
     return true;
 }