Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | Copyright (c) 1997-2004 The PHP Group                                |
4
// +----------------------------------------------------------------------+
5
// | This source file is subject to version 3.0 of the PHP license,       |
6
// | that is bundled with this package in the file LICENSE, and is        |
7
// | available through the world-wide-web at the following url:           |
8
// | http://www.php.net/license/3_0.txt.                                  |
9
// | If you did not receive a copy of the PHP license and are unable to   |
10
// | obtain it through the world-wide-web, please send a note to          |
11
// | license@php.net so we can mail you a copy immediately.               |
12
// +----------------------------------------------------------------------+
13
// | Author: Carsten Harnisch <phpat@intradesys.com>                      |
14
// +----------------------------------------------------------------------+
15
 
16
// $Id: EbatNs_QueryBase.php 1182 2009-03-17 12:41:24Z lautsch $:
17
require_once 'EbatNs_Defines.php';
18
require_once 'EbatNs_ApiCaller.php';
19
require_once 'EbatNs_Result.php';
20
require_once 'EbatNs_QueryResultInfo.php';
21
require_once 'EbatNs_Session.php';
22
/**
23
 *
24
 * @see EbatNs_ItemSellerQuery
25
 * @see EbatNs_OrderQuery
26
 * @see EbatNs_BidQuery
27
 * @see EbatNs_DisputeQuery
28
 * @see EbatNs_CategoryCSQuery
29
 * @see EbatNs_ProductQuery
30
 * @see EbatNs_UserQuery
31
 * @see EbatNs_FeatureQuery
32
 * @see EbatNs_AccessRuleQuery
33
 * @see EbatNs_ItemBidderQuery
34
 * @see EbatNs_CrossPromotionQuery
35
 * @see EbatNs_ShippingRateQuery
36
 * @see EbatNs_HighBidQuery
37
 * @see EbatNs_MyEbayListQuery
38
 * @see EbatNs_TaxJurisdictionQuery
39
 * @see EbatNs_ProductFinderStyleSheetQuery
40
 * @see EbatNs_CharacteristicSetQuery
41
 * @see EbatNs_AdLeadQuery
42
 * @see EbatNs_EventQuery
43
 * @see EbatNs_BestOfferQuery
44
 * @see EbatNs_AccountQuery
45
 * @see EbatNs_CategoryQuery
46
 * @see EbatNs_ProductSearchPageQuery
47
 * @see EbatNs_CustomCategoryQuery
48
 * @see EbatNs_RecommendationQuery
49
 * @see EbatNs_TransactionByItemQuery
50
 * @see EbatNs_PreferenceSetQuery
51
 * @see EbatNs_OutageScheduleQuery
52
 * @see EbatNs_MemberMessageQuery
53
 * @see EbatNs_AttributesStyleSheetQuery
54
 * @see EbatNs_ProductFinderQuery
55
 * @see EbatNs_PromotionRuleQuery
56
 * @see EbatNs_FeedbackQuery
57
 * @see EbatNs_ItemCategoryQuery
58
 * @see EbatNs_CategorySuggestedQuery
59
 * @see EbatNs_ItemQuery
60
 * @see EbatNs_ProductSellingPageQuery
61
 * @see EbatNs_ProductFamilyQuery
62
 * @see EbatNs_ItemShippingQuery
63
 * @see EbatNs_DescriptionTemplateQuery
64
 * @see EbatNs_ItemWatchlistQuery
65
 * @see EbatNs_DetailQuery
66
 * @see EbatNs_NotificationPreferenceQuery
67
 * @see EbatNs_TransactionQuery
68
 */
