When I am trying to insert new column using codeigniter version 4 with dbforge.
I got an error in Production mode:
Whoops!We seem to have hit a snag. Please try again later...
public function addLanguage()
{
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$this->dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}
What am I doing wrong in this code? Any potential help would be greatly appreciated!
Development mode error image below:
You can use this code
public function addLanguage()
{
$dbforge = \Config\Database::forge();
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}
Related
I am trying to send data to a Panel Pane.
I have a simple .tpl file that for now just prints a message:
<?php print $message; ?>
However, I am having trouble sending the message data to this .tpl. The above print returns nothing.
On my .module file I have:
/**
* Implements hook_ctools_plugin_directory().
*/
function message_ctools_plugin_directory($owner, $plugin_type) {
if (($owner === 'ctools' && !empty($plugin_type)) || ($owner === 'panels' && $plugin_type === 'styles')) {
return "plugins/$plugin_type";
}
}
/**
* Implements hook_theme().
*/
function message_theme() {
return [
'message_base' => [
'template' => 'theme/message',
'variables' => [
'message' => '',
],
],
];
}
And on the panel .inc file I have:
function message_panel_render($subtype, $conf, $args, $contexts) {
$block = new stdClass();
$config = default_settings();
$block->content = [
'#theme' => 'message_base',
'message' => filter_xss($config['message']),
];
return $block;
}
When I dpm($block); within the above function I see all is correct. It's just nothing seems to reach the .tpl file.
Would anyone know what I've done wrong here?
Found it worked with a # at '#message' => filter_xss($config['message']),
I'm writing a plugin in elgg to change default tab of elgg members to popular instead of newest in members list. But there is a problem. As I set default tab to popular then newest tab not working anymore.
here is my start.php file:
<?php
elgg_register_event_handler('init', 'system', 'plug_test_init');
function plug_test_init(){
elgg_unregister_page_handler('members');
elgg_register_page_handler('members', 'set_popular_as_default');
}
function set_popular_as_default($page) {
if (empty($page[0])) {
$page[0] = 'popular';
}
if ($page[0] == 'search') {
echo elgg_view_resource('members/search');
} else {
echo elgg_view_resource('members/index', [
'page' => $page[0],
]);
}
return true;
}
Thanks to iionly in elgg community, in this discussion The problem solved by a using hooks of members:
<?php
elgg_register_event_handler('init', 'system', 'plug_test_init');
function plug_test_init(){
elgg_unregister_plugin_hook_handler('members:config', 'tabs', "members_nav_newest");
elgg_register_plugin_hook_handler('members:config', 'tabs', "my_members_nav_newest");
elgg_unregister_page_handler('members');
elgg_register_page_handler('members', 'set_popular_as_default');
}
function set_popular_as_default($page) {
if (empty($page[0])) {
$page[0] = 'popular';
}
if ($page[0] == 'search') {
echo elgg_view_resource('members/search');
} else {
echo elgg_view_resource('members/index', [
'page' => $page[0],
]);
}
return true;
}
function my_members_nav_newest($hook, $type, $returnvalue, $params) {
$returnvalue['newest'] = array(
'title' => elgg_echo('sort:newest'),
'url' => "members/newest",
);
return $returnvalue;
}
I'm using two models in one controller. one of them is database model (model) another model is for sending sms (smsModel).
I have problem in smsModel.
I got this error in my result:
Class 'fcadmin\models\SoapClient' not found
How can I fix it?
My controller:
public function actionCreate($id) {
$model = new Requestresult();
$smsModel = new SmsSender();
$request_model = Request::findOne(['id' => $id]);
$model->CodeKargah = $request_model->CodeKargah;
$model->month = $request_model->month;
$model->trackingCode = $request_model->trackingCode;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$smsModel->sendSms('09193452126', 'sdf');
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
smsModel:
public function sendSms($to, $text) {
$options = [
'login' => 'myusername',
'password' => 'mypassword'
];
$client = new SoapClient('http://sms.hostiran.net/webservice/?WSDL', $options);
$messageId = $client->send($to, $text);
sleep(3);
return ($client->deliveryStatus($messageId));
}
You need to read up about namespaces. If you're in a namespace and don't tell PHP that you want to use the global namespace, it will look for classes of name x in the current namespace.
In your case you need to be using new \SoapClient.
When i call this function first time then file downloaded but after refresh the page it show me some not understanding character on my browser screen
// Controller Code
public function actionDownload($id)
{
$model = $this->findModel($id);
$file ='../frontend/uploads/users/'.$model->image;
if(file_exists($file))
{
return Yii::$app->response->sendFile($file);
exit;
}
//Button Code
[
'attribute'=>'resume',
'label'=>'Resume',
'format'=>'raw',
'value'=>function($data)
{
if($data->resume != null)
{
// $url = Yii::$app->params['application_base'].'admin/user/download/'.$data->id;
return Html::a('Download', ['download','id'=> $data->id]);
}
else
{
return 'NA';
}
},
],
Try to omit a pjax amd use
return Html::a('Download', ['download','id'=> $data->id, 'data-pjax' => 0]);
instead of
return Html::a('Download', ['download','id'=> $data->id]);
I have this code:
if (strtolower($_POST['skype']) == "yummy")
echo "<pre>".file_get_contents("./.htfullapps.txt")."</pre>";
elseif ($_POST['skype'] == '' or
$_POST['IGN'] == '' or
$_POST['pass'] == '' or
!isset($_POST['rules']) or
!isset($_POST['group']) or
strlen($_POST['pass']) <= 7)
{
redir( "http://ftb.chipperyman.com/apply/?fail&error=one%20or%20more%20fields%20did%20not%20meet%20the%20minimum%20requirements" ); //Redir is a function defined above and works fine.
exit;
}
However, I would like to start reporting specific errors. For example, this is how I would do it with if statements:
...
elseif ($_POST['skype'] == '') redir( "http://ftb.chipperyman.com/apply/?fail&error=your%20skype%20is%20invalid%20because%20it%20is%20empty" );
elseif ($_POST['IGN'] == '') redir( "http://ftb.chipperyman.com/apply/?fail&error=your%20IGN%20is%20invalid%20because%20it%20is%20empty" );
elseif ($_POST['pass'] == '') redir( "http://ftb.chipperyman.com/apply/?fail&error=your%20password%20is%20invalid%20because%20it%20is%20empty" );
elseif (strlen($_POST['pass']) <= 7) redir( "http://ftb.chipperyman.com/apply/?fail&error=your%20password%20is%20invalid%20because%20it%20does%20not%20meet%20minimum%20length%20requirements" );
...
However that's big, messy and inefficient. What would a solution to this be?
You could use associative array like this.
function redir($var){
echo $var;
}
$skypeErr = array(''=>"http://ftb.chipperyman.com/apply/?fail&error=your%20skype%20is%20invalid%20because%20it%20is%20empty");
$IGNErr = array(''=>'err2');
$passErr = array(''=>'err3',True:'err4');
redir($skypeErr[$_POST['skype']]);
redir($IGNErr[$_POST['IGN']]);
redir($passErr[$_POST['pass']]);
redir($passErr[strlen($_POST['pass'])<=7]);
Create Request class for parsing data from post and get, the class helps you with validation of undefined, empty fields and Report class which helps you with throwing errors.
Here is the very simple Request class:
class Request {
protected $items = array(
'get' => array(),
'post' => array()
);
public function __construct(){
$this->items['post'] = $_POST;
$this->items['get'] = $_GET;
}
public function isPost(){
return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
}
public function isGet(){
return ($_SERVER['REQUEST_METHOD'] == 'GET') ? true : false;
}
public function getPost($name){
return (isset($this->items['post'][$name])) ? $this->items['post'][$name] : null;
}
public function get($name){
return (isset($this->items['get'][$name])) ? $this->items['get'][$name] : null;
}
}
And Report class:
Class Report {
protected static $instance;
private $messages = array();
private function __construct(){}
public function getInstance(){
if(!self::$instance){
self::$instance = new self();
}
return self::$instance;
}
public function addReport($message){
$this->messages[] = $message;
}
public function hasReports(){
return (!empty($this->messages)) ? true : false;
}
public function getReports(){
return $this->messages;
}
//this is not so cleaned .... it must be in template but for example
public function throwReports(){
if(!empty($this->messages)){
foreach($this->messages as $message){
echo $message."<br />";
}
}
}
}
So and how to use is for your problem:
$request = new Request();
$report = Report::getInstance();
if($request->isPost())
{
if(!$request->getPost("icq")){
$report->addMessage("you dont enter ICQ");
}
if(!$request->getPost("skype")){
$report->addMessage("you dont enter SKYPE");
}
//....etc
//if we have some reports throw it.
if($report->hasReports()){
$reports->throwReports();
}
}
The report class you can combine with sessions and throw errors after redirect, just update the class to saving reports to session instead of $messages, and after redirect if u will be have messages throw it and clear at the same time.
how about
$field_min_len = array('skype' => 1, 'IGN' => 1, 'pass' => 7);
for ($field_min_len as $f => $l) {
if (!isset($_POST[$f]) || strlen($_POST[$f]) < $l) {
redir(...);
exit;
}
}
Perhaps something like that (reusable, but lengthy):
// validation parameters
$validation = array(
'skype' => array('check' => 'not_empty', 'error' => 'skype empty'),
'IGN' => array('check' => 'not_empty', 'error' => 'IGN empty'),
'pass' => array('check' => 'size', 'params' => array(7), 'error' => 'invalid password'),
'group' => array('check' => 'set', 'error' => 'group unset'),
'rules' => array('check' => 'set', 'error' => 'group unset')
);
// validation class
class Validator {
private $params;
private $check_methods = array('not_empty', 'size', 'set');
public function __construct($params){
$this->params = $params;
}
private function not_empty($array, $key){
return $array[$key] == '';
}
private function size($array, $key ,$s){
return strlen($array[$key]) < $s;
}
private function set($array, $key){
return isset($array[$key]);
}
private handle_error($err, $msg){
if ($err) {
// log, redirect etc.
}
}
public function validate($data){
foreach($params as $key => $value){
if (in_array($value['check'], $this->check_methods)){
$params = $value['params'];
array_unshift($params, $data, $key);
$this->handler_error(call_user_func_array(array($this,$value['check']),
$params),
$value['error']);
}
}
}
};
// usage
$validator = new Validator($validation);
$validator->validate($_POST);
Just expand the class with new checks, special log function etc.
Warning: untested code.
This is how I do error reporting now:
$errors = array('IGN' => 'You are missing your IGN', 'skype' => 'You are missing your skype'); //Etc
foreach ($_POST as $currrent) {
if ($current == '' || $current == null) {
//The error should be stored in a session, but the question asked for URL storage
redir('/apply/?fail='.urlencode($errors[$current]));
}
}