コード例 #1
0
ファイル: SortableUploadField.php プロジェクト: vinstah/body
 public function saveInto(DataObjectInterface $record)
 {
     $isNew = !$record->exists();
     parent::saveInto($record);
     // if we're dealing with an unsaved record, we have to rebuild the relation list
     // with the proper meny_many_extraFields attributes (eg. the sort order)
     if ($isNew) {
         // we have to grab the raw post data as the data is in the right order there.
         // this is kind of a hack, but we simply lack the information about the client-side sorting otherwise
         if (isset($_POST[$this->name]) && isset($_POST[$this->name]['Files']) && is_array($_POST[$this->name]['Files'])) {
             $idList = $_POST[$this->name]['Files'];
         } else {
             // take the ItemIDs as a fallback
             $idList = $this->getItemIDs();
         }
         $sortColumn = $this->getSortColumn();
         $relationName = $this->getName();
         if ($relationName && $record->many_many($relationName) !== null && ($list = $record->{$relationName}())) {
             $arrayList = $list->toArray();
             foreach ($arrayList as $item) {
                 $list->remove($item);
                 $list->add($item, array($sortColumn => array_search($item->ID, $idList) + 1));
             }
         }
     }
 }
コード例 #2
0
 public function saveInto(DataObjectInterface $record)
 {
     $isNew = !$record->exists();
     parent::saveInto($record);
     // if we're dealing with an unsaved record, we have to rebuild the relation list
     // with the proper many_many_extraFields attributes (eg. the sort order)
     if ($isNew) {
         // we have to grab the raw post data as the data is in the right order there.
         // this is kind of a hack, but we simply lack the information about the client-side sorting otherwise
         if (isset($_POST[$this->name]) && isset($_POST[$this->name]['Files']) && is_array($_POST[$this->name]['Files'])) {
             $idList = $_POST[$this->name]['Files'];
         } else {
             // take the ItemIDs as a fallback
             $idList = $this->getItemIDs();
         }
         // Get general info about this relation (if possible)
         $sortColumn = $this->getSortColumn();
         $relationName = $this->getName();
         if ($relationName) {
             // Get relation type.
             $isManyMany = $record->many_many($relationName) !== null;
             // Using "null" here since we know it will properly return null if there is no many_many component by this name.
             $isHasMany = (bool) $record->has_many($relationName);
             // TODO: Return to more strict check of === null here when SS framework issue is mainstream: https://github.com/silverstripe/silverstripe-framework/pull/4130
             if ($isManyMany || $isHasMany) {
                 // Get list from that relation and begin to manipulate it.
                 $list = $record->{$relationName}();
                 $arrayList = $list->toArray();
                 foreach ($arrayList as $item) {
                     // Get the specified sort order for this item (offset from posted 0 index).
                     $sortOrder = array_search($item->ID, $idList) + 1;
                     // The method by which we update this relation varies depending on the relationship type...
                     if ($isManyMany) {
                         // Alter data in the pivot table.
                         $list->remove($item);
                         $list->add($item, array($sortColumn => $sortOrder));
                     } elseif ($isHasMany) {
                         // Just update information in the sort field directly on the item itself.
                         $item->{$sortColumn} = $sortOrder;
                         $item->write();
                     }
                 }
             }
         }
     }
 }
 /**
  * Write an ElementContent block with $Content.
  *
  * @return boolean
  */
 public function setElementalContent(DataObjectInterface $record, $post_content)
 {
     if (!isset($this->_classes_using_wordpress_extension['ElementContent'])) {
         throw new WordpressImportException('Must put "WordpressImportDataExtension" on BaseElement.');
     }
     if (!isset($this->_classes_using_elemental[$record->class])) {
         if ($record instanceof SiteTree) {
             throw new WordpressImportException('Must put "WordpressImportDataExtension" on SiteTree.');
         } else {
             throw new WordpressImportException('Must put "WordpressImportDataExtension" on ' . $record->class . '.');
         }
     }
     $post_content = trim($this->_db->process('post_content', $post_content));
     $elementBlocks = $record->ElementArea()->Elements();
     if (!$record->exists()) {
         $subRecord = ElementContent::create();
         $subRecord->HTML = $post_content;
         $subRecord->WordpressData = $record->WordpressData;
         $elementBlocks->add($subRecord);
         // note(Jake): when page $record is written, the rest will write into it. 3.2+ at least.
     } else {
         $subRecord = $elementBlocks->filter(array('WordpressID' => $record->WordpressID, 'ClassName' => 'ElementContent'))->first();
         $isNew = false;
         if (!$subRecord) {
             $subRecord = ElementContent::create();
             $isNew = true;
         }
         if ($subRecord->HTML !== $post_content) {
             // Avoid getChangedFields triggering below by checking equality first.
             $subRecord->HTML = $post_content;
         }
         $subRecord->WordpressData = $record->WordpressData;
         $changedFields = $subRecord->getChangedFields(true, DataObject::CHANGE_VALUE);
         if ($changedFields) {
             try {
                 $isPublished = $subRecord->isPublished();
                 $this->writeAndPublishRecord($subRecord);
                 return true;
             } catch (Exception $e) {
                 $this->log($subRecord, 'error', $e, 1);
                 throw $e;
             }
         }
         if ($isNew) {
             $elementBlocks->add($subRecord);
         }
     }
 }