<commit_before><?php
namespace api\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\Staff;
use common\components\KLogger;

/**
 * Site controller
 */
class StaffController extends Controller
{
    public $enableCsrfValidation = false;
    private $api_key = '43S4342@342Asfd';
    public $layout = false;
    public $logger;

    public function init()
    {
        $this->logger = new KLogger('api_' . date('Ymd'), KLogger::INFO);
        \Yii::$app->response->format = 'json';
    }


    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        die("HELLO API");
    }

    public function actionAdd()
    {
        $this->logger->LogInfo("actionAdd staff data  :" . json_encode(Yii::$app->request->post()));

        if (Yii::$app->user->isGuest) {
            return ['error_code' => 1, 'message' => 'Not login'];
        }
		
        if ($data = Yii::$app->request->post()) {

            //check card_code existed 
			$card_code = isset($data['card_code']) ? $data['card_code'] : '';
			$company_id = isset($data['company_id']) ? $data['company_id'] : '';
			$staff = \common\models\Staff::getStaffFromCardCode($card_code,$company_id);
			if($staff){
				$return = array(
                    'error_code' => 1,
                    'message' => 'Card code is existed'
                );
			}
			//check card_id existed 
			if(empty($card_code)){
				$att_code = isset($data['att_code]']) ? $data['att_code]'] :isset($data['att_code']) ? $data['att_code']:'';
				$company_id = isset($data['company_id']) ? $data['company_id'] : '';
				$staff = \common\models\Staff::getStaffFromAttCode($att_code,$company_id);
				
			}
			if(!$staff){
				$staff = new Staff();
			}
            $staff->name = isset($data['name']) ? $data['name'] : '';
            $staff->phone = isset($data['phone']) ? $data['phone'] : '';
            $staff->email = isset($data['email']) ? $data['email'] : '';
            $staff->card_code = isset($data['card_code']) ? $data['card_code'] : '';
            $staff->card_id = isset($data['card_id']) ? $data['card_id'] : '';
            $staff->att_code = isset($data['att_code]']) ? $data['att_code]'] :isset($data['att_code']) ? $data['att_code']:'';
			
			
			$department_id = isset($data['department_id']) ? $data['department_id'] : 0;
			$department = \common\models\Department::getDepartmentByLocalId($department_id);
			if($department)
				$department_id = $department->id;
            $staff->department_id = $department_id;
			
            $image = isset($data['image']) ? $data['image'] : '';
            $staff->image = '';
            $staff->created_by = isset($data['created_by']) ? $data['created_by'] : Yii::$app->user->identity->id;
            $staff->company_id = isset($data['company_id']) ? $data['company_id'] : '';
            $staff->description = isset($data['description']) ? $data['description'] : '';
            $staff->created_time = date("Y-m-d H:i:s");
            $staff->order = isset($data['order']) ? $data['order'] : '';
            $save = $staff->save(false);
            if ($save) {
                if ($image) {
                    //upload image
                    $path = Yii::$app->params['images']['staff']['path'] . '/' . $staff->company_id;
                    $option = ['width' => 120, 'height' => 120];
                    \common\components\Common::uploadFile($image, $path, $staff, '.png', $option, true);
                }
                $return = array(
                    'error_code' => 0,<commit_msg>update code<commit_after><?php
namespace api\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\Staff;
use common\components\KLogger;

/**
 * Site controller
 */
class StaffController extends Controller
{
    public $enableCsrfValidation = false;
    private $api_key = '43S4342@342Asfd';
    public $layout = false;
    public $logger;

    public function init()
    {
        $this->logger = new KLogger('api_' . date('Ymd'), KLogger::INFO);
        \Yii::$app->response->format = 'json';
    }


    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        die("HELLO API");
    }

    public function actionAdd()
    {
        $this->logger->LogInfo("actionAdd staff data  :" . json_encode(Yii::$app->request->post()));

        if (Yii::$app->user->isGuest) {
            return ['error_code' => 1, 'message' => 'Not login'];
        }
		
        if ($data = Yii::$app->request->post()) {

            //check card_code existed 
			$card_code = isset($data['card_code']) ? $data['card_code'] : '';
			$company_id = isset($data['company_id']) ? $data['company_id'] : '';
			$staff = \common\models\Staff::getStaffFromCardCode($card_code,$company_id);
			if($staff){
				$return = array(
                    'error_code' => 1,
                    'message' => 'Card code is existed'
                );
			}
			//check card_id existed 
			if(empty($card_code)){
				$att_code = isset($data['att_code]']) ? $data['att_code]'] :'';
				if(!$att_code)
					$att_code = isset($data['att_code']) ? $data['att_code'] :'';
				$company_id = isset($data['company_id']) ? $data['company_id'] : '';
				$staff = \common\models\Staff::getStaffFromAttCode($att_code,$company_id);
				
			}
			if(!$staff){
				$staff = new Staff();
			}
            $staff->name = isset($data['name']) ? $data['name'] : '';
            $staff->phone = isset($data['phone']) ? $data['phone'] : '';
            $staff->email = isset($data['email']) ? $data['email'] : '';
            $staff->card_code = isset($data['card_code']) ? $data['card_code'] : '';
            $staff->card_id = isset($data['card_id']) ? $data['card_id'] : '';
            $staff->att_code = $att_code;
			
			$department_id = isset($data['department_id']) ? $data['department_id'] : 0;
			$department = \common\models\Department::getDepartmentByLocalId($department_id);
			if($department)
				$department_id = $department->id;
            $staff->department_id = $department_id;
			
            $image = isset($data['image']) ? $data['image'] : '';
            $staff->image = '';
            $staff->created_by = isset($data['created_by']) ? $data['created_by'] : Yii::$app->user->identity->id;
            $staff->company_id = isset($data['company_id']) ? $data['company_id'] : '';
            $staff->description = isset($data['description']) ? $data['description'] : '';
            $staff->created_time = date("Y-m-d H:i:s");
            $staff->order = isset($data['order']) ? $data['order'] : '';
            $save = $staff->save(false);
            if ($save) {
                if ($image) {
                    //upload image
                    $path = Yii::$app->params['images']['staff']['path'] . '/' . $staff->company_id;
                    $option = ['width' => 120, 'height' => 120];
                    \common\components\Common::uploadFile($image, $path, $staff, '.png', $option, true);
                }
                $return = array(
                    'error_code' => 0,
{'fraction_non_alphanumeric': 0.19793246037215714, 'fraction_numerical': 0.00523776705720193, 'mean_word_length': 2.5343399902581587, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, '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': '1347129', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2287, 'n_words': 373}