69
class EbatNs_QueryBase {
70
  // this array holds all attribute data of the object
71
  var $_props = array();
72
  /**
73
   * sets a property by name and value
74
   */
75
  function _setProp($key, $value)
76
  {
77
    $this->_props[$key] = $value;
78
  }
79
  /**
80
   * gets a property by name
81
   */
82
  function _getProp($key)
83
  {
84
    return $this->_props[$key];
85
  }
86
  /**
87
   *
88
   * @access private
89
   * @var Ebay _ApiCaller
90
   */
91
  var $_apiCaller = EbatNs_NOTHING;
92
  /**
93
   *
94
   * @access private
95
   * @var array
96
   */
97
  var $_resultList = array();
98
  /**
99
   *
100
   * @access private
101
   * @var Ebay _QueryResultInfo
102
   */
103
  var $_resultInfo = null;
104
  /**
105
   * Gives a way to limit the size of the resultList. Acutally if set the attribute will break out when this limit is reached. Anyway the breakout will only happen after the current page is retrieved, e.g. using a pagesize of 100 and a limit of 90 might result in a list containing 100 elements anyway.
106
   * 0 (zero) mean no limit.
107
   *
108
   * @access private
109
   * @var number
110
   */
111
  var $_resultLimit = 0;
112
  /**
113
   *
114
   * @access private
115
   * @var number
116
   */
117
  var $_pageSize = null;
118
  /**
119
   *
120
   * @access private
121
   * @var Ebay _Session
122
   */
123
  var $_session = EbatNs_NOTHING;
124
  /**
125
   * Standard init function, should be called from the constructor(s)
126
   */
127
  function _init()
128
  {
129
  }
130
  /**
131
   * Standard method to do a query. Setup the apropricate query object and specifiy the filter through the properties, then call Query to get back a Result.
132
   *
133
   * @access public
134
   * @param number $detailLevel
135
   * @param number $aPageSize could be used to overwrite the standard page-size given in the session. e.g. QueryItem (->GetSearchResult) normally works with a pageSize of 100
136
   * @param number $delta defines the direction how the various page will get called.
137
   * Here 1 is the default so the pages will get retrieve with ascending direction. If you specify -1 the data retrieval will be done with descending direction (so last page first).
138
   * @param number $currentPage specifies the starting page for the retrieval. This makes only sense in conjunction with a -1 delta.
139
   * @return Ebay _Result
140
   */
141
  function Query($detailLevel = 0, $aPageSize = null, $delta = 1, $currentPage = null)
142
  {
143
    if ($delta == -1) {
144
      if ($currentPage == null) {
145
        die("please give a currentPage also when using a reverse retrieval.");
146
      }
147
      $curPage = $currentPage;
148
    }else {
149
      // Older versions had a 0 here. As most calls are defaulting to 1 if giving
150
      // a 0 the bug did not show up.
151
      $curPage = 1;
152
    }
153
    if ($aPageSize != null) {
154
      $pageSize = $aPageSize;
155
    }else {
156
      if ($this->_pageSize != null) {
157
        $pageSize = $this->_pageSize;
158
      }else {
159
        $pageSize = $this->_session->getPageSize();
160
      }
161
    }
162
    if ($pageSize <= 0) {
163
      // be sure to have a minimal pageSize of 1
164
      // make not really sense, but is an error proven value
165
      $pageSize = 1;
166
    }
167
    /**
168
     * let the user choose the pagesize he wants
169
     * if ($pageSize != 25 || $pageSize != 50 || $pageSize != 100 || $pageSize != 200) {
170
     * $pageSize = 200;
171
     * }
172
     */
173
    $res = $this->QueryWithPages($detailLevel, $curPage, $pageSize);
174
    // token-mode
175
    if ($res->getHasRefreshedToken()) {
176
      $s = $this->_session;
177
      $s->setRequestToken($res->getRefreshedToken());
178
      $ap = $this->_apiCaller;
179
      $s = $ap->_session;
180
      $s->setRequestToken($res->getRefreshedToken());
181
    }
182
    if ($res->isGood()) {
183
      $curPage = $curPage + $delta;
184
      // we had a major bug here preventing loading the last page
185
      // if the call returned more than 200 results.
186
      while ($res->getNumberOfPages() >= $curPage) {
187
        $res = $this->QueryWithPages($detailLevel, $curPage, $pageSize);
188
        // token-mode
189
        if ($res->getHasRefreshedToken()) {
190
          $s = $this->_session;
191
          $s->setRequestToken($res->getRefreshedToken());
192
          $ap = $this->_apiCaller;
193
          $s = $ap->_session;
194
          $s->setRequestToken($res->getRefreshedToken());
195
        }
196
        if ($res->anyErrors()) {
197
          // we had errors so finish the loop
198
          break;
199
        }
200
        if ($res->hasUserBreak()) {
201
          // the retrieval process was aborted
202
          // e.g. the data limit had reached
203
          break;
204
        }
205
        $curPage = $curPage + $delta;
206
      }
207
    }
208
    return $res;
209
  }
210
  /**
211
   *
212
   * @access public
213
   * @param number $detailLevel
214
   * @param number $curPage
215
   * @param number $pageSize
216
   * @return Ebay _Result
217
   */
218
  function QueryWithPages($detailLevel, $curPage, $pageSize) // prepared for overwrite, should not be called directly
219
  {
220
    die("prepared for overwrite, no direct call here");
221
  }
222
  /**
223
   *
224
   * @access public
225
   * @return array
226
   */
227
  function getResultList()
228
  {
229
    return $this->_resultList;
230
  }
231
  /**
232
   *
233
   * @access public
234
   * @return <unspecified>
235
   */
236
  function getResultInfo()
237
  {
238
    return $this->_resultInfo;
239
  }
240
  /**
241
   *
242
   * @access public
243
   * @param Ebay $ _Session $session
244
   * @return void
245
   */
246
  function EbatNs_QueryBase($session)
247
  {
248
    // call to initialisation
249
    // (be sure to call this always on the actual class and prevent any overwriting)
250
    EbatNs_QueryBase::_init();
251
    $this->_session = $session;
252
    $this->_apiCaller = &new EbatNs_ApiCaller($this->_session);
253
  }
254
  /**
255
   * helper function that adds the current objects properties' value to the params array if the property was set
256
   *
257
   * @access public
258
   * @param array $params
259
   * @param string $key
260
   * @param string $datatype use one of the following types :
261
   * default = maps on the data provided in the property
262
   * cdata = encloses the data into a CDATA Section
263
   * boolean = transform data to 0/false or 1/true
264
   * flattenarray = transforms an array property to a flatten list devided by comma
265
   * flattenarray_cdata = transforms an array property to a flatten list devided by comma, all enclosed into a CDATA
266
   * array2xmlstruct = lets xmlTag be the element-name of an surrounding xml-tag, $key is handled as the key for an array property for each entry an extra row is added
267
   * @param string $classPropertyName specifies the name of the property in the object (so _props array index). If not given the parameter key is assumed as the property name also.
268
   * @param string $xmlTag give the ability to surround array data in given tag, used in conjunction with datatype = 'array2xmlstruct'
269
   * @return void
270
   */
271
  function addToParamsIfSet(&$params, $key, $datatype = 'default', $classPropertyName = null, $xmlTag = null)
272
  {
273
    if ($classPropertyName != null) {
274
      // get the data from the index given in $classPropertyName
275
      $thePropValue = $this->_getProp($classPropertyName);
276
      $thePropKey = $classPropertyName;
277
    }else {
278
      $thePropValue = $this->_getProp($key);
279
      $thePropKey = $key;
280
    }
281
    if (array_key_exists($thePropKey, $this->_props)) {
282
      if ($classPropertyName != null) {
283
        // get the data from the index given in $classPropertyName
284
        $thePropValue = $this->_getProp($classPropertyName);
285
      }else {
286
        $thePropValue = $this->_getProp($key);
287
      }
288
      if ($thePropValue != EbatNs_NOTHING) {
289
        switch ($datatype) {
290
          case 'default':
291
            $params["$key"] = $thePropValue;
292
            break;
293
          case 'cdata':
294
            // TODO
295
            // make the right encoding here !
296
            $params["$key"] = "<![CDATA[" . $thePropValue . "]]" . ">";
297
            break;
298
          case 'boolean':
299
          case 'bool':
300
            if ($thePropValue) {
301
              $params["$key"] = "1";
302
            }else {
303
              $params["$key"] = "0";
304
            }
305
            break;
306
          case 'flattenarray': {
307
              if (count($thePropValue)) {
308
                $vals = array_values($thePropValue);
309
                $params["$key"] = implode(",", $vals);
310
              }
311
            }
312
            break;
313
          case 'flattenarray_cdata': {
314
              if (count($thePropValue)) {
315
                $vals = array_values($thePropValue);
316
                $params["$key"] = "<![CDATA[" . implode(",", $vals) . "]]" . ">";
317
              }
318
            }
319
            break;
320
          case 'array2xmlstruct': {
321
              $dataToAdd = "";
322
              foreach ($thePropValue as $entry) {
323
                $dataToAdd = '<' . $key . '>' . $entry . '</' . $key . '>';
324
              }
325
              $params["$xmlTag"] = $dataToAdd;
326
            }
327
            break;
328
        }
329
      }
330
    }else {
331
      print_r("Do not have the property " . $key . "(addToParamsIfSet)");
332
    }
333
  }
334
  /**
335
   * rounds up to the next integer number
336
   *
337
   * @access private
338
   * @param number $x
339
   * @return number
340
   */
341
  function _roundUp($x)
342
  {
343
    $r = round($x);
344
    $d = ($x - $r);
345
    if ($d > 0) {
346
      $x = ($r + 1);
347
    }
348
    return $x;
349
  }
350
  /**
351
   * Gives a way to limit the size of the resultList. Acutally if set the attribute will break out when this limit is reached. Anyway the breakout will only happen after the current page is retrieved, e.g. using a pagesize of 100 and a limit of 90 might result in a list containing 100 elements anyway.
352
   *
353
   * @access public
354
   * @param number $value
355
   * @return void
356
   */
357
  function setResultLimit($value)
358
  {
359
    $this->_resultLimit = $value;
360
  }
361
  /**
362
   * Checks if the size of the current resultList should break the operation. This function will check againt _resultLimit set by setResultLimit (@see setResultLimit).
363
   * checkLimiter will return true (1) if the current operation has to aborted (the flag in the result object will be set accordently) or false (0) if the limit is not reached and the retrieval should be continued.
364
   *
365
   * @access public
366
   * @param Ebay $ _Result $res
367
   * @return boolean
368
   */
369
  function checkLimiter(&$res)
370
  {
371
    if ($this->_resultLimit > 0) {
372
      if (count($this->_resultList) >= $this->_resultLimit) {
373
        $res->setUserBreak();
374
        return true;
375
      }
376
    }
377
    return false;
378
  }
379
  /**
380
   * Regarding all paged resultsets the size of the page is normally retrieved by property PageSize within the Session object. Some calls needs to be adapted to new limits (e.g. GetSearchResults, as here the limit is 100). If using setPageSize the default from the session is overwriten by this value.
381
   *
382
   * @access public
383
   * @param number $value
384
   * @return void
385
   */
386
  function setPageSize($value)
387
  {
388
    $this->_pageSize = $value;
389
  }
390
}
391
 
392
?>