我正在开发一个 PHP 购物清单功能/接口。

4

返回一个购物清单对象数组,可以是某个购物者的清单,也可以是标有“建议”标志为'Y'的清单。

当我手动将令牌添加到$_GET中时,返回一个空数组。[{},{}] 但如果我使用var_dump($shopper_list),我会得到两个不同用户的列表。这应该只有一个,因为一个登录应该等于一个令牌:

[{},{}]
array(2) {
  [0]=>
  object(UserShoppingList)#4 (14) {
    ["sign_in_token":protected]=>
    NULL
    ["Shopper":protected]=>
    NULL
    ["ID":protected]=>
    string(1) "4"
    ["SHOPPING_LIST_NAME":protected]=>
    string(18) "kj's shopping list"
    ["SUGGESTION":protected]=>
    string(1) "N"
    ["SEQUENCE":protected]=>
    NULL
    ["ShoppingListItem":protected]=>
    NULL
    ["api_url":protected]=>
    NULL
    ["table":"Base":private]=>
    string(23) "jfw_shopping_list_names"
    ["procedure":"Base":private]=>
    NULL
    ["procedure_args":"Base":private]=>
    NULL
    ["keys":"Base":private]=>
    array(1) {
      ["ID"]=>
      NULL
    }
    ["alias_db_to_php":"Base":private]=>
    array(0) {
    }
    ["alias_php_to_db":"Base":private]=>
    array(0) {
    }
  }
  [1]=>
  object(UserShoppingList)#5 (14) {
    ["sign_in_token":protected]=>
    NULL
    ["Shopper":protected]=>
    NULL
    ["ID":protected]=>
    string(1) "2"
    ["SHOPPING_LIST_NAME":protected]=>
    string(20) "bonner shopping list"
    ["SUGGESTION":protected]=>
    string(1) "N"
    ["SEQUENCE":protected]=>
    NULL
    ["ShoppingListItem":protected]=>
    NULL
    ["api_url":protected]=>
    NULL
    ["table":"Base":private]=>
    string(23) "jfw_shopping_list_names"
    ["procedure":"Base":private]=>
    NULL
    ["procedure_args":"Base":private]=>
    NULL
    ["keys":"Base":private]=>
    array(1) {
      ["ID"]=>
      NULL
    }
    ["alias_db_to_php":"Base":private]=>
    array(0) {
    }
    ["alias_php_to_db":"Base":private]=>
    array(0) {
    }
  }
}

我的php页面(get_shopping_lists.php)如下所示:

/*
    This script is used to return a JSON array of ShoppingList objects.
    Those objects can be limited to either only "suggested" ShoppingList names,
    or to all of the lists for a single Shopper.  So, either the shopper_id or
    the boolean "suggested_only" should be set.
*/
if(!isset($_GET['token']) && !isset($_GET['suggested_only'])) {
    die('Must pass-in either a \'token\' or \'suggested_only\' flag');
}

if(isset($_GET['token'])) {
    $shopper = new Shopper($_GET['token'])
    or die('Could not instantiate a new Shopper from the \'token\' passed-in');

    $shopper_lists = $shopper->get_lists(true);
    echo json_encode($shopper_lists);
}

if(isset($_GET['suggested_only']) && $_GET['suggested_only'] == 'true') {

}

我希望您能翻译以下内容:我想返回一个购物清单对象的数组,可以为购物者返回(在这种情况下,您需要向购物者类的“get_lists”函数传递一个令牌),或者返回具有“suggested”标志=‘Y’的列表,如果您通过另一个参数传递来表示您只想要建议的列表。每个选项都会以不同的方式调用框架中的部分。
购物者类:
// Most objects in this framework are populated by calling the constructor, but
// this one has a variety of entry points. They don't do any sanity checking
// with eachother, so you can have $user->create and $user->register refer to
// completely different rows.
class Shopper extends Base {

