<commit_before><?php
/**
 * Vocabs controller
 * This is the primary controller for the vocabulary module
 * This module is meant as a standalone with all assets, views and models self contained
 * within the applications/vocabs directory
 * @version 1.0
 * @author  <NAME> <<EMAIL>>
 */
class Vocabs extends MX_Controller {

	/**
	 * Index / Home page
	 * Displaying the Home Page
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	function index(){
		// header('Content-Type: text/html; charset=utf-8');
		$this->blade->render('index');
	}

	/**
	 * Viewing a vocabulary by slug
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	public function view() {
		//use test records for now
		$slug = $this->input->get('any');
		if ($slug) {
			$record = $this->vocab->getBySlug($slug);
		}

		//For Development Only
		if (!$record) {
			$test_records = $this->vocab->test_vocabs();
			$record = $test_records[$slug] ? $test_records[$slug] : false;
		}

		if ($record) {
            $vocab = $record->display_array();
            $vocab['current_version'] = $record->current_version();

			$this->blade
				->set('vocab', $vocab)
				->render('vocab');
		} else {
			throw new Exception('No Record found with slug: '.$slug);
		}
	}

    /**
     * Pre viewing a related entity
     * @return view/html
     * @author  <NAME> <<EMAIL>>
     */
    public function related_preview() {

        $related = json_decode($this->input->get('related'),true);
        $v_id = $this->input->get('v_id');
        $vocabs = $this->vocab->getAll();

        $others = array();

        foreach ($vocabs as $vocab) {
            $thevocab=$vocab->display_array();
            if($thevocab['id']!=$v_id){
                // find all other vocabs that this related entity also published
                if($related['type']=='publisher'){
                    if(isset($thevocab['related_entity'])){
                        foreach($thevocab['related_entity'] as $anotherrelated){
                            if($anotherrelated['type']=='publisher'&& $anotherrelated['id']==$related['id']){
                                $others[] = $thevocab;
                            }
                        }
                    }
                }
                // find all other vocabs that this related entity also contributed to
                if($related['type']=='contributor'){
                    if(isset($thevocab['related_entity'])){
                        foreach($thevocab['related_entity'] as $anotherrelated){
                            if($anotherrelated['type']=='contributor'&& $anotherrelated['id']==$related['id']){
                                $others[] = $thevocab;
                            }
                        }
                    }
                }
                //if a related entity of type vocab is known to us then provide a link to it
                if($related['type']=='vocab'){
                    if($related['id']==$thevocab['id']){
                        $others[]=$thevocab;
                    }
                }
            }
        }



        $related['other_vocabs'] = $others;
            $this->blade
                ->set('related', $related)
                ->render('related_preview');

    }

	/**
	 * Search
	 * Displaying the search page
	 * @ignore Not used for now. Home page is a search hybrid
	 * @version 1.0
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	public function search() {
		$this->blade->render('search');
	}

	/**
	 * Adding a vocabulary
	 * Displaying a view for adding a vocabulary
	 * Using the same CMS as edit
	 * @todo  ACL
	 * @return view
	 * @author  <NAME> <<EMAIL>>
	 */	
	public function add() {
		$this->blade
			->set('scripts', array('vocabs_cms'))
			->set('vocab', false)
			->render('cms');
	}

	/**
	 * Edit a vocabulary
	 * Displaying a view for editing a vocabulary
	 * Using the same CMS as add but directed towards a vocabulary
	 * @todo ACL
	 * @param  string $slug slug of the vocabulary, unique for a vocabulary
	 * @return view
	 * @author  <NAME> <<EMAIL>>
	 */
	public function edit($slug=false) {
		if (!$this->user->isLoggedIn()) throw new Exception('User not logged in');
		if (!$slug) throw new Exception('Require a Vocabulary Slug to edit');
		$vocab = $this->vocab->getBySlug($slug);
		//do some checking of vocab here, ACL stuff @todo
		if (!$vocab) throw new Exception('Vocab Slug '.$slug. ' not found');

		$this->blade
			->set('scripts', array('vocabs_cms'))
			->set('vocab', $vocab)
			->render('cms');
	}

	/**
	 * Page Controller
	 * For displaying static pages that belongs to the vocabs module
	 * @author  <NAME> <<EMAIL>>
	 * @param  $slug supported: [help|about|contribute]
	 * @return view
	 */
	public function page($slug) {
		$this->blade->render($slug);
	}

	/**
	 * Primary search functionality
	 * data is obtained from angularjs php input POST
	 * @author  <NAME> <<EMAIL>>
	 * @return json search result
	 */<commit_msg>ensure that the if the vocab exists in draft form that it is presented on edit<commit_after><?php
/**
 * Vocabs controller
 * This is the primary controller for the vocabulary module
 * This module is meant as a standalone with all assets, views and models self contained
 * within the applications/vocabs directory
 * @version 1.0
 * @author  <NAME> <<EMAIL>>
 */
class Vocabs extends MX_Controller {

