Source of file TableFilterSortServerSaver_Controller.php
Size: 7,871 Bytes - Last Modified: 2017-03-06T02:33:39+00:00
/var/www/docs.ssmods.com/process/src/code/control/TableFilterSortServerSaver_Controller.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 | <?php class TableFilterSortServerSaver_Controller extends Controller { /** * Default URL handlers - (Action)/(ID)/(OtherID). */ private static $url_segment = 'tfs'; /** * Default URL handlers - (Action)/(ID)/(OtherID). */ private static $url_handlers = array( '$Action//$ID/$OtherID/$Version' => 'handleAction', ); /** * Defines methods that can be called directly * @var array */ private static $allowed_actions = array( 'index' => true, 'find' => true, 'start' => true, 'save' => true, 'AddForm' => true, 'dosave' => true, 'load' => true ); protected $parentPageID = null; /** * @param string $action * * @return string (Link) */ public function Link($action = null) { return self::create_link($action); } /** * returns ABSOLUTE link to the shopping cart controller. * @param null | array | string $actionAndOtherLinkVariables * @return string */ protected static function create_link($actionAndOtherLinkVariables = null) { return Controller::join_links( Director::baseURL(), Config::inst()->get(get_called_class(), 'url_segment'), $actionAndOtherLinkVariables ); } function init() { parent::init(); $this->parentPageID = Convert::raw2sql($this->request->param('ID')); Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js'); TableFilterSortAPI::include_requirements(); Requirements::customScript(' jQuery(document).ready( function() { jQuery(\'body\').on( \'click\', \'a.load\', function(event) { event.preventDefault(); var url = jQuery(this).attr("href"); window.top.history.pushState(null, null, url); window.top.jQuery.modal.close(); return false; } ); jQuery(\'a.load\').each( function(i, el) { var myEl = jQuery(el); var baseURL = window.top.location.protocol + "//" + window.top.location.host + window.top.location.pathname; var url = baseURL + "?load=" + jQuery(myEl).attr("href"); jQuery(myEl).attr("href", url); } ); } ) '); } function Title() { if($this->myTitle) { return $this->myTitle; } else { return $this->request->param('Action'); } } function index($request) { $postData = $request->postVars(); if($postData) { //just in case ... Session::clear('TableFilterSortPostData'); if(isset($postData['ParentPageID'])) { $this->parentPageID = $postData['ParentPageID']; return Director::absoluteURL($this->Link('find/'.$this->parentPageID)); } } return '404'; } function find($request) { $this->myTitle = 'Find ' . $this->parentPageID . ' ... '; return $this->renderWith($this->class); } function start($request) { $postData = $request->postVars(); if($postData) { Session::set('TableFilterSortPostData', $postData); return Director::absoluteURL($this->Link('save')); } return '404'; } function save($request) { if($this->dataToSave()) { $this->myTitle = 'Save ' . $this->parentPageID . ' ... '; return $this->renderWith($this->class); } return 'NO DATA TO BE SAVED ...'; } function dosave($data, $form) { $title = Convert::raw2sql($data['Title']); if($title) { $dataToSave = $this->dataToSave(); if($dataToSave) { $obj = TableFilterSortServerSaver::find_or_create($title, $this->parentPageID); $obj->Data = json_encode($dataToSave); $obj->Description = Convert::raw2sql($data["Description"]); $obj->Author = Convert::raw2sql($data["Author"]); $obj->write(); $tags = array(); foreach($data['TagsTempField'] as $tag) { $tag = trim($tag); if($tag) { $tagObject = TableFilterSortTag::find_or_create($tag, $obj); } } Session::clear('TableFilterSortPostData'); //$form->setMessage('Saved successfully', 'good'); } else { $form->setMessage('An Error Occurred', 'bad'); } } else { $form->setMessage('Please provide title', 'bad'); } return $this->renderWith($this->class); } function load($request) { $this->getResponse()->addHeader('Content-Type', 'application/json'); $urlSegment = Convert::raw2sql($request->param('ID')); $obj = TableFilterSortServerSaver::get() ->filter(array('URLSegment' => $urlSegment)) ->first(); if($obj) { return json_encode( array( 'Data' => $obj->Data ) ); } return '{}'; } /** * returns datalist of options for use in template * @param [type] $request [description] * @return [type] [description] */ function MyList() { return TableFilterSortServerSaver::get()->filter(array('ParentPageID' => $this->parentPageID)); } function AddForm() { if( $this->dataToSave() ) { $singleton = Injector::inst()->get('TableFilterSortServerSaver'); $fieldLabels = $singleton->FieldLabels(); $fieldList = FieldList::create( TextField::create('Title', $fieldLabels['Title']) ->setMaxLength(50) ->setAttribute('placeholder', $fieldLabels['Title']), TextField::create('Author', $fieldLabels['Author']) ->setMaxLength(50) ->setAttribute('placeholder', $fieldLabels['Author']), TextareaField::create('Description', $fieldLabels['Description']) ->setAttribute('placeholder', $fieldLabels['Description']) ); for($i = 1; $i < 8; $i++) { $title = $fieldLabels['Tags']. ' #'.$i; $fieldList->push( TextField::create('TagsTempField['.$i.']', $title) ->setAttribute('placeholder', $title) ); } $actionTitle = _t('TableFilterSortServerSaver_Controller.SAVE','Save'); $actionList = FieldList::create( FormAction::create('dosave', $actionTitle) ); $requireFields = RequiredFields::create(array('Title')); return Form::create($this, 'AddForm', $fieldList, $actionList, $requireFields); } } /** * returns an array with three values: * ParentPageID * Data * * @return null | array */ protected function dataToSave() { $data = session::get('TableFilterSortPostData'); if($data) { if(isset($data["ParentPageID"])) { $this->parentPageID = $data["ParentPageID"]; return $data; } } } } |