function edit() {
            $aItem       = $this->data;
            $flash_error = '';

            // Sanitize
            foreach(@$aItem['title'] as $key=>$value) {
                $aItem['title'][$key] = strip_tags( trim ( $value ) );
            }

            $aItem['price']    = !is_null($aItem['price']) ? strip_tags( trim( $aItem['price'] ) ) : $aItem['price'];
            $aItem['cityArea'] = osc_sanitize_name( strip_tags( trim( $aItem['cityArea'] ) ) );
            $aItem['address']  = osc_sanitize_name( strip_tags( trim( $aItem['address'] ) ) );

            // Validate
            if ( !$this->checkAllowedExt($aItem['photos']) ) {
                $flash_error .= _m("Image with an incorrect extension.") . PHP_EOL;
            }
            if ( !$this->checkSize($aItem['photos']) ) {
                $flash_error .= _m("Image is too big. Max. size") . osc_max_size_kb() . " Kb" . PHP_EOL;
            }

            $title_message  = '';
            $td_message     = '';
            foreach(@$aItem['title'] as $key => $value) {
                if( osc_validate_text($value, 1) && osc_validate_max($value, osc_max_characters_per_title()) ) {
                    $td_message = '';
                    break;
                }

                $td_message .=
                    (!osc_validate_text($value, 1) ? _m("Title too short.") . PHP_EOL : '' ) .
                    (!osc_validate_max($value, osc_max_characters_per_title()) ? _m("Title too long.") . PHP_EOL : '' );
            }
            $flash_error .= $td_message;

            $desc_message = '';
            foreach(@$aItem['description'] as $key => $value) {
                if( osc_validate_text($value, 3) &&  osc_validate_max($value, osc_max_characters_per_description()) )  {
                    $desc_message = '';
                    break;
                }

                $desc_message .=
                    (!osc_validate_text($value, 3) ? _m("Description too short.") . PHP_EOL : '' ) .
                    (!osc_validate_max($value, osc_max_characters_per_description()) ? _m("Description too long."). PHP_EOL : '' );
            }
            $flash_error .= $desc_message;

            $flash_error .=
                ((!osc_validate_category($aItem['catId'])) ? _m("Category invalid.") . PHP_EOL : '' ) .
                ((!osc_validate_number($aItem['price'])) ? _m("Price must be a number.") . PHP_EOL : '' ) .
                ((!osc_validate_max(number_format($aItem['price'],0,'',''), 15)) ? _m("Price too long.") . PHP_EOL : '' ) .
                ((!is_null($aItem['price']) && (int)$aItem['price']<0 ) ? _m('Price must be positive number.') . PHP_EOL : '' ) .
                ((!osc_validate_text($aItem['countryName'], 3, false)) ? _m("Country too short.") . PHP_EOL : '' ) .
                ((!osc_validate_max($aItem['countryName'], 50)) ? _m("Country too long.") . PHP_EOL : '' ) .
                ((!osc_validate_text($aItem['regionName'], 2, false)) ? _m("Region too short.") . PHP_EOL : '' ) .
                ((!osc_validate_max($aItem['regionName'], 50)) ? _m("Region too long.") . PHP_EOL : '' ) .
                ((!osc_validate_text($aItem['cityName'], 2, false)) ? _m("City too short.") . PHP_EOL : '' ) .
                ((!osc_validate_max($aItem['cityName'], 50)) ? _m("City too long.") . PHP_EOL : '' ) .
                ((!osc_validate_text($aItem['cityArea'], 3, false)) ? _m("Municipality too short.") . PHP_EOL : '' ) .
                ((!osc_validate_max($aItem['cityArea'], 50)) ? _m("Municipality too long.") . PHP_EOL : '' ) .
                ((!osc_validate_text($aItem['address'], 3, false))? _m("Address too short.") . PHP_EOL : '' ) .
                ((!osc_validate_max($aItem['address'], 100)) ? _m("Address too long.") . PHP_EOL : '' );

            $_meta = Field::newInstance()->findByCategory($aItem['catId']);
            $meta = Params::getParam("meta");
            foreach($_meta as $_m) {
                $meta[$_m['pk_i_id']] = (isset($meta[$_m['pk_i_id']]))?$meta[$_m['pk_i_id']]:'';
            }
            if($meta!='' && count($meta)>0) {
                $mField = Field::newInstance();
                foreach($meta as $k => $v) {
                    if($v=='') {
                        $field = $mField->findByPrimaryKey($k);
                        if($field['b_required']==1) {
                            $flash_error .= sprintf(_m("%s field is required."), $field['s_name']) . PHP_EOL;
                        }
                    }
                }
            };

            // hook pre add or edit
            // DEPRECATED : preitem_psot will be removed in 3.4
            osc_run_hook('pre_item_post');
            osc_run_hook('pre_item_edit', $aItem);

            // Handle error
            if ($flash_error) {
                $success = $flash_error;
            } else {
                $location = array(
                    'fk_c_country_code' => $aItem['countryId'],
                    's_country'         => $aItem['countryName'],
                    'fk_i_region_id'    => $aItem['regionId'],
                    's_region'          => $aItem['regionName'],
                    'fk_i_city_id'      => $aItem['cityId'],
                    's_city'            => $aItem['cityName'],
                    's_city_area'       => $aItem['cityArea'],
                    's_address'         => $aItem['address'],
                    'd_coord_lat'       => $aItem['d_coord_lat'],
                    'd_coord_long'      => $aItem['d_coord_long'],
                    's_zip'             => $aItem['s_zip']
                );

                $locationManager = ItemLocation::newInstance();
                $old_item_location = $locationManager->findByPrimaryKey($aItem['idItem']);

                $locationManager->update( $location, array( 'fk_i_item_id' => $aItem['idItem'] ) );

                $old_item = $this->manager->findByPrimaryKey( $aItem['idItem'] );

                if($aItem['userId'] != '') {
                    $user = User::newInstance()->findByPrimaryKey( $aItem['userId'] );
                    $aItem['userId']      = $aItem['userId'];
                    $aItem['contactName'] = $user['s_name'];
                    $aItem['contactEmail'] = $user['s_email'];
                } else {
                    $aItem['userId']      = NULL;
                }

                if($aItem['price']!='') {
                    $aItem['currency'] = $aItem['currency'];
                } else {
                    $aItem['currency'] = NULL;
                }

                $aUpdate = array(
                    'dt_mod_date'         => date('Y-m-d H:i:s')
                    ,'fk_i_category_id'   => $aItem['catId']
                    ,'i_price'            => $aItem['price']
                    ,'fk_c_currency_code' => $aItem['currency']
                    ,'b_show_email'       => $aItem['showEmail']
                );

                // only can change the user if you're an admin
                if( $this->is_admin ) {
                    $aUpdate['fk_i_user_id']    = $aItem['userId'];
                    $aUpdate['s_contact_name']  = $aItem['contactName'];
                    $aUpdate['s_contact_email'] = $aItem['contactEmail'];

                } else {
                    $aUpdate['s_ip'] = $aItem['s_ip'];
                }

                $result = $this->manager->update( $aUpdate, array('pk_i_id'  => $aItem['idItem'],
                                                                  's_secret' => $aItem['secret'] ) );
                // UPDATE title and description locales
                $this->insertItemLocales( 'EDIT', $aItem['title'], $aItem['description'], $aItem['idItem'] );
                // UPLOAD item resources
                $this->uploadItemResources( $aItem['photos'], $aItem['idItem'] );

                Log::newInstance()->insertLog('item', 'edit', $aItem['idItem'], current(array_values($aItem['title'])), $this->is_admin?'admin':'user', $this->is_admin?osc_logged_admin_id():osc_logged_user_id());
                /**
                 * META FIELDS
                 */
                if($meta!='' && count($meta)>0) {
                    $mField = Field::newInstance();
                    foreach($meta as $k => $v) {
                        // if dateinterval
                        if( is_array($v) && !isset($v['from']) && !isset($v['to']) ) {
                            $v = implode(',', $v);
                        }
                        $mField->replace($aItem['idItem'], $k, $v);
                    }
                }

                $oldIsExpired = osc_isExpired($old_item['dt_expiration']);
                $dt_expiration = Item::newInstance()->updateExpirationDate($aItem['idItem'], $aItem['dt_expiration'], false);
                if($dt_expiration===false) {
                    $dt_expiration = $old_item['dt_expiration'];
                    $aItem['dt_expiration'] = $old_item['dt_expiration'];
                }
                $newIsExpired = osc_isExpired($dt_expiration);

                // Recalculate stats related with items
                $this->_updateStats($result, $old_item, $oldIsExpired, $old_item_location, $aItem, $newIsExpired, $location);

                unset($old_item);

                // THIS HOOK IS FINE, YAY!
                osc_run_hook('edited_item', Item::newInstance()->findByPrimaryKey($aItem['idItem']));
                $success = $result;
            }

            return $success;
        }