    protected $shopper_id;
    protected $email;
    protected $user_name;
    protected $temp_token;
    protected $sign_in_token;

    protected $UserShoppingList;

    function __construct($email = null) {
        // For testing use only. Declined to wrap in this_is_dev because I
        // foresee using it somewhere in the code, pushing live, and being 
//        parent::__construct('jfw_shoppers', array('SHOPPER_ID' => $shopper_id));

        // Allow them to pass an e-mail address or the token
        if (strpos($email, '@') === false) {
            $this->sign_in_token = $email;
        } else {
            $this->email = $email;
        }
    }

    // todo: need a new function to do the actual activation.
    public function activate($temp_token) {
        global $db;

        $this->set_temp_token($temp_token);

        $vars = array();
        $vars[] = array(':i_temp_token', $this->get_temp_token());

        // Returns a Y or N
        return $db->get_function_as_proc('custom.japi_shopper_identity.Activate_User(:i_temp_token)', $vars) == 'Y';
    }

    public function create($password) {
        global $db;

        if (!$this->get_email() || !$this->get_username()) {
            return false;
        }

        $vars = array();
        $vars[] = array(':email', $this->get_email());
        $vars[] = array(':username', $this->get_username());
        $vars[] = array(':password', $password);

        $id = $db->get_function_as_proc('custom.japi_shopper_identity.create_user(:email, :username,  :password)', $vars);
        $this->set_id($id);

        // If it failed, it'll puke on the procedure. If we've come this far, we
        // know it worked.
        return true;
    }

    public function get_email() {
        return $this->email;
    }

    private function get_id() {
        if (isset($this->shopper_id)) {
            return $this->shopper_id;

        // If this object has an e-mail address or the user sent one
        } else if ($this->get_email())  {
            global $db;

            $vars = array();
            $vars[] = array(':i_email_id', $this->get_email());

            // FUNCTION get_id_by_email(i_email_id IN jfw_shoppers.email%TYPE)
            $id = array_pop(array_pop($db->get_function('custom.japi_shopper_identity.get_id_by_email(:i_email_id)', $vars)));

            $this->set_id($id);
            $this->shopper_id = $id;
            return $this->shopper_id;

        // Can also get from token
        } else if ($this->get_sign_in_token())  {
            // todo: call get_id_by_token
            return false;
        }
    }


    // todo: test
    public function get_lists($clobber = false) {
        global $pd;
//        $pd->print_object($this, 'User - has token?');
//        $pd->print_object($this->get_sign_in_token(), 'Token');

        if ($this->UserShoppingList != null && !$clobber) {
            return $this->UserShoppingList;

        } else if ($this->get_sign_in_token()) {
            global $db;
            $pd->print_object($this, 'User - has token?');
            $pd->print_object(strtolower($this->get_sign_in_token()), 'token?');

            $vars = array();
            $vars[] = array(':i_sign_in_token', strtolower($this->get_sign_in_token()));

            $pd->print_object($this->get_sign_in_token(), 'About to seek lists using token');
            $rows = $db->get_function('custom.japi_shopper_identity.get_lists_for_shopper(:i_sign_in_token)', $vars);
            $pd->print_object($rows, 'Rows returned by get_lists using token '.$this->get_sign_in_token());

            // Turn the rows into objects
            $this->UserShoppingList = array_to_objects($rows, 'UserShoppingList');

            return $this->UserShoppingList;
        } else {
            return false;
        }
    }

    public function get_sign_in_token() {
        if ($this->sign_in_token != null) {
            return $this->sign_in_token;
        } else {
            return false;
        }
    }

    public function get_temp_token() {
        if ($this->temp_token != null) {
            return $this->temp_token;
        } else {
            return false;
        }
    }

    public function get_username() {
        return $this->user_name;
    }

    public function json($obj = null, $return_json = false) {
        if ($obj == null) {
            $obj = $this;
        }
        return parent::json($obj, $return_json);
    }

