<commit_before><?php namespace pceuropa\forms;
#Copyright (c) 2016-2017 <NAME> pceuropa.net
use Yii;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

use pceuropa\forms\Form;
use pceuropa\forms\FormBase;
use pceuropa\forms\FormBuilder;
use pceuropa\forms\models\FormModel;

class FormBuilder extends \yii\base\Widget {
    
	public $test_mode = false;
	public $easy_mode = true; 	// if false more options
	public $config = [];		// from widget
	private $options = [];		// to js file
	
	public $table_prefix;
	public $model;				// model active record 
	public $success = false;  	// for response
		
	public function init() {
		parent::init();
		
		$this->registerTranslations();
		$this->model = new FormModel();
		
        $this->options = [
			'easy_mode' => $this->easy_mode,
			'test_mode' => $this->test_mode,
			'update' => false,
			'config' =>  $this->config
		];
	}

	public function run() {
		return $this->formBuilderRender();
	}
	
	public function load($data) {
	
		$this->model->body = $data['form_data'];
		$this->model->title = (isset($data['title'])) ? $data['title'] : null;
		$this->model->url = (isset($data['url'])) ? $data['url'] : null; 
		$this->model->meta_title = (isset($data['title'])) ? $data['title'] : null; 
	
	}
	
	public function findModel($id) {
		$this->model =  $this->model->findModel($id);
	}
	
	
    public function save() {
    
   		if (!($this->success = $this->model->save())){
   			 return $this->success = $this->model->getFirstErrors();
   		} else {
   			return true;
   		}
   		
	}
	
    protected function tableShema(){
		$table_shema = Json::decode($this->model->body)['body'];
    	return FormBase::tableShema($table_shema);
    }
    
    
	public function createTable() {
		if ($this->success === true){
			$table_name = $this->table_prefix . $this->model->getPrimaryKey();
			$query = Yii::$app->db->createCommand()->createTable($table_name, $this->tableShema(), 'CHARACTER SET utf8 COLLATE utf8_general_ci'); 

			try {
			   $query->execute();
			   $this->success = true;
			} catch (\Exception $e) {
			   $this->success = $e->errorInfo[2];
			}
		}
	}
	
	
	// sprawdzic zapisywanie
	public function addColumn($field = false) { // type
		
		if (isset($field['name'])){
			$column = $field['name'];
			//$this->model->body = $field['body'];
			$type = FormBase::getColumnType($field);
		
        	$query = Yii::$app->db->createCommand()->addColumn($this->table_prefix, $column, $type ); 
        
		    try {
		       if ($query->execute()){
				   	//$this->save();
				   	return $this->success = true;
			   }
		       
		    } catch (\Exception $e) {
		       return $this->success = $e->errorInfo[2];
		    }
		}
		
	}
	
	public function renameColumn($data) {
		
		if ( !isset($data['old']) && !isset($data['new']) && $data['old'] === $data['new'] ){
			return $this->success = false;
		}
        	$query = Yii::$app->db->createCommand()->renameColumn( $this->table_prefix, $data['old'],$data['new']); 
        
       	try {
	       	$query->execute();
	       	return $this->success = true;
	    } catch (\Exception $e) {
	       	return $this->success = $e->errorInfo[2];
	    }
	}
	
	public function dropColumn($column) {
		
       $query = Yii::$app->db->createCommand()->dropColumn($this->table_prefix, $column); 
        
       try {
	       	$query->execute();
	       	return $this->success = true;
	    } catch (\Exception $e) {
	       	return $this->success = $e->errorInfo[2];
	    }
	}
	
	
	
	public function response($format = 'json') {
	
		\Yii::$app->response->format = $format;
		return ['success' => $this->success, 'url' => Url::to(['index'])];
	}
	<commit_msg>change variable name prefix table<commit_after><?php namespace pceuropa\forms;
#Copyright (c) 2016-2017 <NAME> pceuropa.net
use Yii;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

use pceuropa\forms\Form;
use pceuropa\forms\FormBase;
use pceuropa\forms\FormBuilder;
use pceuropa\forms\models\FormModel;

class FormBuilder extends \yii\base\Widget {
    
	public $test_mode = false;
	public $easy_mode = true; 	// if false more options
	public $config = [];		// from widget
	private $options = [];		// to js file
	
	public $table;
	public $model;				// model active record 
	public $success = false;  	// for response
		
	public function init() {
		parent::init();
		
		$this->registerTranslations();
		$this->model = new FormModel();
		
        $this->options = [
			'easy_mode' => $this->easy_mode,
			'test_mode' => $this->test_mode,
			'update' => false,
			'config' =>  $this->config
		];
	}

	public function run() {
		return $this->formBuilderRender();
	}
	
	public function load($data) {
	
		$this->model->body = $data['form_data'];
		$this->model->title = (isset($data['title'])) ? $data['title'] : null;
		$this->model->url = (isset($data['url'])) ? $data['url'] : null; 
		$this->model->meta_title = (isset($data['title'])) ? $data['title'] : null; 
	
	}
	
	public function findModel($id) {
		$this->model =  $this->model->findModel($id);
	}
	
	
    public function save() {
    
   		if (!($this->success = $this->model->save())){
   			 return $this->success = $this->model->getFirstErrors();
   		} else {
   			return true;
   		}
   		
	}
	
    protected function tableShema(){
		$table_shema = Json::decode($this->model->body)['body'];
    	return FormBase::tableShema($table_shema);
    }
    
    
	public function createTable() {
		if ($this->success === true){
			$table_name = $this->table . $this->model->getPrimaryKey();
			$query = Yii::$app->db->createCommand()->createTable($table_name, $this->tableShema(), 'CHARACTER SET utf8 COLLATE utf8_general_ci'); 

			try {
			   $query->execute();
			   $this->success = true;
			} catch (\Exception $e) {
			   $this->success = $e->errorInfo[2];
			}
		}
	}
	
	
	// sprawdzic zapisywanie
	public function addColumn($field = false) { // type
		
		if (isset($field['name'])){
			$column = $field['name'];
			//$this->model->body = $field['body'];
			$type = FormBase::getColumnType($field);
		
        	$query = Yii::$app->db->createCommand()->addColumn($this->table, $column, $type ); 
        
		    try {
		       if ($query->execute()){
				   	//$this->save();
				   	return $this->success = true;
			   }
		       
		    } catch (\Exception $e) {
		       return $this->success = $e->errorInfo[2];
		    }
		}
		
	}
	
	public function renameColumn($data) {
		
		if ( !isset($data['old']) && !isset($data['new']) && $data['old'] === $data['new'] ){
			return $this->success = false;
		}
        	$query = Yii::$app->db->createCommand()->renameColumn( $this->table, $data['old'],$data['new']); 
        
       	try {
	       	$query->execute();
	       	return $this->success = true;
	    } catch (\Exception $e) {
	       	return $this->success = $e->errorInfo[2];
	    }
	}
	
	public function dropColumn($column) {
		
       $query = Yii::$app->db->createCommand()->dropColumn($this->table, $column); 
        
       try {
	       	$query->execute();
	       	return $this->success = true;
	    } catch (\Exception $e) {
	       	return $this->success = $e->errorInfo[2];
	    }
	}
	
	
	
	public function response($format = 'json') {
	
		\Yii::$app->response->format = $format;
		return ['success' => $this->success, 'url' => Url::to(['index'])];
	}
	
{'fraction_non_alphanumeric': 0.19329442106692005, 'fraction_numerical': 0.0038007329985068547, 'mean_word_length': 2.863660199265863, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4689583', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2495, 'n_words': 476}