Esempio n. 1
0
 /**
  * Overloaded method onSave()
  * Executed whenever the user clicks at the save button
  */
 public function onSave()
 {
     // first, use the default onSave()
     $object = parent::onSave();
     // if the object has been saved
     if ($object instanceof Product) {
         $source_file = 'tmp/' . $object->photo_path;
         $target_file = 'images/' . $object->photo_path;
         $finfo = new finfo(FILEINFO_MIME_TYPE);
         // if the user uploaded a source file
         if (file_exists($source_file) and $finfo->file($source_file) == 'image/png') {
             // move to the target directory
             rename($source_file, $target_file);
             try {
                 TTransaction::open($this->database);
                 // update the photo_path
                 $object->photo_path = 'images/' . $object->photo_path;
                 $object->store();
                 TTransaction::close();
             } catch (Exception $e) {
                 new TMessage('error', '<b>Error</b> ' . $e->getMessage());
                 TTransaction::rollback();
             }
         }
     }
 }