示例#1
0
 /**
  * Actions to perform after saving a course
  *
  * @param      object  $model \Components\Courses\Models\Course
  * @param      boolean $isNew Is this a newly created entry?
  * @return     void
  */
 public function onOfferingSave($model)
 {
     if (!$model->exists()) {
         return;
     }
     $params = new \Hubzero\Config\Registry($model->get('params'));
     if ($params->get('store_product', 0)) {
         $course = \Components\Courses\Models\Course::getInstance($model->get('course_id'));
         $title = $course->get('title') . ' (' . $model->get('title') . ')';
         $description = $course->get('blurb');
         $price = $params->get('store_price', '30.00');
         $duration = $params->get('store_membership_duration', '1 YEAR');
         if (!$params->get('store_product_id', 0)) {
             include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Course.php';
             $product = new StorefrontModelCourse();
             $product->setName($title);
             $product->setDescription($description);
             $product->setPrice($price);
             // We don't want products showing up for non-published courses
             if ($model->get('state') != 1) {
                 $product->setActiveStatus(0);
             } else {
                 $product->setActiveStatus(1);
             }
             // Membership model: membership duration period (must me in MySQL date format: 1 DAY, 2 MONTH, 3 YEAR...)
             $product->setTimeToLive($duration);
             // Course alias id
             $product->setCourseId($course->get('alias'));
             $product->setOfferingId($model->get('alias'));
             try {
                 // Returns object with values, pId is the new product ID to link to
                 $info = $product->add();
                 $params->set('store_product_id', $info->pId);
                 $model->set('params', $params->toString());
                 $model->store();
             } catch (Exception $e) {
                 $this->setError('ERROR: ' . $e->getMessage());
             }
         } else {
             $warehouse = new StorefrontModelWarehouse();
             try {
                 // Get course by pID returned with $course->add() above
                 $product = $warehouse->getCourse($params->get('store_product_id', 0));
                 $product->setName($title);
                 $product->setDescription($description);
                 $product->setPrice($price);
                 $product->setTimeToLive($duration);
                 if ($model->get('state') != 1) {
                     $product->setActiveStatus(0);
                 } else {
                     $product->setActiveStatus(1);
                 }
                 $product->update();
             } catch (Exception $e) {
                 $this->setError('ERROR: ' . $e->getMessage());
             }
         }
     }
 }
示例#2
0
 /**
  * Display default page
  *
  * @return     void
  */
 public function testgroundTask()
 {
     if (0) {
         // CREATE COUPON
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'StorefrontModelCoupon.php';
         try {
             // Constructor take the coupon code
             $coupon = new StorefrontModelCoupon('hui');
             // Coupon description (shows up in the cart)
             $coupon->setDescription('Test coupon, 10% off product with ID 3');
             // Expiration date
             $coupon->setExpiration('Feb 22, 2022');
             // Number of times coupon can be used (unlimited by default)
             $coupon->setUseLimit(1);
             // Product the coupon will be applied to:
             // first parameter: product ID
             // second parameter [optional, unlimited by default]: max quantity of products coupon will be applied to (if buying multiple)
             $coupon->addObject(3, 1);
             // Action, only 'discount' for now
             // second parameter either percentage ('10%') or absolute dollar value ('20')
             $coupon->setAction('discount', '10%');
             // Add coupon
             $coupon->add();
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // DELETE COUPON
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         try {
             $warehouse->deleteCoupon('couponcode3');
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // CREATE NEW COURSE
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Course.php';
         $course = new StorefrontModelCourse();
         $course->setName('Name of the course');
         $course->setDescription('Short description');
         $course->setPrice(12.0);
         $course->addToCollection('courses');
         // Membership model: membership duration period (must me in MySQL date format: 1 DAY, 2 MONTH, 3 YEAR...)
         $course->setTimeToLive('1 YEAR');
         // Course alias id
         $course->setCourseId('nanoscaletransistors');
         try {
             // Returns object with values, pId is the new product ID to link to
             $info = $course->add();
             //print_r($info);
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // GET EXISTING COURSE, modify it and save
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         try {
             // Get course by pID returned with $course->add() above
             $course = $warehouse->getCourse(1);
             $course->setName('Renamed');
             $course->setDescription('New description');
             $course->setPrice(55.22);
             $course->setTimeToLive('10 YEAR');
             $course->update();
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // UPDATE COURSE by recreating it
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'StorefrontModelCourse.php';
         $course = new StorefrontModelCourse();
         $course->setName('Operations Management 104');
         $course->setDescription('Operations Management 104 is some kind of test course for now...');
         $course->setPrice(13.05);
         $course->setCourseId(5);
         // Existing course ID (pID returned with $course->add() when the course was created). Must be set to be able to update.
         $course->setId(1023);
         try {
             $info = $course->update();
             //print_r($info);
         } catch (Exception $e) {
             echo 'ERROR: ' . $e->getMessage();
         }
         return;
     }
     if (0) {
         // DELETE COURSE
         include_once PATH_CORE . DS . 'components' . DS . 'com_storefront' . DS . 'models' . DS . 'Warehouse.php';
         $warehouse = new StorefrontModelWarehouse();
         // Delete by existing course ID (pID returned with $course->add() when the course was created)
         $warehouse->deleteProduct(1023);
         return;
     }
 }