view controllers/extractapp.php @ 97:c1bb174a22f3 extractapp

Topic synchronization with LGServices. Adaption for moving table from Gazetteer to LGService.
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 16 Feb 2016 15:07:43 +0100
parents ce7e933b18b0
children 5bea390e7ca9
line wrap: on
line source

<?php
/* extractapp.php
 * This file is part of Extraction-interface.
 *
 * Extraction-interface is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Extraction-interface is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Extraction-interface.  If not, see <http://www.gnu.org/licenses/>.
*/

/** 
 * Extractapp class is extended from the BaseController class.
 * It sends commands to the ExtractappModel to process or update the ExtractappModel's state. 
 * It can also pass the commands or data (from ExtractappModel) to the associated view (files in views/Extractapp/), 
 * for example views/Extractapp/TaggingText.php , to change the view's presentation.
 * 
 * There are five actions in Extractapp: tagging text, exporting a table,
 * editing the wordlist, editing the taglist, and configurating tags in one topic.
 * Each action is one of the methods in Extractapp.
 * 
*/

class Extractapp extends BaseController {
	

    protected $viewmodel;

       
    public function __construct($action, $urlvalues, $postdata){
        parent::__construct($action, $urlvalues, $postdata); 
        $this->viewmodel = new ExtractappModel();
    }

    protected function TaggingText() {
        /**
         * TaggingText action in Extractapp handles the taging related actions, for example, 
         * tagging itself, saving tag results, loading and saving regular expression for tagging.
         * They are handled by 'func' variable in postdata, which could come from LGService or from extraction-interface application itself.
         *
         * From LGService, the postdata contains essential information about the file/section of local gazetteers data.
         * This happens when user entering to extraction-interface the first time, or after s/he saved her/is tagging results to LGService.
         * (default one, SaveFullTextToLGService, SmartRegexLoad, SmartRegexSave case)
         * 
         * From extraction-interface itself, it contains the information from previous page.
         * This happens when user choosing another topic in the extraction-interface, 
         * the page need to retrieve related information from database for the topic.
         * (ContinueTagging case)
         */

        $viewmodel = $this->viewmodel;
        
        if (isset($this->postdata['func'])) {
            $func = $this->postdata['func'];
        } else {
            $func = "";
        }
        switch ($func) {
            case 'SaveFullText':
                $viewmodel->SaveFullText($this->postdata);          
                $this->ReturnView($viewmodel->StartTagging(), true); 
                break;   
            case 'SaveFullTextToLGService':
                global $AT_LOCAL;
                if ($AT_LOCAL) {
                    $viewmodel->messages['info'] .= "SaveFullTextToLGService! (local test) ";
                    $viewmodel->SetInfoFromPreviousPage($this->postdata);
                    $response = $viewmodel->SaveFullTextToLGService($this->postdata);
                    
                } else {

                    $viewmodel->messages['info'] .= "SaveFullTextToLGService! ";
                    $viewmodel->SetInfoFromPreviousPage($this->postdata);
                    $response = $viewmodel->SaveFullTextToLGService($this->postdata);
                    $viewmodel->UpdateInfoResponsedFromLGService($response);    // update file_id, branch_id, user_id
                    $viewmodel->SetTextFromFileId();     
                }
                $this->ReturnView($viewmodel->StartTagging(), true); 
                break;

            case 'SmartRegexLoad':
                $viewmodel->LoadSmartRegex($this->postdata['topic_id']);
                break;
            case 'SmartRegexSave':
                $viewmodel->SaveSmartRegex($this->postdata);
                break;
            case 'ReloadText':
                $viewmodel->messages['info'] .= "Reload the latest text ";
                $viewmodel->SetInfoFromPreviousPage($this->postdata);
                $viewmodel->ReloadSetting();    // set file_id = current_fileId, current_fileId = 0
                $viewmodel->SetTextFromFileId();
                $this->ReturnView($viewmodel->StartTagging(), true);
                break;

            case 'ContinueTagging':
                $viewmodel->messages['info'] .= "(Countinue tagging) ";
                $viewmodel->SetInfoFromPreviousPage($this->postdata);  
                
                // when new section hasn't been saved to LGServices, no fileId and no branchId
                if ($this->postdata['fileId'] == 0) {
                    $viewmodel->SetTextFromSectionId();  
                } else {
                    $viewmodel->SetTextFromFileId();    // get text by fileId, from LGService
                }
                 
                $viewmodel->SetTopic($this->postdata['topic']);
        
                $this->ReturnView($viewmodel->StartTagging(), true); 
                break;

            case 'CreateNewTopic':
                $result = $viewmodel->CreateNewTopic($this->postdata); 
                if ($result == "error") {
                    $viewmodel->messages['error'] .= "Fail to create the new topic <br> ";
                } else {
                    // then act like ContinueTagging
                    $viewmodel->messages['info'] .= "Created a new topic <br>";
                    $viewmodel->SetInfoFromPreviousPage($result);  

                    // when new section hasn't been saved to LGServices, no fileId and no branchId
                    if ($this->postdata['fileId'] == 0) {
                        $viewmodel->SetTextFromSectionId(); 
                    } else {
                        $viewmodel->SetTextFromFileId();    // get text by fileId, from LGService
                    }
                    $viewmodel->SetTopic($result['topic']);

                }
                $this->ReturnView($viewmodel->StartTagging(), true);  
                
                break;

            default:    
                // This is where the first time user visit extraction interface from LGService
                $viewmodel->messages['info'] .= "Welcome to Extraction Interface. ";

                if (isset($this->postdata['fileId'])) {
                    if ($this->postdata['fileId'] != 0) { // ($this->postdata['branchId'] != 0) {
                        // --- Existing branch case ---
                        $viewmodel->SetInfoFromPreviousPage($this->postdata);
                        $viewmodel->SetBookMetadataBySectionId();
                        // get text by fileId, from LGService
                        $viewmodel->SetTextFromFileId();
                    }

                } else if ($this->postdata['sectionId'] != 0 && $this->postdata['userId'] != 0) {
                    // --- New branch case ---
                    $viewmodel->SetInfoFromPreviousPage($this->postdata);
                    $viewmodel->SetBookMetadataBySectionId();
                    // get text by sectionId from LGService
                    $viewmodel->SetTextFromSectionId();  

                } else if ($this->postdata['sectionId'] != 0) { 
                    $viewmodel->SetTextFromLocal($this->postdata['sectionId']);
                    $viewmodel->SetBookMetadataBySectionId();

                } else if (is_numeric($this->urlvalues['id'])) {
                    // get text from local file system (for development stage only)
                    $viewmodel->SetTextFromLocal($this->urlvalues['id']);
                    $viewmodel->SetBookMetadataBySectionId();
                    $this->ReturnView_localtest($viewmodel->StartTagging(), true); 
                    break;                    

                } else {
                    $viewmodel->messages['error'] .= "wrong url!!";
                    return;
                }

                $this->ReturnView($viewmodel->StartTagging(), true); 
                break;
        }

       
        
    }


