Пример #1
0
 public function docopy()
 {
     // check for request forgeries
     YRequest::checkToken() or jexit('Invalid Token');
     // init vars
     $now = JFactory::getDate();
     $post = YRequest::get('post');
     $cid = YRequest::getArray('cid', array(), 'int');
     if (count($cid) < 1) {
         JError::raiseError(500, JText::_('Select a item to copy'));
     }
     try {
         // get item table
         $item_table = YTable::getInstance('item');
         $tag_table = YTable::getInstance('tag');
         // get database
         $db = YDatabase::getInstance();
         // copy items
         foreach ($cid as $id) {
             // get item
             $item = $item_table->get($id);
             $elements = $item->getElements();
             $categories = $item->getRelatedCategoryIds();
             // copy item
             $item->id = 0;
             // set id to 0, to force new item
             $item->name .= ' (' . JText::_('Copy') . ')';
             // set copied name
             $item->alias = ItemHelper::getUniqueAlias($id, $item->alias . '-copy');
             // set copied alias
             $item->state = 0;
             // unpublish item
             $item->created = $now->toMySQL();
             $item->created_by = $this->user->get('id');
             $item->modified = $now->toMySQL();
             $item->modified_by = $this->user->get('id');
             $item->hits = 0;
             // copy tags
             $item->setTags($tag_table->getItemTags($id));
             // save copied item/element data
             $item_table->save($item);
             // save category relations
             CategoryHelper::saveCategoryItemRelations($item->id, $categories);
         }
         // set redirect message
         $msg = JText::_('Item Copied');
     } catch (YException $e) {
         // raise notice on exception
         JError::raiseNotice(0, JText::_('Error Copying Item') . ' (' . $e . ')');
         $msg = null;
     }
     $this->setRedirect($this->baseurl, $msg);
 }
Пример #2
0
 public static function importCSV($file, $type = '', $contains_headers = false, $field_separator = ',', $field_enclosure = '"', $element_assignment = array())
 {
     // get application
     if ($application = Zoo::getApplication()) {
         if ($type_obj = $application->getType($type)) {
             $c = 0;
             $assignments = array();
             foreach ($element_assignment as $column => $value) {
                 if (!empty($value[$type])) {
                     $name = $value[$type];
                     $assignments[$name][] = $column;
                 }
             }
             if (!isset($assignments['_name'])) {
                 throw new ImportHelperException('No item name was assigned.');
             }
             if (($handle = fopen($file, "r")) !== FALSE) {
                 $item_table = YTable::getInstance('item');
                 $category_table = YTable::getInstance('category');
                 $user_id = JFactory::getUser()->get('id');
                 $now = JFactory::getDate();
                 $row = 0;
                 $app_categories = $application->getCategories();
                 $app_categories = array_map(create_function('$cat', 'return $cat->name;'), $app_categories);
                 $elements = $type_obj->getElements();
                 while (($data = fgetcsv($handle, 0, $field_separator, $field_enclosure)) !== FALSE) {
                     if (!($contains_headers && $row == 0)) {
                         $item = new Item();
                         $item->application_id = $application->id;
                         $item->type = $type;
                         // store created by
                         $item->created_by = $user_id;
                         // set created
                         $item->created = $now->toMySQL();
                         // store modified_by
                         $item->modified_by = $user_id;
                         // set modified
                         $item->modified = $now->toMySQL();
                         // store element_data and item name
                         $item_categories = array();
                         foreach ($assignments as $assignment => $columns) {
                             $column = current($columns);
                             switch ($assignment) {
                                 case '_name':
                                     $item->name = $data[$column];
                                     break;
                                 case '_created_by_alias':
                                     $item->created_by_alias = $data[$column];
                                     break;
                                 case '_created':
                                     if (!empty($data[$column])) {
                                         $item->created = $data[$column];
                                     }
                                     break;
                                 default:
                                     if (substr($assignment, 0, 9) == '_category') {
                                         foreach ($columns as $column) {
                                             $item_categories[] = $data[$column];
                                         }
                                     } else {
                                         if (isset($elements[$assignment])) {
                                             $elements[$assignment]->unsetData();
                                             switch ($elements[$assignment]->getElementType()) {
                                                 case 'text':
                                                 case 'textarea':
                                                 case 'link':
                                                 case 'email':
                                                 case 'date':
                                                     $element_data = array();
                                                     foreach ($columns as $column) {
                                                         if (!empty($data[$column])) {
                                                             $element_data[$column] = array('value' => $data[$column]);
                                                         }
                                                     }
                                                     $elements[$assignment]->bindData($element_data);
                                                     break;
                                                 case 'gallery':
                                                     $data[$column] = trim($data[$column], '/\\');
                                                     $elements[$assignment]->bindData(array('value' => $data[$column]));
                                                     break;
                                                 case 'image':
                                                 case 'download':
                                                     $elements[$assignment]->bindData(array('file' => $data[$column]));
                                                     break;
                                                 case 'googlemaps':
                                                     $elements[$assignment]->bindData(array('location' => $data[$column]));
                                                     break;
                                             }
                                         }
                                     }
                                     break;
                             }
                         }
                         $elements_string = '<?xml version="1.0" encoding="UTF-8"?><elements>';
                         foreach ($elements as $element) {
                             $elements_string .= $element->toXML();
                         }
                         $elements_string .= '</elements>';
                         $item->elements = $elements_string;
                         $item->alias = YString::sluggify($item->name);
                         if (empty($item->alias)) {
                             $item->alias = '42';
                         }
                         // set a valid category alias
                         while (ItemHelper::checkAliasExists($item->alias)) {
                             $item->alias .= '-2';
                         }
                         if (!empty($item->name)) {
                             try {
                                 $item_table->save($item);
                                 $item_id = $item->id;
                                 $item->unsetElementData();
                                 // store categories
                                 foreach ($item_categories as $category_name) {
                                     if (!in_array($category_name, $app_categories)) {
                                         $category = new Category();
                                         $category->application_id = $application->id;
                                         $category->name = $category_name;
                                         $category->parent = 0;
                                         $category->alias = YString::sluggify($category_name);
                                         // set a valid category alias
                                         while (CategoryHelper::checkAliasExists($category->alias)) {
                                             $category->alias .= '-2';
                                         }
                                         try {
                                             $category_table->save($category);
                                             $related_categories[$category->id] = $category->name;
                                             $app_categories[$category->id] = $category->name;
                                         } catch (CategoryTableException $e) {
                                         }
                                     } else {
                                         $related_categories = array_filter($app_categories, create_function('$cat', 'return $cat=="' . $category_name . '";'));
                                     }
                                     // add category to item relations
                                     if (!empty($related_categories)) {
                                         CategoryHelper::saveCategoryItemRelations($item_id, array_keys($related_categories));
                                     }
                                 }
                             } catch (ItemTableException $e) {
                             }
                         }
                     }
                     $row++;
                 }
                 fclose($handle);
                 return true;
             } else {
                 throw new ImportHelperException('Could not open csv file.');
             }
         } else {
             throw new ImportHelperException('Could not find type.');
         }
     }
     throw new ImportHelperException('No application to import too.');
 }
