if ($result == $building_id) {
         $updated = true;
         $message = __('Image(s) stored successfully.', 'idp-theme');
     } else {
         if ($result == -1) {
             $message = __('There was an error saving one of the images.', 'idp-theme');
         } else {
             if ($result == -2) {
                 $message = __('At least one of the images was of the wrong filetype and/or exceeded the file size limit.', 'idp-theme');
             }
         }
         $error = true;
     }
 } else {
     if (isset($_POST['set_main_image_nonce']) && wp_verify_nonce($_POST['set_main_image_nonce'], 'set_main_image')) {
         $result = Idp_Main::setMainImage($_POST['building_id'], $_POST['image_id'], $user_id);
         if ($result === false) {
             $error = true;
         } else {
             $updated = true;
             $message = __('Main image changed.', 'idp-theme');
         }
     } else {
         if (isset($_POST['delete_image_nonce']) && wp_verify_nonce($_POST['delete_image_nonce'], 'delete_image')) {
             $result = Idp_Main::deleteBuildingImage($_POST['building_id'], $_POST['image_id'], $user_id);
             if ($result === false) {
                 $error = true;
             } else {
                 $updated = true;
                 $message = __('Image deleted.', 'idp-theme');
             }
Beispiel #2
0
 /**
  * Upload images for given building 
  * Return building id on success
  * 
  * @author Christopher Siewert
  * @return $building_id
  */
 public static function storeImages($building_id, $user_id)
 {
     require_once ABSPATH . 'wp-admin/includes/image.php';
     require_once ABSPATH . 'wp-admin/includes/file.php';
     require_once ABSPATH . 'wp-admin/includes/media.php';
     $error = false;
     $filetype_error = false;
     global $wpdb;
     $images = [];
     if ($_FILES) {
         $files = $_FILES["images"];
         foreach ($files['name'] as $key => $value) {
             if ($files['name'][$key]) {
                 $file = array('name' => $files['name'][$key], 'type' => $files['type'][$key], 'tmp_name' => $files['tmp_name'][$key], 'error' => $files['error'][$key], 'size' => $files['size'][$key]);
                 $uploaded_file_type = $files['type'][$key];
                 $allowed_file_types = array('image/jpg', 'image/jpeg', 'image/JPEG', 'image/JPG');
                 if (in_array($uploaded_file_type, $allowed_file_types) && $file['size'] <= 3000000) {
                     $_FILES = array("images" => $file);
                     foreach ($_FILES as $file => $array) {
                         $images[] = media_handle_upload($file, 0);
                     }
                 } else {
                     $filetype_error = true;
                 }
             }
         }
     }
     $main_image = false;
     $building = Idp_Main::getBuildingBasicInfos($building_id);
     if ($building['main_image'] != null) {
         $main_image = true;
     }
     foreach ($images as $image_id) {
         if (is_wp_error($image_id)) {
             $error = true;
         } else {
             $success = $wpdb->insert($wpdb->prefix . 'image', array('building' => $building_id, 'attachment_id' => $image_id), array('%d', '%d'));
             if (!$main_image) {
                 Idp_Main::setMainImage($building_id, $image_id, $user_id);
                 $main_image = true;
             }
         }
     }
     $wpdb->update($result = $wpdb->prefix . 'building', array('update_user' => $user_id, 'update_date' => current_time('mysql')), array('id' => $building_id), array('%d', '%s'), array('%d'));
     //reset status to 'draft'. Attribute categories have to be set to complete the building and thus be able to submit it
     if ($building['status'] == 'complete' && !Idp_Main::categoriesSetAllImages($building_id)) {
         Idp_Main::updateBuildingStatus($building_id, 'draft');
     }
     if ($error) {
         // There was an error uploading one of the images.
         return -1;
     } else {
         if ($filetype_error) {
             //at least one image was of a not allowed file type
             return -2;
         } else {
             // The images were uploaded successfully!
             return $building_id;
         }
     }
 }