    protected function ExportTabDelimited() {
        /**
         * ExportTable action returns "./views/Extractapp/ExportTabDelimited.php" view.
         * ExportTabDelimited.php is using tab as delimiter.
         */
        $viewmodel = $this->viewmodel;

        if (isset($this->postdata['fileId'])) {
            $viewmodel->SetInfoFromPreviousPage($this->postdata);   // fileId in postdata
            
            $viewmodel->SetTextFromFileId();       // this will also set section_id  
            $viewmodel->SetBookMetadataBySectionId();
        }

        $this->ReturnView($viewmodel->ExportTable($this->postdata, true), false);
            
            
    }

    protected function ExportTable() {
        /**
         * ExportTable action returns "./views/Extractapp/ExportTable.php" view.
         */
        $viewmodel = $this->viewmodel;

        if (isset($this->postdata['func'])) {
            $func = $this->postdata['func'];
        } else {
            $func = "";
        }
        switch ($func) {
            case 'exportFromExtractionInterface':
                $viewmodel->SetInfoFromPreviousPage($this->postdata);
    
                $this->ReturnView($viewmodel->ExportTable($this->postdata, false), true);
                break;

            default:    // from file
                // exportFromLGServicesHtmlTable
                if (isset($this->postdata['fileId'])) {
                    $viewmodel->SetInfoFromPreviousPage($this->postdata);   // only fileId in postdata
                    
                    $viewmodel->SetTextFromFileId();       // this will also set section_id  
                    $viewmodel->SetBookMetadataBySectionId();
                }

                $this->ReturnView($viewmodel->ExportTable($this->postdata, true), false);
                break;

        }



    }

    protected function EditWordlist() {
        /**
         * EditWordlist action returns "./views/Extractapp/EditWordlist.php" view, 
         * It adds or saves wordlist based on the 'func' variable in postdata.
         */

        $viewmodel = $this->viewmodel;
        if (isset($this->postdata['func'])) {
            $func = $this->postdata['func'];
        } else {
            $func = "";
        }
        switch ($func) {
            case 'AddNewList':
                $this->ReturnView($viewmodel->AddNewList($this->postdata), true);
                break;
            case 'SaveWordlist':
                $this->ReturnView($viewmodel->SaveWordlist($this->postdata), true);
                break;
            default:    // EditWordlist
                $this->ReturnView($viewmodel->EditWordlist(), true);    
            break;
         } 
    }


    protected function EditTaglist() {
        /**
         * EditTaglist action returns "./views/Extractapp/EditTaglist.php" view.
         * It adds, saves or delete tag in the current topic.
         */

        $viewmodel = $this->viewmodel;
        if (isset($this->postdata['func'])) {
            $func = $this->postdata['func'];
        } else {
            $func = "";
        }
        switch ($func) {
            case 'NewTagElement':
                $this->ReturnView($viewmodel->NewTagElement($this->postdata), true);
                break;
            case 'SaveTagElement':
                $this->ReturnView($viewmodel->SaveTagElement($this->postdata), true);
                break;
            case 'DeleteTag':
                $this->ReturnView($viewmodel->DeleteTag($this->postdata), true);
                break;
            default:    // EditTaglist
                $this->ReturnView($viewmodel->EditTaglist($this->postdata), true);
                break;
        }
    }

 
    protected function ConfigTagsInTopic() {
        /**
         * ConfigTagsInTopic action returns "./views/Extractapp/ConfigTagsInTopic.php" view.
         * It shows or updates the tags-in-topic relation.
         */

        $viewmodel = $this->viewmodel;
        if (isset($this->postdata['func'])) {
            $func = $this->postdata['func'];
        } else {
            $func = "";
        }
        switch ($func) {
            case 'Update':
                $viewmodel->UpdateTagsInTopic($this->postdata);
                break;
            /*
            case 'CreateNewTopic':
                $result = $viewmodel->CreateNewTopic($this->postdata); 
                if ($result == "error") {
                    echo "Fail to create the new topic. Please contact us.";
                } else {
                    $this->ReturnView($viewmodel->ConfigTagsInTopic($result), true);  
                }
                break;
            */
            default:    
                $this->ReturnView($viewmodel->ConfigTagsInTopic($this->postdata), true);  
                break;
        } 
    }


    
	
}


?>