I have website.com/sample-page with some text and a download button (external link like download.com/file)
I want to change the target link (download.com/file) if a user is from France. I dont want to redirect entire page/website, only this link.
I am using wordpress (if helps).
Some advices, please.
Is very simple, you need to detect the user language, then go with if, elseif, else and call the header location when need or simple doing echo of link...
$find = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = substr($find,0,2);
if ($lang == "fr") {
header("Location: download.com/file_fr");
exit();
} elseif ($lang == "it") {
header("Location: download.com/file_it");
exit();
} elseif ($lang == "xx") {
header("Location: download.com/file_xx");
exit();
} else {
header("Location: download.com/file");
exit();
}
or using another trick...
$link = "";
$find = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = substr($find,0,2);
if ($lang == "fr") {
$link = "download.com/file_fr";
} elseif ($lang == "it") {
$link = "download.com/file_it";
} elseif ($lang == "xx") {
$link = "download.com/file_xx";
} else {
$link = "download.com/file";
}
echo "$link";
Let's me know if my answer is what you want :)
Related
I'm missing a column in my exchangeArray. It is the column with the filename, here a snippet of my controller add action:
if ($form->isValid()) {
$data = $form->getData();
// Upload path
$location = "public/files/";
// A bit validation of uploaded file
$allowedExtension = array('xls', 'xlsx');
$extension = explode('.', $data['DCL_Path']['name']);
$extension = end($extension);
$import['DCL_Path']=$data['DCL_Path']['name'];
//$fileName = time() . '.' . $extension;
$fileName = $data['DCL_Path']['name'];
// Check if everything is OK!
//echo $fileName;
if (0 === $data['DCL_Path']['error'] && in_array($extension, $allowedExtension)) {
move_uploaded_file($data['DCL_Path']['tmp_name'], $location . $fileName);
} else {
echo 'Something went wrong!';
}
//$namen = explode(",", $import ); //Konvertierung des Strings in ein Array
//echo "<pre>"; var_dump($namen); echo "</pre>"; //Formartierte Ausgabe des Arrays
$this->table->saveImport($import);
Everything will be saved, but DCL_path is missing.
Here my exchangearray also:
public function exchangeArray(array $data)
{
$this->DCLID= !empty($data['DCLID']) ? $data['DCLID'] : null;
$this->UnitID= !empty($data['UnitID']) ? $data['UnitID'] : null;
$this->DCL_Path= !empty($data['DCL_Path']) ? $data['DCL_Path'] : null;
$this->Importdate= !empty($data['Importdate']) ? $data['Importdate'] : null;
$this->Importuser= !empty($data['Importuser']) ? $data['Importuser'] : null;
$this->Importok= !empty($data['Importok']) ? $data['Importok'] : null;
$this->DCL_Type= !empty($data['DCL_Type']) ? $data['DCL_Type'] : null;
$this->Changed_per_User= !empty($data['Changed_per_User']) ? $data['Changed_per_User'] : null;
$this->Description_Changes= !empty($data['Description_Changes']) ? $data['Description_Changes'] : null;
}
I added this statement: $import['DCL_Path']=$data['DCL_Path']['name']; to force it, but it didn't help. So my question would be how to add data in datafields of my exchangeArray.
EDIT1: Here is the start off my addAction.
$import = new Import(); //Neue Instanz von Import
$form->setInputFilter($import->getInputFilter()); //Filter an Form binden
$form->setData($request->getPost()); //Daten abholen
//echo $form->isValid();
if (! $form->isValid()) {
return ['form' => $form]; //Wenn die Daten nicht valide sind
// return $this->redirect()->toRoute('import'); //Routing
// Zend_Debug::dump("nicht valide");
//echo "nicht valide";
}
else{ //aus Tableadapter
$import->exchangeArray($form->getData());
// echo "valide";
//echo $import["DCL_Path"];
// Merge data thus
$data = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$this->getRequest()->getFiles()->toArray()
);
$form->setData($data);
I also find another solution which is working, I post it additional perhaps it might help somebody else. Of course I still would prefer the way via exchangeArray.
$this->table->saveImport($import); //Daten speichern
$letzter = $this->table->getlastImport($fileName);
echo ($letzter->DCLID);
$this->table->updateoneRow($letzter->DCLID, 'DCL_Path', $fileName);
I made a module that display how many days ago a article was published
it looks like this.
{source}
<?php
$jinput = JFactory::getDocument()->input;
$option = $jinput->get('option');
$view = $jinput->get('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& $jinput->get("content");
$article->load($article_id);
$date = new JDate($article->get("publish_up"));
$currentTime = new JDate('now');
$interval = $date->diff($currentTime);
if($interval->d == 0) {
echo 'dzisiaj' . "<br>";
}
else if( $interval->d == 1) {
echo 'wczoraj' . "<br>";
}
else if( $interval->d > 1) {
echo $interval->format('%a dni temu') . "<br>";
}
}
?>
{/source}
And it works on my local joomla but when use it on custom template it doesnt work. I'm using Joomla 3.4.8.
The issue is you're trying to access the input values using Document Factory that's wrong you have to use
$jinput = JFactory::getApplication()->input;
Document Factory is used for other purpose like adding , styles or Js to the pages etc. read more about input here.
Hope it make sense.
I've got an "addForm" and a "editForm". After I have added a document file in the addForm, it will be saved in my db. If I want to edit this form, I have to upload this document again. The old document will be deleted. I would like to make a function to check whether the document is already uploaded or not, so I do not have to upload it every time I want to edit an item. I just do not know where to start. A little help will be great.
my add/editform:
$pdf = new Zend_Form_Element_File('document');
$pdf->setLabel('Nieuwe PDF')
->addValidator('extension', true, array('docx',
'docx','pdf','txt'))
->addValidator('Count', false, 1)
->addValidator('Size', false, 10240000)
->setDestination( PUBLIC_PATH . '/../data/invoicespdf/')
->setRequired(false);
Try using this in the controller...modify it to fit your needs :
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
if ('administrator' == $user->role) {
$oldFileName = $form->getElement('oldfilename')->getValue(); //the hidden field
$data = $form->getValues();
$model->populate($data);
if (file_exists('uploads/cv/' . $oldFileName)) {
$form->getElement('cv')->setIgnore(true); //this is my Form File Element - the file exists, I don't need to store the filename
} else { // if you want you can unlink $oldFileName
$upload = new Zend_File_Transfer_Adapter_Http();
$info = $upload->getFileInfo('cv');
$upload->setDestination("uploads/cv/");
if (file_exists('uploads/cv/' . $info['cv']['name'])) {
$newFileName = time() . rand(0, 100000) . "-" . $info['cv']['name']; // I need to avoid overwriting file
} else {
$newFileName = $info['cv']['name'];
$upload->addFilter('Rename', $newFileName);
}
try {
$upload->receive();
} catch (Zend_File_Transfer_Exception $e) {
$e->getMessage();
}
}
$model->save();
return $this->_helper->redirector('list');
} else {
//some error message
$this->_helper->redirector('list');
}
} else { //form not valid
$this->view->form = $form;
}
} else {
$model->find($id);
$data = array();
$data = $model->toArray();
$data['oldfilename'] = $model->get_cv(); //the filename stored in db
$form->getElement('cv')->setRequired(false);
$form->populate($data);
$this->view->form = $form;
}
So, I am using Zend to handle my image uploads. This script works well, but I was wondering if there is a way to resize the image that is being uploaded no matter what the current size is. I've seen 2 similar posts, but their code was entirely different, thus would be difficult to translate into mine from theirs. If possible, I would really like to not have to use extensions, but I will if I have to. Any ideas?
if (isset($_POST['upload'])) {
require_once('scripter/lib.php');
//try {
$destination = 'C:\----';
$uploader = new Zend_File_Transfer_Adapter_Http();
$uploader->setDestination($destination);
$filename = $uploader->getFileName(NULL, FALSE);
$uploader->addValidator('Size', FALSE, '10000kB');
$uploader->addValidator('ImageSize', FALSE, array('minheight' => 100, 'minwidth' => 100));
//$pic = $filename;
if (!$uploader->isValid() || $errors) {
$messages = $uploader->getMessages();
} else {
//$pic = $filename;
$no_spaces = str_replace(' ', '_', $filename, $renamed);
$uploader->addValidator('Extension', FALSE, 'gif, png, jpg');
$recognized = FALSE;
if ($uploader->isValid()) {
$recognized = TRUE;
} else {
$mime = $uploader->getMimeType();
$acceptable = array('jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif');
$key = array_search($mime, $acceptable);
if (!$key) {
$messages[] = 'Unrecognized image type';
} else {
$no_spaces = "$no_spaces.$key";
$recognized = TRUE;
$renamed = TRUE;
}
}
$uploader->clearValidators();
if ($recognized) {
$existing = scandir($destination);
if (in_array($no_spaces, $existing)) {
$dot = strrpos($no_spaces, '.');
$base = substr($no_spaces, 0, $dot);
$extension = substr($no_spaces, $dot);
$i = 1;
do {
$no_spaces = $base . '_' . $i++ . $extension;
} while (in_array($no_spaces, $existing));
$renamed = TRUE;
}
$uploader->addFilter('Rename', array('target' => $no_spaces));
$success = $uploader->receive();
if (!$success) {
$messages = $uploader->getMessages();
} else {
//$pic = $no_spaces;
$uploaded = "$filename uploaded successfully";
$pic = $filename;
if ($renamed) {
$pic = "imgs/upld/" . $no_spaces;
$uploaded .= " and renamed $no_spaces";
//$pic = $no_spaces;
//$pic = $uploader->getFileName(NULL, FALSE);
} else {$pic = "imgs/upld/" . $filename;;}
$messages[] = "$uploaded";
//$pic = $no_spaces;
}
Zend Framework does not ship with a component for handling images.
Good News! PHP has several components that are really good at dealing with all kinds of image issues.
GD (one of those great PHP extensions) is currently shipped as a core extension for PHP, perhaps you will find it useful.
Maybe this will help: http://phpcodeforbeginner.blogspot.com/2013/04/resize-image-or-crop-image-using-gd.html
(not really trying to be too snarky ;)
How can i upload a file? Stackoverflow i cant page codes so please check this link: http://pastebin.org/386639
Thanks in advance
LATER:
Later i updated this as following, because ZF is not friendly, they keep still everything top secret! :P
public static function mvUploadFile()
{
//
// $_GET/_POST/FILE what ever
//
$fname = basename( $_FILES['attachment']['name']);
$_fname = strtolower (end(explode('.',$fname) ) );
Zend_Debug::dump( $_FILES );
//
// Filter the file
//
switch($_fname)
{
case ($_fname == 'jpg' ||
$_fname == 'jpeg' ||
$_fname == 'gif' ||
$_fname == 'bmp' ||
$_fname == 'png' ||
$_fname == 'html' ||
$_fname == 'pdf' ||
$_fname == 'doc' ||
$_fname == 'docx' ||
$_fname == 'xls'
):
$target_path = APPLICATION_PATH . "/../public/files/textual_translation_attachment/";
//chmod("../up" , 0777);
$target_path = $target_path . basename( $_FILES['attachment']['name']);
if(move_uploaded_file($_FILES['attachment']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
//$_sql = "insert into a (huis,image) values ('$_app','$_file')";
// send the file name only .....
//echo $fname ;
}else{
//echo "error ";
}
break;
}
return $fname;
}
If you
Zend_Debug::dump($_FILES);
Do you see your file?
If you are using jQuery, you may want to try Uploadify. This can be done using the
Zend Framework Uploadify Extension
http://gondo.webdesigners.sk/zend-framework-uploadify-extension/