    // Most objects in this framework are populated by calling the constructor,
    // but the only way to populate this one is to call this function with good 
    // credentials.
    public function login($password) {
        global $db;

        if (!$this->get_email()) {
            return false;
        }

        // Log them in now that we know who they are. 
        $vars = array();
        $vars[] = array(':i_email_id', $this->get_email());
        $vars[] = array(':i_password', $password);

        // This also exists, but is not yet in use:
        // $token = $db->get_function_as_proc('custom.japi_shopper_identity.login_by_username(:i_username, :i_password)', $vars);
        $token = $db->get_function_as_proc('custom.japi_shopper_identity.Login_by_Email(:i_email_id, :i_password)', $vars);
        // todo: what if it's bad credentials?

        if ($token == null) {
            return false;

        } else {
            $this->set_sign_in_token($token);
            return $this->get_sign_in_token();
        }
    }

    public function password_reset($tmp_token, $password) {
        global $db;

        if (strlen($password) < 8) {
            return false;
        }

        $vars = array();
        $vars[] = array(':temp_token', $tmp_token);
        $vars[] = array(':new_password', $password);

        return $db->get_function_as_proc('custom.japi_shopper_identity.password_reset(:temp_token, :new_password)', $vars) == 'Y';
    }

    public function request_activation() {
        global $db;

        $vars = array();
        $vars[] = array(':i_shopper_id', $this->get_id());

        // Returns a temp token
        $temp_token = $db->get_function_as_proc('custom.japi_shopper_identity.activate_user_request(:i_shopper_id)', $vars);

        if ($temp_token == null) {
            return false;
        } else {
            $this->send_activation_email();
            return $temp_token;
        }
    }

    public function request_password_reset() {
        global $db, $pd;

        if (!$this->get_id()) {
            return false;
        }

        $vars = array();
        $vars[] = array(':shopper_id', $this->get_id());

        $temp_token = $db->get_function_as_proc('custom.japi_shopper_identity.password_reset_request(:shopper_id)', $vars);
        if ($temp_token == null) {
            return false;
        } else {
            $this->set_temp_token($temp_token);
            $pd->print_object('About to send the e-mail');
            $this->send_password_email();
            $pd->print_object('Sent the email');
            return $this->get_temp_token();
        }
    }


    private function send_activation_email() {
        if (!$this->get_email() || !$this->get_temp_token())  {
            return false;
        }


        $fancy = '
<div style="text-align: center;"><img src="logo.jpg" /></div>
<h2>Welcome to com!</h2>
<p>To complete your registration, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

URL?token='.$this->get_temp_token().'

Thanks!
';

        $plain = 'Welcome to com!

To complete your registration, please activate your account by going to the URL below:

URL?token='.$this->get_temp_token().'

Thanks!
';

        // todo: subject could probably be better
        return email_customer($this->get_email(), 'Welcome to com!', $fancy, $plain);
    }


    private function send_password_email() {
        global $pd;
        $pd->print_object('In send_password_email');
        $pd->print_object($this->get_email(), 'E-mail');
        $pd->print_object($this->get_temp_token(), 'Token');

        if (!$this->get_email() || !$this->get_temp_token())  {
            return false;
        }

        $pd->print_object($this->get_email(), 'Have all the data I need');


        $fancy = '
<div style="text-align: center;"><img src="logo.jpg" /></div>
<h2>Welcome to com!</h2>
<p>To reset your password, <a href="todo: ">click here</a> or copy and paste the URL into your browser:</p>

<p>URL?token='.$this->get_temp_token().'</p>

<p>Thanks!</p>
';
        $plain = 'Welcome to com!

To reset your password by going to the URL below:

URL?token='.$this->get_temp_token().'

Thanks!
';
        $pd->print_object('About to actually e-mail');


        return email_customer($this->get_email(), "Reset your com password", $fancy, $plain);
    }

