Ejemplo n.º 1
0
function advert_save($id, $option, $task)
{
    $app = JFactory::getApplication();
    $db =& JFactory::getDBO();
    // Load Record Object
    $row = new wbAdvert_advert($db);
    if ($id) {
        $row->load($id);
    }
    $row->bind($_POST);
    // Reset Impressions Counter
    if ($task == 'advert.reset') {
        $row->clicks = 0;
        $row->impmade = 0;
    }
    // Code
    if (array_key_exists('code', $_POST)) {
        $row->code = stripslashes(trim($_POST['code']));
    }
    // Dates
    $nullDate = $db->getNullDate();
    if (strlen(trim($row->date_start)) < 10) {
        $row->date_start = date('Y-m-d', time() + $app->getCfg('offset') * 60 * 60);
    }
    if (strlen(trim($row->date_stop)) < 10) {
        $row->date_stop = $nullDate;
    }
    // Times
    if (!strlen($row->time_start)) {
        $row->time_start = null;
    }
    if (!strlen($row->time_stop)) {
        $row->time_stop = null;
    }
    // Weekdays
    $weekdays = JRequest::getVar('weekdays', array(), 'method', 'array');
    if (count($weekdays)) {
        if (in_array('all', $weekdays)) {
            $row->weekdays = null;
        } else {
            $row->weekdays = implode(',', $weekdays);
        }
    } else {
        $row->weekdays = '';
    }
    // Upload Image
    if (!$row->upload()) {
        echo "<script> alert('" . JText::sprintf('MSG_DBERR', $row->getError()) . "'); window.history.go(-1); </script>\n";
        exit;
    }
    // Save Record
    if (!$row->store(true)) {
        echo "<script> alert('" . JText::sprintf('MSG_DBERR', $row->getError()) . "'); window.history.go(-1); </script>\n";
        exit;
    }
    // Update Session
    $session =& JFactory::getSession();
    $session->set('wbadvert_cache_adsizes', null);
    // Save Group Index
    $idx_group = wbAdvert_Common::stripFromArray(JRequest::getVar('idx_group', array(0), 'method', 'array'), 0);
    $wbAdvert_idx_group = new wbAdvert_idx_group();
    $wbAdvert_idx_group->delete($row->id, 0);
    if (count($idx_group)) {
        $wbAdvert_idx_group->save($row->id, 0, $idx_group);
    } else {
        $wbAdvert_idx_group->save($row->id, 0, 0);
    }
    // Save Category Index
    $idx_category = wbAdvert_Common::stripFromArray(JRequest::getVar('idx_category', array(0), 'method', 'array'), 0);
    $wbAdvert_idx_category = new wbAdvert_idx_category();
    $wbAdvert_idx_category->delete($row->id, 0);
    if (count($idx_category)) {
        $wbAdvert_idx_category->save($row->id, 0, $idx_category);
    } else {
        $wbAdvert_idx_category->save($row->id, 0, 0);
    }
    // Save Menu Index
    $idx_menu = wbAdvert_Common::stripFromArray(JRequest::getVar('idx_menu', array(0), 'method', 'array'), 0);
    $wbAdvert_idx_menu = new wbAdvert_idx_menu();
    $wbAdvert_idx_menu->delete($row->id, 0);
    if (count($idx_menu)) {
        $wbAdvert_idx_menu->save($row->id, 0, $idx_menu);
    } else {
        $wbAdvert_idx_menu->save($row->id, 0, 0);
    }
    // Save Content Index
    $idx_content = array();
    $idx_temps = explode(',', preg_replace('/[\\r\\n\\t]/', ',', JRequest::getVar('idx_content', '')));
    foreach ($idx_temps as $idx_temp) {
        if ((int) $idx_temp > 0) {
            $idx_content[] = (int) $idx_temp;
        }
    }
    $wbAdvert_idx_content = new wbAdvert_idx_content();
    $wbAdvert_idx_content->delete($row->id, 0);
    if (count($idx_content)) {
        $wbAdvert_idx_content->save($row->id, 0, $idx_content);
    } else {
        $wbAdvert_idx_content->save($row->id, 0, 0);
    }
    // Check Required
    if (!$row->check()) {
        $app->redirect('index.php?option=' . WBADVERT_NAME . '&task=advert.edit&hidemainmenu=1&id=' . $row->id, $row->getError(), 'error');
        exit;
    } else {
        $row->store();
    }
    // Checkin
    $row->checkin();
    foreach ($idx_group as $group_id) {
        $row->reorder($group_id);
    }
    // Redirect
    $msg = JText::sprintf('MSG_SAVED', JText::_('Advertisement'));
    if (in_array($task, array('advert.apply', 'advert.reset'))) {
        $app->redirect('index.php?option=' . WBADVERT_NAME . '&task=advert.edit&hidemainmenu=1&id=' . $row->id, $msg);
    } else {
        $app->redirect('index.php?option=' . WBADVERT_NAME . '&task=advert', $msg);
    }
}