Added contact offers.
[readifood.git] / lib / functions.php
index eda4153..985c02b 100644 (file)
     return $q->orderByDate()->find();
   }
 
+  function get_available_offers($contact = null) {
+    $q = new OfferQuery;
+    $q->where("(valid_from is null or valid_from<=now())");
+    $q->where("(valid_to is null or valid_to>now())");
+    if (! is_null($contact)) {
+      $qq = new OfferStateQuery;
+      $qq->filterByContactId($contact->getId());
+      $ids = Array();
+      foreach ($qq->find() as $state) $ids[] = $state->getOfferId();
+      if (count($ids)) $q->_or()->filterById($ids);
+    }
+    return $q->orderById()->find();
+  }
+
+  function get_offer_state($contact, $offer) {
+    $q = new OfferStateQuery;
+    $q->filterByOfferId($offer->getId());
+    $q->filterByContactId($contact->getId());
+    return $q->orderByUpdated('desc')->findOne();
+  }
+
   function get_user_by_contact_id($id, $verbose = true) {
     $q = new UserQuery;
     $user = $q->findOneByContactId($id);