/**
  * Prepare project images before saving.
  *
  * @param   CrowdfundingTableProject $table
  * @param   array  $data
  *
  * @throws Exception
  *
  * @since    1.6
  */
 protected function prepareTableData($table, $data)
 {
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     // Set order value
     if (!$table->get('id') and !$table->get('ordering')) {
         $db = $this->getDbo();
         $query = $db->getQuery(true);
         $query->select('MAX(ordering)')->from($db->quoteName('#__crowdf_projects'));
         $db->setQuery($query, 0, 1);
         $max = $db->loadResult();
         $table->set('ordering', $max + 1);
     }
     // Prepare image.
     if (!empty($data['image'])) {
         // Delete old image if I upload a new one
         if ($table->get('image')) {
             $imagesFolder = $params->get('images_directory', 'images/crowdfunding');
             // Remove an image from the filesystem
             $fileImage = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image'));
             $fileSmall = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image_small'));
             $fileSquare = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('image_square'));
             if (is_file($fileImage)) {
                 JFile::delete($fileImage);
             }
             if (is_file($fileSmall)) {
                 JFile::delete($fileSmall);
             }
             if (is_file($fileSquare)) {
                 JFile::delete($fileSquare);
             }
         }
         $table->set('image', $data['image']);
         $table->set('image_small', $data['image_small']);
         $table->set('image_square', $data['image_square']);
     }
     // Prepare pitch image.
     if (!empty($data['pitch_image'])) {
         // Delete old image if I upload a new one
         if ($table->get('pitch_image')) {
             $imagesFolder = $params->get('images_directory', 'images/crowdfunding');
             // Remove an image from the filesystem
             $pitchImage = JPath::clean(JPATH_ROOT . DIRECTORY_SEPARATOR . $imagesFolder . DIRECTORY_SEPARATOR . $table->get('pitch_image'));
             if (is_file($pitchImage)) {
                 JFile::delete($pitchImage);
             }
         }
         $table->set('pitch_image', $data['pitch_image']);
     }
     // If an alias does not exist, I will generate the new one using the title.
     if (!$table->get('alias')) {
         $table->set('alias', $table->get('title'));
     }
     if ((int) JFactory::getConfig()->get('unicodeslugs') === 1) {
         $table->set('alias', JFilterOutput::stringUrlUnicodeSlug($table->get('alias')));
     } else {
         $table->set('alias', JFilterOutput::stringURLSafe($table->get('alias')));
     }
     // Prepare funding duration
     $durationType = Joomla\Utilities\ArrayHelper::getValue($data, 'duration_type');
     $fundingStart = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_start');
     $fundingEnd = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_end');
     $fundingDays = Joomla\Utilities\ArrayHelper::getValue($data, 'funding_days');
     // Prepare funding start date.
     $fundingStartValidator = new Prism\Validator\Date($fundingStart);
     if (!$fundingStartValidator->isValid()) {
         $table->funding_start = Prism\Constants::DATE_DEFAULT_SQL_DATE;
     } else {
         $date = new JDate($fundingStart);
         $table->funding_start = $date->toSql();
     }
     switch ($durationType) {
         case 'days':
             // Set funding day.
             $table->funding_days = $fundingDays;
             // Calculate end date
             $fundingStartValidator = new Prism\Validator\Date($table->funding_start);
             if (!$fundingStartValidator->isValid()) {
                 $table->funding_end = Prism\Constants::DATE_DEFAULT_SQL_DATE;
             } else {
                 $fundingStartDate = new Crowdfunding\Date($table->funding_start);
                 $fundingEndDate = $fundingStartDate->calculateEndDate($table->funding_days);
                 $table->funding_end = $fundingEndDate->toSql();
             }
             break;
         case 'date':
             $fundingEndValidator = new Prism\Validator\Date($fundingEnd);
             if (!$fundingEndValidator->isValid()) {
                 throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_INVALID_DATE'));
             }
             $date = new JDate($fundingEnd);
             $table->funding_days = 0;
             $table->funding_end = $date->toSql();
             break;
         default:
             $table->funding_days = 0;
             $table->funding_end = Prism\Constants::DATE_DEFAULT_SQL_DATE;
             break;
     }
 }