Exemple #2
0
 function edit()
 {
     $aItem = $this->data;
     $flash_error = '';
     // Initiate HTML Purifier
     require_once LIB_PATH . 'htmlpurifier/HTMLPurifier.auto.php';
     $config = HTMLPurifier_Config::createDefault();
     $config->set('HTML.Allowed', 'b,strong,i,em,u,a[href|title],ul,ol,li,p[style],br,span[style]');
     $config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
     $config->set('Cache.SerializerPath', ABS_PATH . 'oc-content/uploads');
     $purifier = new HTMLPurifier($config);
     // Sanitize
     foreach (@$aItem['title'] as $key => $value) {
         $aItem['title'][$key] = strip_tags(trim($value));
     }
     foreach (@$aItem['description'] as $key => $value) {
         $aItem['description'][$key] = $purifier->purify($value);
     }
     $aItem['price'] = !is_null($aItem['price']) ? strip_tags(trim($aItem['price'])) : $aItem['price'];
     $aItem['cityArea'] = osc_sanitize_name(strip_tags(trim($aItem['cityArea'])));
     $aItem['address'] = osc_sanitize_name(strip_tags(trim($aItem['address'])));
     // Validate
     if (!$this->checkAllowedExt($aItem['photos'])) {
         $flash_error .= _m("Image with incorrect extension.") . PHP_EOL;
     }
     if (!$this->checkSize($aItem['photos'])) {
         $flash_error .= _m("Images too big. Max. size ") . osc_max_size_kb() . " Kb" . PHP_EOL;
     }
     $title_message = '';
     $td_message = '';
     foreach (@$aItem['title'] as $key => $value) {
         if (osc_validate_text($value, 1) && osc_validate_max($value, 100)) {
             $td_message = '';
             break;
         }
         $td_message .= (!osc_validate_text($value, 1) ? _m("Title too short.") . PHP_EOL : '') . (!osc_validate_max($value, 100) ? _m("Title too long.") . PHP_EOL : '');
     }
     $flash_error .= $td_message;
     $desc_message = '';
     foreach (@$aItem['description'] as $key => $value) {
         if (osc_validate_text($value, 3) && osc_validate_max($value, 5000)) {
             $desc_message = '';
             break;
         }
         $desc_message .= (!osc_validate_text($value, 3) ? _m("Description too short.") . PHP_EOL : '') . (!osc_validate_max($value, 5000) ? _m("Description too long.") . PHP_EOL : '');
     }
     $flash_error .= $desc_message;
     $flash_error .= (!osc_validate_category($aItem['catId']) ? _m("Category invalid.") . PHP_EOL : '') . (!osc_validate_number($aItem['price']) ? _m("Price must be number.") . PHP_EOL : '') . (!osc_validate_max($aItem['price'], 15) ? _m("Price too long.") . PHP_EOL : '') . (!osc_validate_text($aItem['countryName'], 3, false) ? _m("Country too short.") . PHP_EOL : '') . (!osc_validate_max($aItem['countryName'], 50) ? _m("Country too long.") . PHP_EOL : '') . (!osc_validate_text($aItem['regionName'], 3, false) ? _m("Region too short.") . PHP_EOL : '') . (!osc_validate_max($aItem['regionName'], 50) ? _m("Region too long.") . PHP_EOL : '') . (!osc_validate_text($aItem['cityName'], 3, false) ? _m("City too short.") . PHP_EOL : '') . (!osc_validate_max($aItem['cityName'], 50) ? _m("City too long.") . PHP_EOL : '') . (!osc_validate_text($aItem['cityArea'], 3, false) ? _m("Municipality too short.") . PHP_EOL : '') . (!osc_validate_max($aItem['cityArea'], 50) ? _m("Municipality too long.") . PHP_EOL : '') . (!osc_validate_text($aItem['address'], 3, false) ? _m("Address too short.") . PHP_EOL : '') . (!osc_validate_max($aItem['address'], 100) ? _m("Address too long.") . PHP_EOL : '');
     $meta = Params::getParam("meta");
     if ($meta != '' && count($meta) > 0) {
         $mField = Field::newInstance();
         foreach ($meta as $k => $v) {
             if ($v == '') {
                 $field = $mField->findByPrimaryKey($k);
                 if ($field['b_required'] == 1) {
                     $flash_error .= sprintf(_m("%s field is required."), $field['s_name']);
                 }
             }
         }
     }
     // hook pre add or edit
     osc_run_hook('pre_item_post');
     // Handle error
     if ($flash_error) {
         return $flash_error;
     } else {
         $location = array('fk_c_country_code' => $aItem['countryId'], 's_country' => $aItem['countryName'], 'fk_i_region_id' => $aItem['regionId'], 's_region' => $aItem['regionName'], 'fk_i_city_id' => $aItem['cityId'], 's_city' => $aItem['cityName'], 's_city_area' => $aItem['cityArea'], 's_address' => $aItem['address']);
         $locationManager = ItemLocation::newInstance();
         $locationManager->update($location, array('fk_i_item_id' => $aItem['idItem']));
         // Update category numbers
         $old_item = $this->manager->findByPrimaryKey($aItem['idItem']);
         if ($old_item['fk_i_category_id'] != $aItem['catId']) {
             CategoryStats::newInstance()->increaseNumItems($aItem['catId']);
             CategoryStats::newInstance()->decreaseNumItems($old_item['fk_i_category_id']);
         }
         unset($old_item);
         $result = $this->manager->update(array('dt_mod_date' => date('Y-m-d H:i:s'), 'fk_i_category_id' => $aItem['catId'], 'i_price' => $aItem['price'], 'fk_c_currency_code' => $aItem['currency']), array('pk_i_id' => $aItem['idItem'], 's_secret' => $aItem['secret']));
         // UPDATE title and description locales
         $this->insertItemLocales('EDIT', $aItem['title'], $aItem['description'], $aItem['idItem']);
         // UPLOAD item resources
         $this->uploadItemResources($aItem['photos'], $aItem['idItem']);
         Log::newInstance()->insertLog('item', 'edit', $aItem['idItem'], current(array_values($aItem['title'])), $this->is_admin ? 'admin' : 'user', $this->is_admin ? osc_logged_admin_id() : osc_logged_user_id());
         /**
          * META FIELDS
          */
         if ($meta != '' && count($meta) > 0) {
             $mField = Field::newInstance();
             foreach ($meta as $k => $v) {
                 $mField->replace($aItem['idItem'], $k, $v);
             }
         }
         osc_run_hook('item_edit_post', $aItem['catId'], $aItem['idItem']);
         return 1;
     }
     return 0;
 }