Пример #3
0
 public function save()
 {
     // check for request forgeries
     YRequest::checkToken() or jexit('Invalid Token');
     // init vars
     $post = JRequest::get('post');
     $db = YDatabase::getInstance();
     $tzoffset = JFactory::getConfig()->getValue('config.offset');
     $now = JFactory::getDate();
     $now->setOffset($tzoffset);
     $msg = '';
     try {
         $this->_init();
         // is this an item edit?
         $edit = (int) $this->item->id;
         // is current user the item owner and does the user have sufficient user rights
         if ($edit && (!$this->item->canAccess($this->user) || $this->item->created_by != $this->user->id)) {
             throw new YControllerException('You are not allowed to make changes to this item.');
         }
         // get default category - only in none trusted mode
         $categories = array();
         if (!$this->submission->isInTrustedMode() && ($category = $this->submission->getForm($this->type->id)->get('category'))) {
             $categories[] = $category;
         }
         // get element data from post
         if (isset($post['elements'])) {
             // filter element data
             if (!$this->submission->isInTrustedMode() && !UserHelper::isJoomlaAdmin($this->user)) {
                 JRequest::setVar('elements', SubmissionHelper::filterData($post['elements']));
                 $post = JRequest::get('post');
             }
             // merge elements into post
             $post = array_merge($post, $post['elements']);
         }
         // fix publishing dates in trusted mode
         if ($this->submission->isInTrustedMode()) {
             // set publish up date
             if (isset($post['publish_up'])) {
                 if (empty($post['publish_up'])) {
                     $post['publish_up'] = $now->toMySQL(true);
                 }
             }
             // set publish down date
             if (isset($post['publish_down'])) {
                 if (trim($post['publish_down']) == JText::_('Never') || trim($post['publish_down']) == '') {
                     $post['publish_down'] = $db->getNullDate();
                 }
             }
         }
         // sanatize tags
         if (!isset($post['tags'])) {
             $post['tags'] = array();
         }
         // build new item form and bind it with post data
         $form = new ItemForm(array('submission' => $this->submission, 'item' => $this->item, 'elements_config' => $this->elements_config));
         $form->bind($post);
         // save item if form is valid
         if ($form->isValid()) {
             // set name
             $this->item->name = $form->getValue('name');
             // bind elements
             foreach ($this->elements_config as $data) {
                 if (($element = $this->item->getElement($data->element)) && ($field = $form->getFormField($data->element))) {
                     if ($field_data = $field->hasError() ? $field->getTaintedValue() : $field->getValue()) {
                         $element->bindData($field_data);
                     } else {
                         $element->bindData();
                     }
                     // perform submission uploads
                     if ($element instanceof iSubmissionUpload) {
                         $element->doUpload();
                     }
                 }
             }
             // set alias
             $this->item->alias = ItemHelper::getUniqueAlias($this->item->id, YString::sluggify($this->item->name));
             // set modified
             $this->item->modified = $now->toMySQL();
             $this->item->modified_by = $this->user->get('id');
             // creating new item
             if (!$edit) {
                 // set state
                 $this->item->state = 0;
                 // set created date
                 $this->item->created = $now->toMySQL();
                 $this->item->created_by = $this->user->get('id');
                 $this->item->created_by_alias = '';
                 // set publish up - publish down
                 $this->item->publish_up = $now->toMySQL();
                 $this->item->publish_down = $db->getNullDate();
                 // set access
                 $this->item->access = 0;
                 // set searchable
                 $this->item->searchable = 1;
             }
             if ($this->submission->isInTrustedMode()) {
                 // set state
                 $this->item->state = $form->getValue('state');
                 // set publish up
                 if (($publish_up = $form->getValue('publish_up')) && !empty($publish_up)) {
                     $date = JFactory::getDate($publish_up, $tzoffset);
                     $publish_up = $date->toMySQL();
                 }
                 $this->item->publish_up = $publish_up;
                 // set publish down
                 if (($publish_down = $form->getValue('publish_down')) && !empty($publish_down) && !($publish_down == $db->getNullDate())) {
                     $date = JFactory::getDate($publish_down, $tzoffset);
                     $publish_down = $date->toMySQL();
                 }
                 $this->item->publish_down = $publish_down;
                 // set searchable
                 $this->item->searchable = $form->getValue('searchable');
                 // set comments enabled
                 $this->item->params = $this->item->getParams()->set('config.enable_comments', $form->getValue('enable_comments'))->toString();
                 // set frontpage
                 if ($form->getValue('frontpage')) {
                     $categories[] = 0;
                 }
                 // set categories
                 $tmp_categories = $form->getValue('categories');
                 if (!empty($tmp_categories)) {
                     foreach ($form->getValue('categories') as $category) {
                         $categories[] = $category;
                     }
                 }
                 // set tags
                 $tags = $form->hasError('tags') ? $form->getTaintedValue('tags') : $form->getValue('tags');
                 $this->item->setTags($tags);
             } else {
                 // spam protection - user may only submit items every SubmissionController::TIME_BETWEEN_PUBLIC_SUBMISSIONS seconds
                 if (!$edit) {
                     $timestamp = $this->session->get('ZOO_LAST_SUBMISSION_TIMESTAMP');
                     $now = time();
                     if ($now < $timestamp + SubmissionController::TIME_BETWEEN_PUBLIC_SUBMISSIONS) {
                         throw new SubmissionControllerException('You are submitting to fast, please try again in a few moments.');
                     }
                     $this->session->set('ZOO_LAST_SUBMISSION_TIMESTAMP', $now);
                 }
             }
             // save item
             YTable::getInstance('item')->save($this->item);
             // save category relations - only if editing in trusted mode
             if (!$edit || $this->submission->isInTrustedMode()) {
                 CategoryHelper::saveCategoryItemRelations($this->item->id, $categories);
             }
             // set redirect message
             $msg = $this->submission->isInTrustedMode() ? JText::_('Thanks for your submission.') : JText::_('Thanks for your submission. It will be reviewed before being posted on the site.');
             // add form to session if form is not valid
         } else {
             $this->addFormToSession($form);
         }
     } catch (SubmissionControllerException $e) {
         // raise warning on exception
         JError::raiseWarning(0, (string) $e);
     } catch (YException $e) {
         // raise warning on exception
         JError::raiseWarning(0, JText::_('There was an error saving your submission, please try again later.'));
         // add exception details, for super administrators only
         if ($this->user->superadmin) {
             JError::raiseWarning(0, (string) $e);
         }
     }
     // redirect to mysubmissions
     if ($this->redirect == 'mysubmissions' && $form && $form->isValid()) {
         $link = RouteHelper::getMySubmissionsRoute($this->submission);
         // redirect to edit form
     } else {
         $link = RouteHelper::getSubmissionRoute($this->submission, $this->type->id, $this->hash, $this->item_id, $this->redirect);
     }
     $link = JRoute::_($link, false);
     $this->setRedirect($link, $msg);
 }