    public function set_email($email) {
        return $this->email = $email;
    }

    public function set_id($email) {
        return $this->shopper_id;
    }

    public function set_sign_in_token($token) {
        return $this->sign_in_token = $token;
    }

    public function set_temp_token($token) {
        return $this->temp_token = $token;
    }

    public function set_username($username) {
        return $this->user_name = $username;
    }
}

我现在有点困惑如何引用购物者列表,如果他们没有列表,则选择建议的列表。

我的(oracle11g)数据库中有一些测试列表:

     ID SHOPPING_LIST_NAME                                 S SEQUENCE
------- -------------------------------------------------- - --------
      3 793d7384fa4fa247d6fae07db104147d0a2dad6e           N
      1 test amnaik shopping list                          N
      4 kj's shopping list                                 N
      5 kj's shopping list from 1384201636                 N
      6 kj's shopping list from 1384201659                 N
      7 kj's shopping list from 1384202055                 N
      8 kj's shopping list from 1384202089                 N
      2 bonner shopping list                               N

8 rows selected.



SHOPPING_LIST_ID SHOPPER_ID ITM_CD    QUANTITY CHANGE_DA CHANGE_US A O
---------------- ---------- --------- -------- --------- --------- - -
               1          2 ABI85MT06        4 28-OCT-13 CUSTOM    N N
               1          1 STZ28AC1         3 11-NOV-13 CUSTOM    Y N
               1          1 ABI85MT06        3 11-NOV-13 CUSTOM    Y N
               1          1 XYZ              1                     Y N
               2          1 XYZ              1                     Y N
               4         67 MND44SA01        1                     Y N
               4         67 MND44SA02        1                     Y N
               2         67 MND44SA02        1                     Y N
               1          1 ABCDEF           1                     Y N

9 rows selected.

通常情况下,任何帮助或建议都将不胜感激。谢谢。


你能发布Shopper类的完整代码吗?一旦你使用令牌实例化该类,如何填充它们的列表就不清楚了。根据上面的代码,你创建一个新的Shopper对象,然后调用get_lists(),它只是获取UserShoppingList中已经存在的内容。你需要在类构造函数中加载这个函数,或者get_lists()实际上应该从数据库中获取。 - Chris Rasco
谢谢@ChrisRasco,我现在发布了整个Shopper类。 - Head Way
1个回答

4
基于Shopper类,你更新后的get_lists函数正在调用一个数据库方法:
$rows = $db->get_function('custom.japi_shopper_identity.get_lists_for_shopper(:i_sign_in_token)', $vars);

该方法返回一个数组,我假设它是来自Oracle数据库的查询结果。在将其转换为一组对象并将其返回给get_shopping_lists.php之后,您需要检查以确保此值不是false,因为这是get_lists()方法调用中的最终else条件。如果是,输出某种类型的错误消息(如果您的消费者期望JSON格式,则可能为JSON格式)。
至于您想要添加的新内容:
- 如果他们没有清单,则自动创建一个清单 - 获取建议的列表
对于第一个问题,在获取行后,您将检查$ rows是否为空,然后调用一个方法来创建一个列表(在Oracle端也可能是某种类型的customer.japi_shopper...函数)。
对于第二个问题,您将使用if / else分支逻辑(如果$suggested == true,则获取这些结果;否则,执行您已经执行的操作...)。

谢谢您的回复,但我还是有点困惑,克里斯。 - Head Way
我应该在购物者类中构建这另外两个函数,还是只需将它们添加到我的get_shopping_lists.php页面中? - Head Way
现在我一直收到“必须传递'token'或'suggested_only'标志”的die消息... - Head Way
给我发封电子邮件,我们可以通过邮件讨论,或者我可以加入IRC。我的电子邮件在我的个人资料中。 - Chris Rasco
我在这个人身上悬赏了一笔赏金。 - Head Way
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接