Ejemplo n.º 1
0
 /**
  * This is a convenience method so that you don't have to code this every time
  * you want to save a simple model's data.
  *
  * It uses the assigned model to save the sent form fields.
  * If saving fails, it populates $this->_ValidationResults with validation errors & related fields.
  *
  * @return unknown
  */
 public function Save()
 {
     $SaveResult = FALSE;
     if ($this->ErrorCount() == 0) {
         if (!isset($this->_Model)) {
             trigger_error(ErrorMessage("You cannot call the form's save method if a model has not been defined.", "Form", "Save"), E_USER_ERROR);
         }
         $Args = array_merge(func_get_args(), array(NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
         $SaveResult = $this->_Model->Save($this->FormValues(), $Args[0], $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6], $Args[7], $Args[8], $Args[9]);
         if ($SaveResult === FALSE) {
             // NOTE: THE VALIDATION FUNCTION NAMES ARE ALSO THE LANGUAGE
             // TRANSLATIONS OF THE ERROR MESSAGES. CHECK THEM OUT IN THE LOCALE
             // FILE.
             $this->SetValidationResults($this->_Model->ValidationResults());
         }
     }
     return $SaveResult;
 }
Ejemplo n.º 2
0
 /**
  * Save conversation from form submission.
  * 
  * @since 2.0.0
  * @access public
  *
  * @param array $FormPostValues Values submitted via form.
  * @param object $MessageModel Message starting the conversation.
  * @return int Unique ID of conversation created or updated.
  */
 public function Save($FormPostValues, $MessageModel)
 {
     $Session = Gdn::Session();
     // Define the primary key in this model's table.
     $this->DefineSchema();
     $MessageModel->DefineSchema();
     if (!GetValue('RecipientUserID', $FormPostValues) && isset($FormPostValues['To'])) {
         $To = explode(',', $FormPostValues['To']);
         $To = array_map('trim', $To);
         $RecipientUserIDs = $this->SQL->Select('UserID')->From('User')->WhereIn('Name', $To)->Get();
         $RecipientUserIDs = ConsolidateArrayValuesByKey($RecipientUserIDs, 'UserID');
         $FormPostValues['RecipientUserID'] = $RecipientUserIDs;
     }
     // Add & apply any extra validation rules:
     $this->Validation->ApplyRule('Body', 'Required');
     $MessageModel->Validation->ApplyRule('Body', 'Required');
     // Make sure that there is at least one recipient
     $this->Validation->AddRule('OneOrMoreArrayItemRequired', 'function:ValidateOneOrMoreArrayItemRequired');
     $this->Validation->ApplyRule('RecipientUserID', 'OneOrMoreArrayItemRequired');
     // Add insert/update fields
     $this->AddInsertFields($FormPostValues);
     $this->AddUpdateFields($FormPostValues);
     // Validate the form posted values
     $ConversationID = FALSE;
     if ($this->Validate($FormPostValues) && $MessageModel->Validate($FormPostValues)) {
         $Fields = $this->Validation->ValidationFields();
         // All fields on the form that relate to the schema
         // Define the recipients, and make sure that the sender is in the list
         $RecipientUserIDs = GetValue('RecipientUserID', $Fields, 0);
         if (!in_array($Session->UserID, $RecipientUserIDs)) {
             $RecipientUserIDs[] = $Session->UserID;
         }
         // Also make sure there are no duplicates in the recipient list
         $RecipientUserIDs = array_unique($RecipientUserIDs);
         sort($RecipientUserIDs);
         $Fields = $this->Validation->SchemaValidationFields();
         // All fields on the form that relate to the schema
         $Fields['Contributors'] = Gdn_Format::Serialize($RecipientUserIDs);
         $ConversationID = $this->SQL->Insert($this->Name, $Fields);
         $FormPostValues['ConversationID'] = $ConversationID;
         $MessageID = $MessageModel->Save($FormPostValues);
         $this->SQL->Update('Conversation')->Set('FirstMessageID', $MessageID)->Where('ConversationID', $ConversationID)->Put();
         // Now that the message & conversation have been inserted, insert all of the recipients
         foreach ($RecipientUserIDs as $UserID) {
             $CountReadMessages = $UserID == $Session->UserID ? 1 : 0;
             $this->SQL->Options('Ignore', TRUE)->Insert('UserConversation', array('UserID' => $UserID, 'ConversationID' => $ConversationID, 'LastMessageID' => $MessageID, 'CountReadMessages' => $CountReadMessages, 'DateConversationUpdated' => $FormPostValues['DateUpdated']));
         }
         // And update the CountUnreadConversations count on each user related to the discussion.
         $this->UpdateUserUnreadCount($RecipientUserIDs, TRUE);
         // Add notifications (this isn't done by the conversationmessagemodule
         // because the conversation has not yet been created at the time they are
         // inserted)
         $UnreadData = $this->SQL->Select('uc.UserID')->From('UserConversation uc')->Where('uc.ConversationID', $ConversationID)->Where('uc.UserID <>', $Session->UserID)->Get();
         $ActivityModel = new ActivityModel();
         foreach ($UnreadData->Result() as $User) {
             // Notify the users of the new message.
             $ActivityID = $ActivityModel->Add($Session->UserID, 'ConversationMessage', '', $User->UserID, '', "/messages/{$ConversationID}#{$MessageID}", FALSE);
             $Story = ArrayValue('Body', $FormPostValues, '');
             $ActivityModel->SendNotification($ActivityID, $Story);
         }
     } else {
         // Make sure that all of the validation results from both validations are present for view by the form
         foreach ($MessageModel->ValidationResults() as $FieldName => $Results) {
             foreach ($Results as $Result) {
                 $this->Validation->AddValidationResult($FieldName, $Result);
             }
         }
     }
     return $ConversationID;
 }
Ejemplo n.º 3
0
echo "<pre>";
$siblingList2 = $object->GetSiblingList();
print_r($siblingList2);
echo "</pre>";
//setchildlist
$sibling2 = new sibling('new sibling1');
$sibling3 = new sibling('new sibling2');
$sibling4 = new sibling('new sibling3');
$newSiblingList = array($sibling2, $sibling3, $sibling4);
$child2 = new child('new child1');
$child3 = new child('new child2');
$child4 = new child('new child3');
$newChildList = array($child2, $child3, $child4);
$object->SetChildList($newChildList);
$object->SetSiblingList($newSiblingList);
$object->Save();
$object2 = new object();
foreach (array_keys($newChildList) as $key) {
    $newChild =& $newChildList[$key];
    $object2->AddChild($newChild);
}
//$object2->Save();
echo "<br/>object2 should have 3 children";
echo "<pre>";
print_r($object2);
echo "</pre>";
//setsiblinglist
//testing deletelist
//$object->DeleteList(array(array("attribute", "=", "obj att"), array("objectId", ">", 20)),true );
//essentials
//save   - ok  (deep) watch out for foreach childrenlist/siblinglist
 /**
  * DNF
  *
  */
 public function testGetList_Or()
 {
     $this->object->Save();
     $newObject1 = new object("newobject1 att");
     $newObject2 = new object("newobject2 att");
     $newObject3 = new object("newobject3 att");
     $newObject1->Save();
     $newObject2->Save();
     $newObject3->Save();
     $objectList = $this->object->GetList(array(array("attribute", "=", "obj att")));
     $this->assertEquals(1, sizeof($objectList));
     $objectList = $this->object->GetList(array(array("attribute", "=", "obj att"), array("or"), array("attribute", "=", "newobject1 att")));
     $this->assertEquals(2, sizeof($objectList));
     $objectList = $this->object->GetList(array(array("attribute", "=", "obj att"), array("or"), array("attribute", "=", "newobject1 att"), array("or"), array("attribute", "=", "newobject2 att")));
     $this->assertEquals(3, sizeof($objectList));
 }
Ejemplo n.º 5
0
 /**
  *   Create and populate an Order record for this purchase.
  *   Gets the billto and shipto addresses from the cart, if any.
  *   Items are saved in the purchases table by handlePurchase().
  *
  *   This function is called only by our own handlePurchase() function,
  *   but is made "protected" so a derived class can use it if necessary.
  *
  *   @return string  Order ID, to link to the purchases table
  */
 protected function CreateOrder()
 {
     global $_TABLES, $_PP_CONF;
     // See if an order already exists for this transaction.
     // If so, load it and update the status. If not, continue on
     // and create a new order
     $order_id = DB_getItem($_TABLES['paypal.orders'], 'order_id', "pmt_txn_id='" . DB_escapeString($this->pp_data['txn_id']) . "'");
     if (!empty($order_id)) {
         $this->Order = new ppOrder($order_id);
         if ($this->Order->order_id != '') {
             $this->Order->log_user = $this->gw->Description();
             $this->Order->UpdateStatus($this->pp_data['status']);
         }
         return 2;
     }
     $this->Order = new ppOrder();
     USES_paypal_class_cart();
     if (isset($this->pp_data['custom']['cart_id'])) {
         $cart = new ppCart($this->pp_data['custom']['cart_id']);
         if (!$_PP_CONF['sys_test_ipn'] && !$cart->hasItems()) {
             return 1;
             // shouldn't normally be empty except during testing
         }
     } else {
         $cart = NULL;
     }
     $uid = (int) $this->pp_data['custom']['uid'];
     $this->Order->uid = $uid;
     $this->Order->status = !empty($this->pp_data['status']) ? $this->pp_data['status'] : 'pending';
     if ($uid > 1) {
         USES_paypal_class_userinfo();
         $U = new ppUserInfo($uid);
     }
     // Get the billing and shipping addresses from the cart record,
     // if any.  There may not be a cart in the database if it was
     // removed by a previous IPN, e.g. this is the 'completed' message
     // and we already processed a 'pending' message
     if ($cart) {
         $BillTo = $cart->getAddress('billto');
     }
     if (empty($BillTo) && $uid > 1) {
         $BillTo = $U->getDefaultAddress('billto');
     }
     if (is_array($BillTo)) {
         $this->Order->setBilling($BillTo);
     }
     $ShipTo = $this->pp_data['shipto'];
     if (empty($ShipTo)) {
         if ($cart) {
             $ShipTo = $cart->getAddress('shipto');
         }
         if (empty($ShipTo) && $uid > 1) {
             $ShipTo = $U->getDefaultAddress('shipto');
         }
     }
     if (is_array($ShipTo)) {
         $this->Order->setShipping($ShipTo);
     }
     if (isset($this->pp_data['shipto']['phone'])) {
         $this->Order->phone = $this->pp_data['shipto']['phone'];
     }
     $this->Order->pmt_method = $this->gw_id;
     $this->Order->pmt_txn_id = $this->pp_data['txn_id'];
     $this->Order->tax = $this->pp_data['pmt_tax'];
     $this->Order->shipping = $this->pp_data['pmt_shipping'];
     $this->Order->handling = $this->pp_data['pmt_handling'];
     $this->Order->buyer_email = $this->pp_data['payer_email'];
     $this->Order->log_user = $this->gw->Description();
     $order_id = $this->Order->Save();
     $db_order_id = DB_escapeString($order_id);
     $this->Order->items = array();
     foreach ($this->items as $id => $item) {
         $options = DB_escapeString($item['options']);
         list($item_number, $options) = explode('|', $item['item_number']);
         //if (is_numeric($item['item_number'])) {
         if (is_numeric($item_number)) {
             // For Paypal catalog options, check for options and append
             // to the description.  Update quantity on hand if tracking
             // is enabled.  These actions don't apply to items from
             // other plugins.
             if (!empty($options)) {
                 // options is expected as CSV
                 $sql = "SELECT attr_value\n                            FROM {$_TABLES['paypal.prod_attr']}\n                            WHERE attr_id IN ({$options})";
                 $optres = DB_query($sql);
                 $opt_str = '';
                 while ($O = DB_fetchArray($optres, false)) {
                     $opt_str .= ', ' . $O['attr_value'];
                 }
                 $item['name'] .= $opt_str;
             }
             /*$sql = "UPDATE {$_TABLES['paypal.products']} SET
               onhand = GREATEST(0, onhand - " . 
                   (int)$item['quantity'] . ") 
               WHERE id = '" . (int)$item['item_number'] .
               "' AND track_onhand > 0";*/
             //COM_errorLog($sql);
             DB_query($sql, 1);
         }
         $sql = "INSERT INTO {$_TABLES['paypal.purchases']} SET \n                    order_id = '{$db_order_id}',\n                    product_id = '{$item['item_number']}',\n                    description = '" . DB_escapeString($item['name']) . "',\n                    quantity = '{$item['quantity']}', \n                    user_id = '{$this->pp_data['custom']['uid']}', \n                    txn_type = '{$this->pp_data['custom']['transtype']}',\n                    txn_id = '{$this->pp_data['txn_id']}', \n                    purchase_date = '{$this->sql_date}', \n                    status = 'pending',\n                    token = '" . md5(time()) . "',\n                    price = " . (double) $item['price'] . ",\n                    options = '{$options}'";
         // add an expiration date if appropriate
         if (is_numeric($item['expiration']) && $item['expiration'] > 0) {
             $sql .= ", expiration = DATE_ADD('{$_PP_CONF['now']}', INTERVAL {$item['expiration']} DAY)";
         }
         PAYPAL_debug($sql);
         DB_query($sql);
     }
     // foreach item
     // Reload the order to get the items
     $this->Order->Load();
     // If this was a user's cart, then clear that also
     if (isset($this->pp_data['custom']['cart_id']) && !empty($this->pp_data['custom']['cart_id'])) {
         if (!$_PP_CONF['sys_test_ipn']) {
             DB_delete($_TABLES['paypal.cart'], 'cart_id', $this->pp_data['custom']['cart_id']);
             PAYPAL_debug('Cart ' . $this->pp_data['custom']['cart_id'] . ' deleted');
         }
     } else {
         PAYPAL_debug('no cart to delete');
     }
     return 0;
 }