Thursday, April 21, 2011

Display Confirm Message before a Custom Object Action with Admin Generator in Symfony

        object_actions:
          _edit: ~
          send: { params: { onclick : "if(confirm('Are you sure?')){return true;}else{return false;}" } }

Form Validation Errors At Top of the Page in Symfony

    <?php foreach($form->getWidgetSchema()->getPositions() as $widgetName): ?> <?php if($form[$widgetName]->hasError()): ?>
  • <?php echo $form[$widgetName]->renderLabelName().': '.__($form[$widgetName]->getError()->getMessageFormat()); ?>
  • <?php endif; ?> <?php endforeach; ?>

Monday, April 18, 2011

Show HTML Tags Instead of Entities in Flash Messages in Symfony

Use this instead:
$sf_user->getFlash('msg', ESC_RAW);

Send Email with Gmail SMTP in Symfony

Put this to the fatories.yml:
  mailer:
    param:
      transport:
        class: Swift_SmtpTransport
        param:
          host:       smtp.gmail.com
          port:       465
          encryption: ssl
          username:   "...@gmail.com"
          password:   "..."

Thursday, April 7, 2011

Mime Type Problem when Uploading DOC, DOCX, ODT Files in Symfony

If you create a DOC file with OpenOffice, its mimetype will be application/octet-stream, that can be anything binary data. Of course, you can't allow this mimetype at uploading.

If you try to upload DOCX and ODT files, symfony will recognise them as a ZIP archive. To corrrect this and the OpenOffice DOC mimetype problem, add the 'mime_type_guessers' option to sfValidatorFile:
$this->setValidator('filename', new sfValidatorFile(array(
      'max_size' => ...,
      'path' => ...,
      'mime_type_guessers' => array('guessFromFileinfo'),
      'required' => ...,
      ...

Convert RTF to TEXT with PHP and Linux

I always use UnRTF to get text from RTF files. The simplest way to convert the rtf file to html, then use the strip_tags method.
exec("unrtf test.rtf", $output);
echo strip_tags(implode('', $output));
However UnRTF is not fully compatible with UTF-8, it has problems with accents, ex. with the Hungarian accents, thus, I had to correct the Hungarian accents this way:
$from = array("Û", "û", "õ", "Õ");
$to = array("Ű", "ű", "ő", "Ő");

exec("unrtf test.rtf", $output);
echo strip_tags(str_replace($from, $to, html_entity_decode(implode('', $output), ENT_QUOTES, 'UTF-8')));

Convert ODT to TEXT with PHP

Shorter way (requires odt2txt installed and shell_exec enabled):
echo shell_exec("odt2txt --encoding=utf8 test.odt");
Longer way (requires PHP 5.2+ and ZIP extension enabled):
function odt2text($filename) {
    return readZippedXML($filename, "content.xml");
}

function readZippedXML($archiveFile, $dataFile) {
    // Create new ZIP archive
    $zip = new ZipArchive;

    // Open received archive file
    if (true === $zip->open($archiveFile)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags
            return strip_tags($xml->saveXML());
        }
        $zip->close();
    }

    // In case of failure return empty string
    return "";
}

echo odt2text("test.odt");

Convert DOCX to TEXT with PHP

First, you have to use PHP 5.2+ and enable the ZIP (ZipArchive) extension for PHP.
function docx2text($filename) {
    return readZippedXML($filename, "word/document.xml");
}

function readZippedXML($archiveFile, $dataFile) {
    // Create new ZIP archive
    $zip = new ZipArchive;

    // Open received archive file
    if (true === $zip->open($archiveFile)) {
        // If done, search for the data file in the archive
        if (($index = $zip->locateName($dataFile)) !== false) {
            // If found, read it to the string
            $data = $zip->getFromIndex($index);
            // Close archive file
            $zip->close();
            // Load XML from a string
            // Skip errors and warnings
            $xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            // Return data without XML formatting tags
            return strip_tags($xml->saveXML());
        }
        $zip->close();
    }

    // In case of failure return empty string
    return "";
}

echo docx2text("test.docx"); 

Wednesday, April 6, 2011

Convert DOC to TEXT with PHP and Linux

$file     = $directory.'/'.$filename;
$fileinfo = pathinfo($filename);
$content  = "";

// doc to text
if($fileinfo['extension'] == 'doc')
{
   $content = shell_exec("antiword -m UTF-8.txt -t $file");
}

Convert PDF to TEXT with PHP and Linux

To install pdftotext, use: apt-get install xpdf
$file     = $directory.'/'.$filename;
$fileinfo = pathinfo($filename);
$content  = "";

// pdt to text
if($fileinfo['extension'] == 'pdf')
{
   $outpath = preg_replace("/\.pdf$/", "", $file).".txt";
   system("pdftotext -enc UTF-8 ".escapeshellcmd($file), $ret);
   if($ret == 0) {
      $content = file_get_contents($outpath);
      unlink($outpath);
   }
}

Monday, April 4, 2011

Correct the Icon Position of sfAdminThemejRollerPlugin Buttons in IE, Chrome, Safari

Use this code in the layout after the CSS definitions....