<commit_before><?php

class Controller_api_drivers_shift extends Crunchbutton_Controller_RestAccount {

	public function init() {
		switch ( c::getPagePiece( 3 ) ) {
			case 'community':
				$this->community();
				break;

			case 'driver':
				$this->driver();
				break;

			case 'driver-schedule-sms-config':
				$this->driverScheduleSMSConfig();
				break;

			case 'driver-note-update':
				$this->driverNoteUpdate();
				break;

			case 'driver-orders-per-hour':
				$this->driverOrdersPerHour();
				break;

			default:
				echo json_encode( [ 'error' => 'invalid object' ] );
				break;
		}
	}

	public function driverScheduleSMSConfig(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$value = $this->request()[ 'value' ];
			$value = ( $value && $value > 0 ) ? 1 : 0;
			$admin->setConfig( Crunchbutton_Admin::CONFIG_RECEIVE_DRIVER_SCHEDULE_SMS_WARNING, $value );
			echo json_encode( [ 'success' => 'success' ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverOrdersPerHour(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$orders = $this->request()[ 'orders' ];
			$admin->saveOrdersPerHour( $orders );
			echo json_encode( [ 'success' => true ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverNoteUpdate(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$text = $this->request()[ 'text' ];
			$admin->addNote( $text );
			$note = $admin->note();
			echo json_encode( [ 'success' => $note->exports() ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverAssign(){

		$ids_admin = $this->request()[ 'id_admin' ];
		$id_community_shift = $this->request()[ 'id_community_shift' ];
		$ids_admin_permanently = $this->request()[ 'id_admin_permanently' ];

		$ids_admin = $this->request()[ 'id_admin' ];
		$id_community_shift = $this->request()[ 'id_community_shift' ];
		$ids_admin_permanently = $this->request()[ 'id_admin_permanently' ];

		$to_remove = [];
		$to_remove_permanency = [];
		$to_add = $ids_admin;

		$assigneds = Crunchbutton_Admin_Shift_Assign::q( 'SELECT * FROM admin_shift_assign WHERE id_community_shift = "' . $id_community_shift . '"  ORDER BY id_admin' );
		foreach( $assigneds as $assigned ){

			// about the shift
			if( count( $to_add ) > 0 ){
				$key = array_search( $assigned->id_admin, $to_add );
				if( $key !== false ){
					unset( $to_add[ $key ] );
				} else {
					$to_remove[] = $assigned;
				}
			} else {
				$to_remove[] = $assigned;
			}

			// about permanency
			if( count( $ids_admin_permanently ) > 0 ){
				$key = array_search( $assigned->id_admin, $ids_admin_permanently );
				if( $key === false ){
					if( $assigned->isPermanent() ){
						$to_remove_permanency[] = $assigned;
						// unset( $ids_admin_permanently[ $key ] );
					}
				}
			} else {
				$to_remove_permanency[] = $assigned;
			}
		}

		if( count( $to_remove ) > 0 ){
			foreach( $to_remove as $remove ){
				if( $remove->isPermanent() ){
					// prevent the shift to be added again
					$removed = Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $remove->id_community_shift, $remove->id_admin );
				}
				$remove->delete();
			}
		}

		if( count( $to_remove_permanency ) > 0 ){
			foreach( $to_remove_permanency as $remove ){
				$shift = $remove->shift();

				$id_father = $shift->id_community_shift_father;
				if( !$id_father && $shift->recurring ){
					$id_father = $shift->id_community_shift;
				}

				if( $id_father ){
					$id_admin = $remove->id_admin;
					$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
					// Remove permanency
					Crunchbutton_Admin_Shift_Assign_Permanently::removeByAdminShiftFather( $id_admin, $id_father );

					// Remove next shifts assignments for the permanency
					$remove_assignment_after = ( $now->format( 'YmdHis' ) > $shift->dateEnd()->format( 'YmdHis' ) ? $now : $shift->dateEnd() ) ;

					$assignments = Crunchbutton_Admin_Shift_Assign::q('
						SELECT asa.* FROM admin_shift_assign asa
						INNER JOIN community_shift cs
							ON cs.id_community_shift = asa.id_community_shift
							AND cs.id_community_shift_father = ?
						WHERE cs.date_start > ?
							AND asa.id_admin = ?
							AND asa.warned = false
					', [$id_father, $remove_assignment_after->format( 'Y-m-d' ), $id_admin]);

					foreach( $assignments as $assignment ){
						$assignment->delete();
					}
				}
			}
		}

		if( count( $to_add ) > 0 ){
			foreach( $to_add as $id_admin ){
				Crunchbutton_Admin_Shift_Assign::assignAdminToShift( $id_admin, $id_community_shift, false );
			}
		}

		if( count( $ids_admin_permanently ) > 0 ){
			foreach( $ids_admin_permanently as $id_admin ){
				Crunchbutton_Admin_Shift_Assign_Permanently::addDriver( $id_admin, $id_community_shift );
				if( count( $to_add ) > 0 ){
					$key = array_search( $id_admin, $to_add );
					if( $key === false ){
						Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $id_community_shift, $id_admin );
					}
				} else {
					Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $id_community_shift, $id_admin );
				}
			}
		}


		echo json_encode( array( 'success' => true ) );
	}

	public function driver(){

		switch ( c::getPagePiece( 4 ) ) {

			case 'assign':
				$this->driverAssign();
				break;

			default:
				$allItems = $this->request()[ 'allItems' ];
				$wantWorkItems = $this->request()[ 'wantWorkItems' ];
				$dontWantWorkItems = $this->request()[ 'dontWantWorkItems' ];
				$completed = $this->request()[ 'completed' ];
				$shifts = $this->request()[ 'shifts' ];
				$year = $this->request()[ 'year' ];
				$week = $this->request()[ 'week' ];

				if( count( $allItems ) > 0 ){

					$id_admin = c::admin()->id_admin;

					$status = Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $id_admin, $week, $year );
					$status->completed = $completed;<commit_msg>Driving schedule bug #5652<commit_after><?php

class Controller_api_drivers_shift extends Crunchbutton_Controller_RestAccount {

	public function init() {
		switch ( c::getPagePiece( 3 ) ) {
			case 'community':
				$this->community();
				break;

			case 'driver':
				$this->driver();
				break;

			case 'driver-schedule-sms-config':
				$this->driverScheduleSMSConfig();
				break;

			case 'driver-note-update':
				$this->driverNoteUpdate();
				break;

			case 'driver-orders-per-hour':
				$this->driverOrdersPerHour();
				break;

			default:
				echo json_encode( [ 'error' => 'invalid object' ] );
				break;
		}
	}

	public function driverScheduleSMSConfig(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$value = $this->request()[ 'value' ];
			$value = ( $value && $value > 0 ) ? 1 : 0;
			$admin->setConfig( Crunchbutton_Admin::CONFIG_RECEIVE_DRIVER_SCHEDULE_SMS_WARNING, $value );
			echo json_encode( [ 'success' => 'success' ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverOrdersPerHour(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$orders = $this->request()[ 'orders' ];
			$admin->saveOrdersPerHour( $orders );
			echo json_encode( [ 'success' => true ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverNoteUpdate(){
		$id_admin = $this->request()[ 'id_admin' ];
		$admin = Admin::o( $id_admin );
		if( $admin->id_admin ){
			$text = $this->request()[ 'text' ];
			$admin->addNote( $text );
			$note = $admin->note();
			echo json_encode( [ 'success' => $note->exports() ] );
		} else {
			echo json_encode( [ 'error' => 'invalid object' ] );
		}
	}

	public function driverAssign(){

		$ids_admin = $this->request()[ 'id_admin' ];
		$id_community_shift = $this->request()[ 'id_community_shift' ];
		$ids_admin_permanently = $this->request()[ 'id_admin_permanently' ];

		$to_remove = [];
		$to_remove_permanency = [];
		$to_add = $ids_admin;

		$assigneds = Crunchbutton_Admin_Shift_Assign::q( 'SELECT * FROM admin_shift_assign WHERE id_community_shift = "' . $id_community_shift . '"  ORDER BY id_admin' );

		foreach( $assigneds as $assigned ){

			// about the shift
			if( count( $to_add ) > 0 ){
				$key = array_search( $assigned->id_admin, $to_add );
				if( $key !== false ){
					unset( $to_add[ $key ] );
				} else {
					$to_remove[] = $assigned;
					$_to_remove[] = $assigned->id_admin;
				}
			} else {
				$to_remove[] = $assigned;
				$_to_remove[] = $assigned->id_admin;
			}

			// about permanency
			if( count( $ids_admin_permanently ) > 0 ){
				$key = array_search( $assigned->id_admin, $ids_admin_permanently );
				if( $key === false ){
					if( $assigned->isPermanent() ){
						$to_remove_permanency[] = $assigned;
						$_to_remove_permanency[] = $assigned->id_admin;
						// unset( $ids_admin_permanently[ $key ] );
					}
				}
			} else {
				if( $assigned->isPermanent() ){
					$to_remove_permanency[] = $assigned;
					$_to_remove_permanency[] = $assigned->id_admin;
				}
			}
		}

		if( count( $to_remove ) > 0 ){
			foreach( $to_remove as $remove ){
				if( $remove->isPermanent() ){
					// prevent the shift to be added again
					$removed = Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $remove->id_community_shift, $remove->id_admin );
				}
				$remove->delete();
			}
		}

		if( count( $to_remove_permanency ) > 0 ){

			foreach( $to_remove_permanency as $remove ){

				$shift = $remove->shift();

				$id_father = $shift->id_community_shift_father;
				if( !$id_father && $shift->recurring ){
					$id_father = $shift->id_community_shift;
				}

				if( $id_father ){
					$id_admin = $remove->id_admin;
					$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
					// Remove permanency
					Crunchbutton_Admin_Shift_Assign_Permanently::removeByAdminShiftFather( $id_admin, $id_father );

					// Remove next shifts assignments for the permanency
					$remove_assignment_after = ( $now->format( 'YmdHis' ) > $shift->dateEnd()->format( 'YmdHis' ) ? $now : $shift->dateEnd() ) ;

					$assignments = Crunchbutton_Admin_Shift_Assign::q('
						SELECT asa.* FROM admin_shift_assign asa
						INNER JOIN community_shift cs
							ON cs.id_community_shift = asa.id_community_shift
							AND cs.id_community_shift_father = ?
						WHERE cs.date_start > ?
							AND asa.id_admin = ?
							AND asa.warned = false
					', [$id_father, $remove_assignment_after->format( 'Y-m-d' ), $id_admin]);

					foreach( $assignments as $assignment ){
						$assignment->delete();
					}
				}
			}
		}

		if( count( $to_add ) > 0 ){
			foreach( $to_add as $id_admin ){
				Crunchbutton_Admin_Shift_Assign::assignAdminToShift( $id_admin, $id_community_shift, false );
			}
		}

		if( count( $ids_admin_permanently ) > 0 ){
			foreach( $ids_admin_permanently as $id_admin ){
				Crunchbutton_Admin_Shift_Assign_Permanently::addDriver( $id_admin, $id_community_shift );
				if( count( $to_add ) > 0 ){
					$key = array_search( $id_admin, $to_add );
					if( $key === false ){
						Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $id_community_shift, $id_admin );
					}
				} else {
					Crunchbutton_Admin_Shift_Assign_Permanently_Removed::add( $id_community_shift, $id_admin );
				}
			}
		}

		echo json_encode( array( 'success' => true ) );
	}

	public function driver(){

		switch ( c::getPagePiece( 4 ) ) {

			case 'assign':
				$this->driverAssign();
				break;

			default:
				$allItems = $this->request()[ 'allItems' ];
				$wantWorkItems = $this->request()[ 'wantWorkItems' ];
				$dontWantWorkItems = $this->request()[ 'dontWantWorkItems' ];
				$completed = $this->request()[ 'completed' ];
				$shifts = $this->request()[ 'shifts' ];
				$year = $this->request()[ 'year' ];
				$week = $this->request()[ 'week' ];

				if( count( $allItems ) > 0 ){

					$id_admin = c::admin()->id_admin;

					$status = Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $id_admin, $week, $year );
					$status->completed = $completed;
{'fraction_non_alphanumeric': 0.16494173254013528, 'fraction_numerical': 0.002444788525792519, 'mean_word_length': 3.4935920908092273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 151, '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': '6269035', 'n_tokens_mistral': 5265, 'n_tokens_neox': 4278, 'n_words': 809}