	/**
	 * Index / Home page
	 * Displaying the Home Page
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	function index(){
		// header('Content-Type: text/html; charset=utf-8');
		$this->blade->render('index');
	}

	/**
	 * Viewing a vocabulary by slug
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	public function view() {
		//use test records for now
		$slug = $this->input->get('any');
		if ($slug) {
			$record = $this->vocab->getBySlug($slug);
		}

		//For Development Only
		if (!$record) {
			$test_records = $this->vocab->test_vocabs();
			$record = $test_records[$slug] ? $test_records[$slug] : false;
		}

		if ($record) {
            $vocab = $record->display_array();
            $vocab['current_version'] = $record->current_version();

			$this->blade
				->set('vocab', $vocab)
				->render('vocab');
		} else {
			throw new Exception('No Record found with slug: '.$slug);
		}
	}

    /**
     * Pre viewing a related entity
     * @return view/html
     * @author  <NAME> <<EMAIL>>
     */
    public function related_preview() {

        $related = json_decode($this->input->get('related'),true);
        $v_id = $this->input->get('v_id');
        $vocabs = $this->vocab->getAll();

        $others = array();

        foreach ($vocabs as $vocab) {
            $thevocab=$vocab->display_array();
            if($thevocab['id']!=$v_id){
                // find all other vocabs that this related entity also published
                if($related['type']=='publisher'){
                    if(isset($thevocab['related_entity'])){
                        foreach($thevocab['related_entity'] as $anotherrelated){
                            if($anotherrelated['type']=='publisher'&& $anotherrelated['id']==$related['id']){
                                $others[] = $thevocab;
                            }
                        }
                    }
                }
                // find all other vocabs that this related entity also contributed to
                if($related['type']=='contributor'){
                    if(isset($thevocab['related_entity'])){
                        foreach($thevocab['related_entity'] as $anotherrelated){
                            if($anotherrelated['type']=='contributor'&& $anotherrelated['id']==$related['id']){
                                $others[] = $thevocab;
                            }
                        }
                    }
                }
                //if a related entity of type vocab is known to us then provide a link to it
                if($related['type']=='vocab'){
                    if($related['id']==$thevocab['id']){
                        $others[]=$thevocab;
                    }
                }
            }
        }



        $related['other_vocabs'] = $others;
            $this->blade
                ->set('related', $related)
                ->render('related_preview');

    }

	/**
	 * Search
	 * Displaying the search page
	 * @ignore Not used for now. Home page is a search hybrid
	 * @version 1.0
	 * @return view/html
	 * @author  <NAME> <<EMAIL>>
	 */
	public function search() {
		$this->blade->render('search');
	}

	/**
	 * Adding a vocabulary
	 * Displaying a view for adding a vocabulary
	 * Using the same CMS as edit
	 * @todo  ACL
	 * @return view
	 * @author  <NAME> <<EMAIL>>
	 */	
	public function add() {
		$this->blade
			->set('scripts', array('vocabs_cms'))
			->set('vocab', false)
			->render('cms');
	}

	/**
	 * Edit a vocabulary
	 * Displaying a view for editing a vocabulary
	 * Using the same CMS as add but directed towards a vocabulary
	 * @todo ACL
	 * @param  string $slug slug of the vocabulary, unique for a vocabulary
	 * @return view
	 * @author  <NAME> <<EMAIL>>
	 */
	public function edit($slug=false) {
		if (!$this->user->isLoggedIn()) throw new Exception('User not logged in');
		if (!$slug) throw new Exception('Require a Vocabulary Slug to edit');
		$vocab = $this->vocab->getBySlug($slug);
        if($vocab->prop['status']=='published') {
            $draft_vocab = $this->vocab->getBySlug($slug.'DRAFT');
            if($draft_vocab) {
                $vocab = $draft_vocab;
            }
        }
		//do some checking of vocab here, ACL stuff @todo
		if (!$vocab) throw new Exception('Vocab Slug '.$slug. ' not found');

		$this->blade
			->set('scripts', array('vocabs_cms'))
			->set('vocab', $vocab)
			->render('cms');
	}

	/**
	 * Page Controller
	 * For displaying static pages that belongs to the vocabs module
	 * @author  <NAME> <<EMAIL>>
	 * @param  $slug supported: [help|about|contribute]
	 * @return view
	 */
	public function page($slug) {
		$this->blade->render($slug);
	}

	/**
	 * Primary search functionality
	 * data is obtained from angularjs php input POST
	 * @author  <NAME> <<EMAIL>>
	 * @return json search result
	 */
{'fraction_non_alphanumeric': 0.15209721677773422, 'fraction_numerical': 0.0009800078400627205, 'mean_word_length': 2.1458076448828605, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 138, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3394438', 'n_tokens_mistral': 3341, 'n_tokens_neox': 3025, 'n_words': 805}