Tuesday, August 3, 2010

Redirect to Referer in Action in Symfony

$this->redirect($this->getRequest()->getReferer());

Validating Integers without Regular Expression in PHP

ctype_digit checks if all of the characters in the provided string are numerical. Therefore, you must convert variables to string first, then you check them with ctype_digit.
$array = array(5, 5.2, "456", "564-223", "5.2", "673");
foreach($array as $item) {
   if(ctype_digit((string)$item)) {
      echo "int: ".$item."\n";
   } else {
      echo "not int: ".$item."\n";
   }
}

Monday, August 2, 2010

Delete embed forms/objects in Symfony

First of all, you have to add a delete checkbox to the embedded form:
// lib/form/doctrine/ formclass -> configure()
if ($this->object->exists())
{
   $this->widgetSchema['delete'] = new sfWidgetFormInputCheckbox();
   $this->validatorSchema['delete'] = new sfValidatorPass();
}
Don't forget to echo the 'delete' form widget in the template. Now open the form class which embed the other form. You have to overwrite some basic methods. The code below is used and created by me, but perhaps you won't be able to use/understand. Please, first check the the following blog entry: Embedding Relations in Forms with Symfony 1.3 and Doctrine
public function saveEmbeddedForms($con = null, $forms = null)
 {
  if (null === $forms)
  {
   $notes = $this->getValue('newNotes');
   $forms = $this->embeddedForms;
   foreach ($this->embeddedForms['newNotes'] as $name => $form)
   {
    if (!isset($notes[$name]))
    {
     unset($forms['newNotes'][$name]);
    }
   }
  }
  
  foreach ($forms as $form)
  {
    if ($form instanceof sfFormObject)
    {
   if (!in_array($form->getObject()->getId(), $this->scheduledForDeletion))
   {
     $form->saveEmbeddedForms($con);
     $form->getObject()->save($con);
   }
    }
    else
    {
   $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
    }
  }


 }
 
  protected function doBind(array $values)
  {   
    if (isset($values['Notes']))
    {
      foreach ($values['Notes'] as $i => $noteValues)
      {
        if (isset($noteValues['delete']) && $noteValues['id'])
        {
          $this->scheduledForDeletion[$i] = $noteValues['id'];
        }
      }
    }
    
    parent::doBind($values);
  }
  
  protected function doUpdateObject($values)
  {
    if (count($this->scheduledForDeletion))
    {
      foreach ($this->scheduledForDeletion as $index => $id)
      {
        unset($values['Notes'][$index]);
        unset($this->object['Notes'][$index]);
        Doctrine::getTable('ServiceNote')->find($id)->delete();
      }
    }
 
    $this->getObject()->fromArray($values);
  }

Saturday, July 31, 2010

Disable HTML entities in CKEditor

Find ckeditor/ckeditor.config.js or ckeditor/config.js and add: config.entities = false;

Tuesday, July 27, 2010

Install AVR Media AUDIO and VIDEO Player for Joomla 1.5

  1. Install com_avreloaded-x.x.x.zip
  2. Check PHP upload_max_filesize setting, sometimes it has a very low value
  3. Go to Media Manager and create 'audio' and 'videos' folder in 'stories'
  4. Go to Global Settings and add the extensions (for uploading) you need (mp3, avi, mpg, etc)
  5. When you write an article, you will find an AVR Media button at the bottom, use that to include an audio or video player...

Thursday, July 15, 2010

Get User ID with Jumi in Joomla

If you include a php file to an article with Jumi, you can get the user id with this:
defined('_JEXEC') OR defined('_VALID_MOS') OR die( "Direct Access Is Not Allowed" );

$jAp= & JFactory::getApplication();
$user =& JFactory::getUser();
echo $user->get('id');

Tuesday, July 6, 2010

Select Items by the First Letter in MySQL

If you try to select items by the first letter and you want MySQL to deal with case sensitivity and accents, you have to collate the utf8_unicode_ci table to utf8_bin in this way...
SELECT * FROM thetable WHERE firstname COLLATE utf8_bin LIKE 'É%'