public function save($data, $form)
    {
        $className = $this->config()->get('tree_class');

        // Existing or new record?
        $id = $data['ID'];
        if (substr($id, 0, 3) != 'new') {
            /** @var SiteTree $record */
            $record = DataObject::get_by_id($className, $id);
            // Check edit permissions
            if ($record && !$record->canEdit()) {
                return Security::permissionFailure($this);
            }
            if (!$record || !$record->ID) {
                throw new HTTPResponse_Exception("Bad record ID #$id", 404);
            }
        } else {
            if (!$className::singleton()->canCreate()) {
                return Security::permissionFailure($this);
            }
            $record = $this->getNewItem($id, false);
        }

        // Check publishing permissions
        $doPublish = !empty($data['publish']);
        if ($record && $doPublish && !$record->canPublish()) {
            return Security::permissionFailure($this);
        }

        // TODO Coupling to SiteTree
        $record->HasBrokenLink = 0;
        $record->HasBrokenFile = 0;

        if (!$record->ObsoleteClassName) {
            $record->writeWithoutVersion();
        }

        // Update the class instance if necessary
        if (isset($data['ClassName']) && $data['ClassName'] != $record->ClassName) {
            // Replace $record with a new instance of the new class
            $newClassName = $data['ClassName'];
            $record = $record->newClassInstance($newClassName);
        }

        // save form data into record
        $form->saveInto($record);
        $record->write();

        // If the 'Publish' button was clicked, also publish the page
        if ($doPublish) {
            $record->publishRecursive();
            $message = _t(
                __CLASS__ . '.PUBLISHED',
                "Published '{title}' successfully.",
                ['title' => $record->Title]
            );
        } else {
            $message = _t(
                __CLASS__ . '.SAVED',
                "Saved '{title}' successfully.",
                ['title' => $record->Title]
            );
        }

        $this->getResponse()->addHeader('X-Status', rawurlencode($message));
        return $this->getResponseNegotiator()->respond($this->getRequest());
    }
public function save($data, $form)
    {
        $className = $this->config()->get('tree_class');

        // Existing or new record?
        $id = $data['ID'];
        if (substr($id, 0, 3) != 'new') {
            /** @var SiteTree $record */
            $record = DataObject::get_by_id($className, $id);
            // Check edit permissions
            if ($record && !$record->canEdit()) {
                return Security::permissionFailure($this);
            }
            if (!$record || !$record->ID) {
                throw new HTTPResponse_Exception("Bad record ID #$id", 404);
            }
        } else {
            if (!$className::singleton()->canCreate()) {
                return Security::permissionFailure($this);
            }
            $record = $this->getNewItem($id, false);
        }

        // Check publishing permissions
        $doPublish = !empty($data['publish']);
        if ($record && $doPublish && !$record->canPublish()) {
            return Security::permissionFailure($this);
        }

        // TODO Coupling to SiteTree
        $record->HasBrokenLink = 0;
        $record->HasBrokenFile = 0;

        if (!$record->ObsoleteClassName) {
            $record->writeWithoutVersion();
        }

        // Update the class instance if necessary
        if (isset($data['ClassName']) && $data['ClassName'] != $record->ClassName) {
            // Replace $record with a new instance of the new class
            $newClassName = $data['ClassName'];
            $record = $record->newClassInstance($newClassName);
        }

        // save form data into record
        $form->saveInto($record);
        $record->write();

        // If the 'Publish' button was clicked, also publish the page
        if ($doPublish) {
            $record->publishRecursive();
            $message = _t(
                __CLASS__ . '.PUBLISHED',
                "Published '{title}' successfully.",
                ['title' => $record->Title]
            );
        } else {
            $message = _t(
                __CLASS__ . '.SAVED',
                "Saved '{title}' successfully.",
                ['title' => $record->Title]
            );
        }

        $this->getResponse()->addHeader('X-Status', rawurlencode($message));
        return $this->getResponseNegotiator()->respond($this->getRequest());
    }
public function save ( $ data , $ form ) { $ className = $ this -> config ( ) -> get ( 'tree_class' ) ; // Existing or new record? $ id = $ data [ 'ID' ] ; if ( substr ( $ id , 0 , 3 ) != 'new' ) { /** @var SiteTree $record */ $ record = DataObject :: get_by_id ( $ className , $ id ) ; // Check edit permissions if ( $ record && ! $ record -> canEdit ( ) ) { return Security :: permissionFailure ( $ this ) ; } if ( ! $ record || ! $ record -> ID ) { throw new HTTPResponse_Exception ( "Bad record ID #$id" , 404 ) ; } } else { if ( ! $ className :: singleton ( ) -> canCreate ( ) ) { return Security :: permissionFailure ( $ this ) ; } $ record = $ this -> getNewItem ( $ id , false ) ; } // Check publishing permissions $ doPublish = ! empty ( $ data [ 'publish' ] ) ; if ( $ record && $ doPublish && ! $ record -> canPublish ( ) ) { return Security :: permissionFailure ( $ this ) ; } // TODO Coupling to SiteTree $ record -> HasBrokenLink = 0 ; $ record -> HasBrokenFile = 0 ; if ( ! $ record -> ObsoleteClassName ) { $ record -> writeWithoutVersion ( ) ; } // Update the class instance if necessary if ( isset ( $ data [ 'ClassName' ] ) && $ data [ 'ClassName' ] != $ record -> ClassName ) { // Replace $record with a new instance of the new class $ newClassName = $ data [ 'ClassName' ] ; $ record = $ record -> newClassInstance ( $ newClassName ) ; } // save form data into record $ form -> saveInto ( $ record ) ; $ record -> write ( ) ; // If the 'Publish' button was clicked, also publish the page if ( $ doPublish ) { $ record -> publishRecursive ( ) ; $ message = _t ( __CLASS__ . '.PUBLISHED' , "Published '{title}' successfully." , [ 'title' => $ record -> Title ] ) ; } else { $ message = _t ( __CLASS__ . '.SAVED' , "Saved '{title}' successfully." , [ 'title' => $ record -> Title ] ) ; } $ this -> getResponse ( ) -> addHeader ( 'X-Status' , rawurlencode ( $ message ) ) ; return $ this -> getResponseNegotiator ( ) -> respond ( $ this -> getRequest ( ) ) ; }
Save and Publish page handler

@param array $data
@param Form $form
@return HTTPResponse
@throws HTTPResponse_Exception
Save and Publish page handler